教学服务系统

 找回密码
 立即注册
搜索
查看: 617|回复: 2

信息计算2019级1班4号吴春滟

[复制链接]

9

主题

20

帖子

85

积分

注册会员

Rank: 2

积分
85
发表于 2022-6-2 22:16:31 | 显示全部楼层 |阅读模式
本帖最后由 吴春滟 于 2022-6-2 22:20 编辑

大数RSA算法的实现

一.原理:
1. 随意选择两个大的质数p和q,p不等于q,计算N = pq.
2. 根据欧拉函数,求得r=φ(N)=φ(p)φ(q)=(p-1)(q-1)。
3. 选择一个小于r的整数e,是e与r互质。并求得e关于r的模反元素,命名为d。(求d令ed≡1(mod r))。(模反元素存在,当且仅当e与r互质)
4.公开整数n和e,秘密保存d;
5.将明文m(m<n,是一个整数)加密成密文c,加密算法为c=E(m)=memodn;
6.将密文c解密为明文m,解密算法为m=D(c)=cdmodn。


  1. package mm;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. import java.math.BigInteger;
  5. import java.security.SecureRandom;

  6. public class Rsa1 {
  7.         private static final int ORDER = 100000;// 随机数的数量级
  8.         private static final int MIN = 10000; // 选择的随机数的最小值

  9.         public static long getPrime() { // 获取一个随机数,判断是否为素数,并用MillerRabin方法检验
  10.                 long x = 0;
  11.                 while (x % 2 == 0 || !MillerRabin(x, 5)) {
  12.                         x = getRandom();
  13.                 }
  14.                 return x;
  15.         }

  16.         public static long getRandom() {// 随机生成一个奇数
  17.                 long x = 3;
  18.                 Random rd = new Random();
  19.                 do {
  20.                         x = rd.nextInt(ORDER);
  21.                 } while (x < MIN || x % 2 == 0);
  22.                 return x;
  23.         }

  24.         public static boolean MillerRabin(long n, int t) {
  25.                 for (int i = 0; i < t; i++)
  26.                         if (!isPrime(n))
  27.                                 return false;
  28.                 return true;
  29.         }

  30.         public static boolean isPrime(long n) {
  31.                 long k, q;
  32.                 SecureRandom random = new SecureRandom();
  33.                 for (k = 0; (((n - 1) >> k) & 1) == 0; k++)
  34.                         ;
  35.                 q = (n - 1) >> k;
  36.                 long a = random.nextLong(n);
  37.                 if (squareMultiply(a, q, n) == 1)
  38.                         return true;
  39.                 for (int j = 0; j < k; j++)
  40.                         if (squareMultiply(a, (int) Math.pow(2, j) * q, n) == n - 1)
  41.                                 return true;
  42.                 return false;
  43.         }

  44.         public static long squareMultiply(long a, long b, long p) {
  45.                 long x = 1, y = a;
  46.                 long len = (long) Math.ceil((Math.log(b) / Math.log(2)));
  47.                 for (int i = 0; i < len; i++) {
  48.                         if (((b >> i) & 1) == 1) {
  49.                                 x = (x * y) % p;
  50.                         }
  51.                         y = (y * y) % p;
  52.                 }
  53.                 return x;
  54.         }

  55.         public static long powMod(long x, long y, long z) {//模幂运算

  56.                 if (y == 0)
  57.                         return 1 % z;

  58.                 long half = powMod(x, y >> 1, z);

  59.                 half = (half * half) % z;

  60.                 if ((y & 1) == 0) { // y是偶数

  61.                         return half;

  62.                 } else { // y是奇数

  63.                         return (half * (x % z)) % z;

  64.                 }

  65.         }

  66.         public static long Inverse(long a, long b) { // 模逆运算
  67.                 long[] m = { 1, 0, a };
  68.                 long[] n = { 0, 1, b };
  69.                 long[] temp = new long[3];
  70.                 long q = 0; // 初始化
  71.                 boolean flag = true;
  72.                 while (flag) {
  73.                         q = m[2] / n[2];
  74.                         for (int i = 0; i < 3; i++) {
  75.                                 temp[i] = m[i] - q * n[i];
  76.                                 m[i] = n[i];
  77.                                 n[i] = temp[i];
  78.                         }
  79.                         if (n[2] == 1) {
  80.                                 if (n[1] < 0) {
  81.                                         n[1] = n[1] + a;
  82.                                 }
  83.                                 return n[1];
  84.                         }
  85.                         if (n[2] == 0) {
  86.                                 flag = false;
  87.                         }
  88.                 }
  89.                 return 0;
  90.         }

  91.         public static void main(String[] args) {
  92.                 long p = getPrime(); // 生成两个素数
  93.                 long q = getPrime();
  94.                 while (p == q) {
  95.                         q = getPrime();
  96.                 }
  97.                 long n = p * q;
  98.                 long φ = (p - 1) * (q - 1);
  99.                 long e = getPrime();
  100.                 long d = Inverse(φ, e);
  101.                 System.out.println("p:"+p);
  102.                 System.out.println("q:"+q);
  103.                 System.out.println("n:"+n);
  104.                 System.out.println("φ:"+φ);
  105.                 System.out.println("e:"+e);
  106.                 System.out.println("d:"+d);

  107.                 System.out.print("请输入明文:");
  108.                 Scanner sc = new Scanner(System.in);
  109.                 long m=sc.nextLong();
  110.                 long c=powMod(m, e, n);
  111.                 System.out.println("密文:" + c);
  112.                 m = powMod(c, d, n);
  113.                 System.out.println("解密得到明文:" + m);
  114.                 sc.close();
  115.         }
  116. }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

