Andy 胡

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

统计

RAS算法简单示例(Java版)

RSA算法——由三位发明者Ronald Rivest、Adi Shamir 和 Leonard Adleman 姓氏的首字母拼在一起组成。

RSA算法属于“公开密钥加密技术”,其加密和解密的秘钥不同。

用于加密的密钥可以公开,因此称为“公钥”,而用于解密的密钥是只有自己才知道,称为“私钥”。

简单算法如下所示:

#####创建公钥#####

(1)选两个质数a,b。

    如:a=13,b=29

(2)求c=a*b。

    如:c=377

(3)求d=(a-1)*(b-1)

    如:d=336

(4)选择和d没有公约数的e

    如:e = 5

(5)得到公钥(c,e)

PublicKey [c=377, e=5]

 

#####创建私钥#####

(1)求出f,使其满足(f*e)÷d余1

    如:f=269

(2)得到私钥(c,f)

PrivateKey [c=377, f=269]

 

###使用公钥加密:密文 =((明文的e次方)÷c)的余数

###使用私钥解密:明文 =((密文的f次方)÷c)的余数

 

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
class PublicKey {
    int c;
    int e;
 
    @Override
    public String toString() {
        return "PublicKey [c=" + c + ", e=" + e + "]";
    }
}
 
class PrivateKey {
    int c;
    int f;
 
    @Override
    public String toString() {
        return "PrivateKey [c=" + c + ", f=" + f + "]";
    }
}
 
/**
 *
 * @author Andy Hoo
 *
 */
public class RSA {
    PublicKey publicKey = new PublicKey();
    PrivateKey privateKey = new PrivateKey();
 
    public void mKey() {
        System.out.println("#####创建公钥#####");
        System.out.println("(1)选两个质数a,b。");
        int a = 13, b = 29;
        System.out.println("    如:a=" + a + ",b=" + b);
        System.out.println("(2)求c=a*b。");
        int c = a * b;
        System.out.println("    如:c=" + c);
        System.out.println("(3)求d=(a-1)*(b-1)");
        int d = (a - 1) * (b - 1);
        System.out.println("    如:d=" + d);
        System.out.println("(4)选择和d没有公约数的e");
        int e;
        for (e = 2; e < d; e++) {
            if (!getCommonDivisor(d, e)) {
                break;
            }
        }
        System.out.println("    如:e = " + e);
 
        System.out.println("(5)得到公钥(c,e)");
        publicKey.c = c;
        publicKey.e = e;
        System.out.println(publicKey);
 
        System.out.println("#####创建私钥#####");
        System.out.println("(1)求出f,使其满足(f*e)÷d余1");
        int f = 0;
        for (f = 1; f <= d; f++) {
            if ((f * e) % d == 1) {
                System.out.println("    如:f=" + f);
                break;
            }
        }
        System.out.println("(2)得到私钥(c,f)");
        privateKey.c = c;
        privateKey.f = f;
        System.out.println(privateKey);
    }
 
    /**
     * 加密
     *
     * @param original原文
     * @return 密文
     */
    public int encrypt(int original) {
        System.out.println("###使用公钥加密:密文 =((明文的e次方)÷c)的余数");
        int cryptograph = powAndRemainder(original, publicKey.e, publicKey.c);
        return cryptograph;
    }
 
    /**
     * 解密
     *
     * @param cryptograph密文
     */
    public int decrypt(int cryptograph) {
        System.out.println("###使用私钥解密:明文 =((密文的f次方)÷c)的余数");
        int after = powAndRemainder(cryptograph, privateKey.f, privateKey.c);
        return after;
    }
 
    /**
     * 加密解密通用方法: (a的b次方)%c
     */
    public static int powAndRemainder(int a, int b, int c) {
        BigDecimal bd = new BigDecimal(a);
        // 求幂
        BigDecimal bd2 = bd.pow(b);
        // 取余数
        BigDecimal[] dr = bd2.divideAndRemainder(new BigDecimal(c));
        return dr[1].intValue();
    }
 
    /**
     * 求公约数
     *
     * @param a1
     * @param a2
     * @return true有公约数/false没有公约数
     */
    public static boolean getCommonDivisor(int a1, int a2) {
        List<Integer> commonDivisor = new ArrayList<Integer>();
        int min = Math.min(a1, a2);
        for (int n = 2; n <= min; n++) {
            if (a1 % n == 0 && a2 % n == 0) {
                commonDivisor.add(n);
            }
        }
        if (commonDivisor.size() != 0) {
            System.out.println(a1 + "," + a2 + "有公约数" + commonDivisor);
            return true;
        } else {
            System.out.println(a1 + "," + a2 + "没有公约数");
            return false;
        }
    }
 
    public static void main(String[] args) {
        RSA rsa = new RSA();
        // 获取秘钥
        rsa.mKey();
        // 原文
        int original = 101;
        System.out.println("原文:" + original);
        // 加密
        int cryptograph = rsa.encrypt(original);
        System.out.println("密文:" + cryptograph);
        // 解密
        int after = rsa.decrypt(cryptograph);
        System.out.println("解密后:" + after);
    }
}

  

posted on   talkwah  阅读(1133)  评论(0编辑  收藏  举报

编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
点击右上角即可分享
微信分享提示