|

楼主 |
发表于 2022-5-3 11:22:05
|
显示全部楼层
本帖最后由 108林荟萃 于 2022-6-2 16:03 编辑
RSA算法实现 RSA算法常用于非对称加密,非对称加密流程如下: - (1)乙方生成两把密钥(公钥和私钥)。公钥是公开的,任何人都可以获得,私钥则是保密的。
- (2)甲方获取乙方的公钥,然后用它对信息加密。
- (3)乙方得到加密后的信息,用私钥解密。
1.Encryptors.java - package RSAEncryptionAlgorithm;
- import java.io.UnsupportedEncodingException;
- import java.math.BigInteger;
- import java.util.Random;
- import java.util.Base64;
- import java.util.Scanner;
- public class Encryptors {
- public static void main(String[] args) throws UnsupportedEncodingException {
- //System.out.println(RSATolls.GetGcd(new BigInteger("168468468"), new BigInteger("489416814641")));;
- //System.out.println(RSATolls.GetModInv(new BigInteger("168468468"), new BigInteger("489416814641")));
- BigInteger p = BigInteger.probablePrime(256, new Random());
- BigInteger q = BigInteger.probablePrime(256, new Random());
- BigInteger n = p.multiply(q);
- String nn = Base64.getEncoder().encodeToString(n.toByteArray());//编码之后的n
- System.out.println("n:");
- System.out.println(nn);
- BigInteger fai_n = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
- BigInteger e = new BigInteger(fai_n.bitLength(), new Random());
- //System.out.println(n);
- //System.out.println(fai_n);
- while (!RSATolls.GetGcd(e, fai_n).equals(BigInteger.ONE)) {
- do {
- e = new BigInteger(fai_n.bitLength(), new Random());
- } while (e.compareTo(fai_n) >= 1);
- }//公钥
- BigInteger d = RSATolls.GetModInv(e, fai_n);//私钥
- String ee = Base64.getEncoder().encodeToString(e.toByteArray());//编码之后的公钥
- System.out.println("公钥:");
- System.out.println(ee);
- String dd = Base64.getEncoder().encodeToString(d.toByteArray());//编码之后的密钥
- System.out.println("私钥:");
- System.out.println(dd);
- //加密
- System.out.println("加密:");
- System.out.println("请输入需要加密的明文:");
- Scanner scan = new Scanner(System.in);
- String userInput = scan.nextLine();
- byte[] plaintextByte = userInput.getBytes("UTF-8");
- byte[] plaintextByteCode = Base64.getEncoder().encode(plaintextByte);
- BigInteger plaintextBI = new BigInteger(plaintextByteCode);
- //System.out.println(plaintextBI);
- System.out.println("输入n:" + nn);
- System.out.println("输入公钥e:" + ee);
- n = new BigInteger(Base64.getDecoder().decode(nn.getBytes()));
- e = new BigInteger(Base64.getDecoder().decode(ee.getBytes()));
- BigInteger c = plaintextBI.modPow(e, n);
- System.out.println("密文:");
- System.out.println(Base64.getEncoder().encodeToString(c.toByteArray()));
- //解密
- System.out.println("解密:");
- String cipher = Base64.getEncoder().encodeToString(c.toByteArray());
- System.out.println("输入密文" + cipher);
- c = new BigInteger(Base64.getDecoder().decode(cipher));
- System.out.println("输入n:" + nn);
- System.out.println("输入私钥d:" + dd);
- n = new BigInteger(Base64.getDecoder().decode(nn.getBytes()));
- d = new BigInteger(Base64.getDecoder().decode(dd.getBytes()));
- BigInteger m = c.modPow(d, n);
- System.out.println("得到明文:");
- System.out.println(new String(Base64.getDecoder().decode(m.toByteArray()), "UTF-8"));
- }
- }
复制代码2.RSATolls.java - package RSAEncryptionAlgorithm;
- import java.math.BigInteger;
- public class RSATolls {
- //求最大公因数
- public static BigInteger GetGcd(BigInteger n, BigInteger u) {
- BigInteger n1, n2;
- n1 = n;
- n2 = u;
- while (true) {
- //q = BigInteger.DivRem(n1, n2, out r);
- BigInteger[] c = n1.divideAndRemainder(n2);
- if (!c[1].equals(BigInteger.ZERO)) {
- n1 = n2;
- n2 = c[1];
- } else
- break;
- }
- return n2;
- }
- //求模逆
- public static BigInteger GetModInv(BigInteger value, BigInteger modulus) {
- BigInteger n1, n2, b1 = BigInteger.ZERO, b2 = BigInteger.ONE, t;
- n1 = modulus;
- n2 = value;
- while (true) {
- BigInteger[] c = n1.divideAndRemainder(n2);
- if (!c[1].equals(BigInteger.ZERO)) {
- n1 = n2;
- n2 = c[1];
- t = b2;
- b2 = b1.subtract(c[0].multiply(b2));
- b1 = t;
- } else if (!n2.equals(BigInteger.ONE)) {
- b2 = modulus.add(new BigInteger("-1"));
- break;
- } else {
- break;
- }
- }
- if (b2.equals(modulus.add(new BigInteger("-1")))) {
- return new BigInteger("-1");
- }
- while (b2.compareTo(BigInteger.ZERO) == -1) {
- b2 = b2.add(modulus);
- }
- return b2.remainder(modulus);
- }
- }
复制代码3.运行截图:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|