9

主题

20

帖子

85

积分

注册会员

Rank: 2

积分
85
 楼主| 发表于 2022-6-2 22:29:51 | 显示全部楼层
本帖最后由 吴春滟 于 2022-6-2 22:41 编辑

ECC
一、算法简介
椭圆加密算法(ECC)是一种公钥加密体制,最初由Koblitz和Miller两人于1985年提出,其数学基础是利用椭圆曲线上的有理点构成Abel加法群上椭圆离散对数的计算困难性。公钥密码体制根据其所依据的难题一般分为三类:大素数分解问题类、离散对数问题类、椭圆曲线类。有时也把椭圆曲线类归为离散对数类。
相比RSA,ECC优势是可以使用更短的密钥,来实现与RSA相当或更高的安全,RSA加密算法也是一种非对称加密算法,在公开密钥加密和电子商业中RSA被广泛使用。

二、原理
1、用户A选定一条椭圆曲线Ep(a,b),并取椭圆曲线上一点,作为基点G;
2、用户A选择一个私有密钥k,并生成公开密钥K=kG;
3、用户A将Ep(a,b)和点K,G传给用户B;
4、用户B接到信息后 ,将待传输的明文编码到Ep(a,b)上一点M,并产生一个随机整数r;
5、用户B计算点C1=M+rK,C2=rG;
6、用户B将C1、C2传给用户A;
7、用户A接到信息后,计算C1-kC2,结果就是点M。
  1. #include<iostream>
  2. #include<math.h>
  3. #include<time.h>
  4. using namespace std;

  5. class point
  6. {
  7. public:
  8.         int x;
  9.         int y;
  10. };
  11. point P[100];
  12. int num = 0;

  13. //取模
  14. int my_mod(int a, int p)
  15. {
  16.         int i;
  17.         i = a / p;
  18.         int re = a - i * p;
  19.         if (re >= 0)
  20.         {
  21.                 return re;
  22.         }
  23.         else
  24.         {
  25.                 return re + p;
  26.         }
  27. }

  28. int my_pow(int a, int m, int p)
  29. {
  30.         int result = 1;
  31.         for (int i = 0; i < m; i++)
  32.         {
  33.                 result = (result * a) % p;
  34.         }
  35.         return result;
  36. }

  37. int my_sqrt(int s)
  38. {
  39.         int t;
  40.         t = (int)sqrt(s);
  41.         if (t * t == s)
  42.         {
  43.                 return t;
  44.         }
  45.         else {
  46.                 return -1;
  47.         }
  48. }

  49. void all_points(int a, int b, int p)
  50. {
  51.         for (int i = 0; i < p; i++)
  52.         {
  53.                 int s = i * i * i + a * i + b;
  54.                 while (s < 0)
  55.                 {
  56.                         s += p;
  57.                 }
  58.                 s = my_mod(s, p);

  59.                 int re = my_pow(s, (p - 1) / 2, p);
  60.                 if (re == 1)
  61.                 {
  62.                         //求y
  63.                         int n = 1, y;
  64.                         int f = my_sqrt(s);
  65.                         if (f != -1)
  66.                         {
  67.                                 y = f;
  68.                         }
  69.                         else
  70.                         {
  71.                                 for (; n <= p - 1;)
  72.                                 {
  73.                                         s = s + n * p;
  74.                                         f = my_sqrt(s);
  75.                                         if (f != -1)
  76.                                         {
  77.                                                 y = f;
  78.                                                 break;
  79.                                         }
  80.                                         n++;
  81.                                 }
  82.                         }
  83. y = my_mod(y, p);
  84.                         P[num].x = i;
  85.                         P[num].y = y;
  86.                         num++;
  87.                         if (y != 0)
  88.                         {
  89.                                 P[num].x = i;
  90.                                 P[num].y = (p - y) % p;
  91.                                 num++;
  92.                         }
  93.                 }
  94.         }
  95. }

  96. void show()
  97. {
  98.         for (int i = 0; i < num; i++)
  99.         {
  100.                 cout << P[i].x << " " << P[i].y << endl;
  101.         }
  102. }


  103. int extend(int a, int b, int& x, int& y)
  104. {
  105.         if (b == 0)
  106.         {
  107.                 x = 1;
  108.                 y = 0;
  109.                 return a;
  110.         }
  111.         int r = extend(b, a % b, x, y);
  112.         int t = x;
  113.         x = y;
  114.         y = t - a / b * y;
  115.         return r;
  116. }

  117. //递归扩展欧几里得求逆
  118. int inv(int a, int b)
  119. {
  120.         int x, y;
  121.         int r = extend(a, b, x, y);
  122.         if (r != 1)
  123.         {
  124.                 return 0;
  125.         }
  126.         x = x % b;
  127.         if (x < 0)
  128.         {
  129.                 x = x + b;
  130.         }
  131.         return x;
  132. }


  133. //加法运算
  134. point add(point p1, point p2, int a, int p)
  135. {
  136.         long t; int flag = 0;
  137.         int x1 = p1.x; int y1 = p1.y;
  138.         int x2 = p2.x; int y2 = p2.y;
  139.         int tx, ty; int x3, y3;

  140.         if ((x2 == x1) && (y2 == y1))
  141.         {
  142.                 //相同点
  143.                 if (y1 == 0)
  144.                 {
  145.                         flag = 1;
  146.                 }
  147.                 else
  148.                 {
  149.                         t = (3 * x1 * x1 + a) * inv(2 * y1, p) % p;
  150.                 }
  151.         }
  152.         else
  153.         {
  154.                 //不同点
  155.                 ty = y2 - y1;
  156.                 tx = x2 - x1;
  157.                 while (tx < 0)
  158.                 {
  159.                         tx = tx + p;
  160.                 }
  161.                 while (ty < 0)
  162.                 {
  163.                         ty = ty + p;
  164.                 }

  165.                 if (tx == 0 && ty != 0)
  166.                 {
  167.                         flag = 1;
  168.                 }
  169.                 else
  170.                 {
  171.                         //点不相等
  172.                         t = ty * inv(tx, p) % p;
  173.                 }
  174.         }

  175.         if (flag == 1)
  176.         {
  177.                 //无限点
  178.                 p2.x = -1;
  179.                 p2.y = -1;
  180.         }
  181.         else
  182.         {
  183.                 x3 = (t * t - x1 - x2) % p;
  184.                 y3 = (t * (x1 - x3) - y1) % p;
  185.                 while (x3 < 0)
  186.                 {
  187.                         x3 += p;
  188.                 }
  189.                 while (y3 < 0)
  190.                 {
  191.                         y3 += p;
  192.                 }
  193.                 p2.x = x3;
  194.                 p2.y = y3;
  195.         }
  196.         return p2;
  197. }

  198. //随机选取一个生成元并计算阶
  199. int jie(point& pp, int a, int p)
  200. {
  201.         int ii = rand() % num;
  202.         point P0 = P[ii];
  203.         point p1, p2;
  204.         int number = 1;
  205. p1.x = P0.x; p2.x = P0.x;
  206.         p1.y = P0.y; p2.y = P0.y;
  207.         while (true)
  208.         {
  209.                 p2 = add(p1, p2, a, p);
  210.                 if (p2.x == -1 && p2.y == -1)
  211.                 {
  212.                         break;
  213.                 }
  214.                 number++;
  215.                 if (p2.x == p1.x)
  216.                 {
  217.                         break;
  218.                 }
  219.         }
  220.         pp.x = p1.x;
  221.         pp.y = p1.y;
  222.         int n = ++number;
  223.         return n;
  224. }

  225. //素数判断
  226. bool judge(int num)
  227. {
  228.         bool ret = true;
  229.         int ubound = sqrt(num) + 1;
  230.         for (int i = 2; i < ubound; i++)
  231.         {
  232.                 if (num % i == 0)
  233.                 {
  234.                         ret = false;
  235.                         break;
  236.                 }
  237.         }
  238.         return ret;
  239. }

  240. //计算kG
  241. point cal(point G, int k, int a, int p)
  242. {
  243.         point temp = G;
  244.         for (int i = 0; i < k - 1; i++)
  245.         {
  246.                 temp = add(temp, G, a, p);
  247.         }
  248.         return temp;
  249. }

  250. int main()
  251. {
  252.         srand(time(NULL));
  253.         int a, b, p;
  254.         point generator; int n;
  255.         char SE[10];
  256.         char CR[10];

  257.         cout << "请输入椭圆曲线群(a,b,p):";
  258.         cin >> a >> b >> p;
  259.         cout << "请输入明文:";
  260.         cin >> SE;
  261.         cout << "请输入密钥:";
  262.         cin >> CR;

  263.         //计算所有点
  264.         all_points(a, b, p);
  265.         //选取生成元,直到阶为素数
  266.         do
  267.         {
  268.                 n = jie(generator, a, p);
  269.         } while (judge(n) == false);
  270.         cout << endl << "选取生成元(" << generator.x << "," << generator.y << "),阶为:" << n << endl;
  271.         //选取私钥
  272.         int ka = int(CR[0]) % (n - 1) + 1;//选取使用的密钥
  273.         point pa = cal(generator, ka, a, p);//计算公钥
  274.         cout << "私钥:" << ka << endl;
  275.         cout << "公钥:(" << pa.x << "," << pa.y << ")" << endl;

  276.         //加密
  277.         int k = 0;//随机数k
  278.         k = rand() % (n - 2) + 1;
  279.         point C1 = cal(generator, k, a, p);//计算C1

  280.         //m嵌入到椭圆曲线
  281.         int t = rand() % num; //选择映射点
  282.         point Pt = P[t];
  283.         point P2 = cal(pa, k, a, p);
  284.         point Pm = add(Pt, P2, a, p);
  285.         cout << endl << "要发送的密文:" << endl;
  286.         cout << "kG=(" << C1.x << "," << C1.y << "),pt+kPa=(" << Pm.x << "," << Pm.y << ")";
  287.         int C[100];
  288.         cout << ",C = { ";
  289.         for (int i = 0; i < strlen(SE); i++)
  290.         {
  291.                 C[i] = int(SE[i]) * Pt.x + Pt.y;//选取要加密的明文
  292.                 cout << C[i] << " ";
  293.         }
  294.         cout << "}" << endl;


  295.         //解密
  296.         point temp, temp1;
  297.         int m;
  298.         temp = cal(C1, ka, a, p);
  299.         temp.y = 0 - temp.y;
  300.         temp1 = add(Pm, temp, a, p);//求解Pt
  301.         printf("\n解密结果:\n");
  302.         for (int i = 0; i < strlen(SE); i++)
  303.         {
  304.                 m = (C[i] - temp1.y) / temp1.x;
  305.                 printf("%c", char(m));//输出密文
  306.         }
  307.         printf("\n");

  308.         return 0;
  309. }
复制代码

回复

使用道具 举报

9

主题

20

帖子

85

积分

注册会员

Rank: 2

积分
85
 楼主| 发表于 2022-6-3 13:22:59 | 显示全部楼层

一、RSA加密

RSA加密是一种非对称加密。可以在不直接传递密钥的情况下,完成解密;是由一对密钥来进行加解密的过程,分别称为公钥和私钥。两者之间有数学相关,该加密算法的原理就是对一极大整数做因数分解的困难性来保证安全性。通常个人保存私钥,公钥是公开的(可能同时多人持有)。

二、对签名和验签过程详细解释:

签名过程:

  • A计算消息m的消息摘要,记为 h(m)
  • A使用私钥(n,d)对h(m)加密,生成签名s, s满足:s=(h(m))^d mod n;
    由于A是用自己的私钥对消息摘要加密,所以只用使用s的公钥才能解密该消息摘要,这样A就不可否认自己发送了该消息给B.
  • A发送消息和签名(m,s)给B

验签过程:

  • B计算消息m的消息摘要(计算方式和A相同),记为h(m)
  • B使用A的公钥(n,e)解密s,得到 H(m), H(m) = s^e mod n
  • B比较H(m)与h(m),相同才能证明验签成功

三、对加密/解密和签名/验签完整过程详细解释:
A->B:
1.A提取消息m的消息摘要h(m),并使用自己的私钥对摘要h(m)进行加密,生成签名s
2.A将签名s和消息m一起,使用B的公钥进行加密,生成密文c,发送给B

B:
1.B接收到密文c,使用自己的私钥解密c得到明文m和数字签名s
2.B使用A的公钥解密数字签名s解密得到H(m)
3.B使用相同的方法提取消息m的消息摘要h(m)
4.B比较两个消息摘要。相同则验证成功;不同则验证失败


  1. import java.io.ByteArrayOutputStream;
  2. import java.security.KeyFactory;
  3. import java.security.KeyPair;
  4. import java.security.KeyPairGenerator;
  5. import java.security.PrivateKey;
  6. import java.security.PublicKey;
  7. import java.security.Signature;
  8. import java.security.spec.PKCS8EncodedKeySpec;
  9. import java.security.spec.X509EncodedKeySpec;
  10. import javax.crypto.Cipher;
  11. import org.apache.commons.codec.binary.Base64;

  12. public class TestRSA {

  13.     /**
  14.      * RSA最大加密明文大小
  15.      */
  16.     private static final int MAX_ENCRYPT_BLOCK = 64;

  17.     /**
  18.      * RSA最大解密密文大小
  19.      */
  20.     private static final int MAX_DECRYPT_BLOCK = 75;

  21.     /**
  22.      * 获取密钥对
  23.      * @return 密钥对
  24.      */
  25.     public static KeyPair getKeyPair() throws Exception {
  26.         KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
  27.         generator.initialize(512);
  28.         return generator.generateKeyPair();
  29.     }

  30.     /**
  31.      * 获取私钥
  32.      *
  33.      * @param privateKey 私钥字符串
  34.      * @return
  35.      */
  36.     public static PrivateKey getPrivateKey(String privateKey) throws Exception {
  37.         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  38.         byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());
  39.         PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
  40.         return keyFactory.generatePrivate(keySpec);
  41.     }

  42.     /**
  43.      * 获取公钥
  44.      * @param publicKey 公钥字符串
  45.      * @return
  46.      */
  47.     public static PublicKey getPublicKey(String publicKey) throws Exception {
  48.         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  49.         byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
  50.         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
  51.         return keyFactory.generatePublic(keySpec);
  52.     }

  53.     /**
  54.      * RSA加密
  55.      *
  56.      * @param data 待加密数据
  57.      * @param publicKey 公钥
  58.      * @return
  59.      */
  60.     public static String encrypt(String data, PublicKey publicKey) throws Exception {
  61.         Cipher cipher = Cipher.getInstance("RSA");
  62.         cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  63.         int inputLen = data.getBytes().length;
  64.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  65.         int offset = 0;
  66.         byte[] cache;
  67.         int i = 0;
  68.         // 对数据分段加密
  69.         while (inputLen - offset > 0) {
  70.             if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
  71.                 cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
  72.             } else {
  73.                 cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
  74.             }
  75.             out.write(cache, 0, cache.length);
  76.             i++;
  77.             offset = i * MAX_ENCRYPT_BLOCK;
  78.         }
  79.         byte[] encryptedData = out.toByteArray();
  80.         out.close();
  81.         // 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
  82.         // 加密后的字符串
  83.         return new String(Base64.encodeBase64String(encryptedData));
  84.     }

  85.     /**
  86.      * RSA解密
  87.      *
  88.      * @param data 待解密数据
  89.      * @param privateKey 私钥
  90.      * @return
  91.      */
  92.     public static String decrypt(String data, PrivateKey privateKey) throws Exception {
  93.         Cipher cipher = Cipher.getInstance("RSA");
  94.         cipher.init(Cipher.DECRYPT_MODE, privateKey);
  95.         byte[] dataBytes = Base64.decodeBase64(data);
  96.         int inputLen = dataBytes.length;
  97.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  98.         int offset = 0;
  99.         byte[] cache;
  100.         int i = 0;
  101.         // 对数据分段解密
  102.         while (inputLen - offset > 0) {
  103.             if (inputLen - offset > MAX_DECRYPT_BLOCK) {
  104.                 cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
  105.             } else {
  106.                 cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
  107.             }
  108.             out.write(cache, 0, cache.length);
  109.             i++;
  110.             offset = i * MAX_DECRYPT_BLOCK;
  111.         }
  112.         byte[] decryptedData = out.toByteArray();
  113.         out.close();
  114.         // 解密后的内容
  115.         return new String(decryptedData, "UTF-8");
  116.     }

  117.     /**
  118.      * 签名
  119.      * @param data 待签名数据
  120.      * @param privateKey 私钥
  121.      * @return 签名
  122.      */
  123.     public static String sign(String data, PrivateKey privateKey) throws Exception {
  124.         byte[] keyBytes = privateKey.getEncoded();
  125.         PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
  126.         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  127.         PrivateKey key = keyFactory.generatePrivate(keySpec);
  128.         Signature signature = Signature.getInstance("MD5withRSA");
  129.         signature.initSign(key);
  130.         signature.update(data.getBytes());
  131.         return new String(Base64.encodeBase64(signature.sign()));
  132.     }

  133.     /**
  134.      * 验签
  135.      *
  136.      * @param srcData 原始字符串
  137.      * @param publicKey 公钥
  138.      * @param sign 签名
  139.      * @return 是否验签通过
  140.      */
  141.     public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception {
  142.         byte[] keyBytes = publicKey.getEncoded();
  143.         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
  144.         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  145.         PublicKey key = keyFactory.generatePublic(keySpec);
  146.         Signature signature = Signature.getInstance("MD5withRSA");
  147.         signature.initVerify(key);
  148.         signature.update(srcData.getBytes());
  149.         return signature.verify(Base64.decodeBase64(sign.getBytes()));
  150.     }
  151. }
  152. public static void main(String[] args) {
  153.         try {
  154.             // 生成密钥对
  155.             KeyPair keyPair = getKeyPair();
  156.             String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
  157.             String publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
  158.             System.out.println("私钥:" + privateKey);
  159.             System.out.println("公钥:" + publicKey);
  160.             // RSA加密 待加密的文字内容
  161.             String data = "123456";
  162.             String encryptData = encrypt(data, getPublicKey(publicKey));
  163.             System.out.println("加密前内容:" + data);
  164.             System.out.println("加密后内容:" + encryptData);
  165.             // RSA解密
  166.             String decryptData = decrypt(encryptData, getPrivateKey(privateKey));
  167.             System.out.println("解密后内容:" + decryptData);

  168.             // RSA签名
  169.             String sign = sign(data, getPrivateKey(privateKey));
  170.             // RSA验签
  171.             boolean result = verify(data, getPublicKey(publicKey), sign);
  172.             System.out.print("验签结果:" + result);
  173.         } catch (Exception e) {
  174.             e.printStackTrace();
  175.             System.out.print("加解密异常");
  176.         }
  177. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 01:41 , Processed in 0.016369 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表