用Java设计一个rsa加密算法,要求可实现键盘输入明文、密钥,键盘输出每一轮加密结果和最后加密解密的结果
时间: 2024-02-11 07:07:16 浏览: 171
下面是一个简单的RSA加密算法Java实现的示例代码,可以实现键盘输入明文、密钥,键盘输出每一轮加密结果和最后加密解密的结果:
```java
import java.math.BigInteger;
import java.util.Scanner;
public class RSAEncryption {
private static final BigInteger ONE = BigInteger.ONE;
private static final BigInteger TWO = BigInteger.valueOf(2);
private static final BigInteger THREE = BigInteger.valueOf(3);
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 读取明文
System.out.print("Enter plain text: ");
String plainText = scanner.nextLine();
// 读取密钥
System.out.print("Enter public key (n,e): ");
String[] publicKeyParts = scanner.nextLine().split(",");
BigInteger n = new BigInteger(publicKeyParts[0]);
BigInteger e = new BigInteger(publicKeyParts[1]);
// 加密明文
byte[] encryptedBytes = encrypt(plainText.getBytes(), n, e);
System.out.println("Encrypted bytes: " + new BigInteger(1, encryptedBytes).toString(16));
// 解密密文
System.out.print("Enter private key (d): ");
BigInteger d = new BigInteger(scanner.nextLine());
byte[] decryptedBytes = decrypt(encryptedBytes, n, d);
String decryptedText = new String(decryptedBytes);
System.out.println("Decrypted text: " + decryptedText);
}
private static byte[] encrypt(byte[] plainBytes, BigInteger n, BigInteger e) {
BigInteger plain = new BigInteger(plainBytes);
BigInteger encrypted = plain.modPow(e, n);
byte[] encryptedBytes = encrypted.toByteArray();
// 添加填充字节,确保密文长度与n的位数相同
if (encryptedBytes.length < n.bitLength() / 8) {
byte[] paddedBytes = new byte[n.bitLength() / 8 - encryptedBytes.length];
for (int i = 0; i < paddedBytes.length; i++) {
paddedBytes[i] = (byte) (Math.random() * 256);
}
encryptedBytes = concat(paddedBytes, encryptedBytes);
}
// 输出每一轮加密结果
System.out.println("Round 1: " + new BigInteger(1, encryptedBytes).toString(16));
// 进行第二轮加密,确保RSA填充方案的安全性
BigInteger r = generateRandomNumber(n.bitLength() - 1);
BigInteger x = new BigInteger(1, concat(new byte[] { 0 }, encryptedBytes));
BigInteger y = r.modPow(TWO, n);
BigInteger z = x.multiply(y).mod(n);
byte[] encryptedBytes2 = z.toByteArray();
// 添加填充字节,确保密文长度与n的位数相同
if (encryptedBytes2.length < n.bitLength() / 8) {
byte[] paddedBytes = new byte[n.bitLength() / 8 - encryptedBytes2.length];
for (int i = 0; i < paddedBytes.length; i++) {
paddedBytes[i] = (byte) (Math.random() * 256);
}
encryptedBytes2 = concat(paddedBytes, encryptedBytes2);
}
// 输出每一轮加密结果
System.out.println("Round 2: " + new BigInteger(1, encryptedBytes2).toString(16));
return encryptedBytes2;
}
private static byte[] decrypt(byte[] encryptedBytes, BigInteger n, BigInteger d) {
// 进行第一轮解密
BigInteger x = new BigInteger(1, encryptedBytes);
BigInteger y = x.modPow(d, n);
byte[] decryptedBytes = y.toByteArray();
// 去除填充字节
int index = 0;
for (int i = 0; i < decryptedBytes.length; i++) {
if (decryptedBytes[i] != 0) {
index = i;
break;
}
}
decryptedBytes = Arrays.copyOfRange(decryptedBytes, index, decryptedBytes.length);
// 输出每一轮解密结果
System.out.println("Round 1: " + new BigInteger(1, decryptedBytes).toString(16));
// 进行第二轮解密
BigInteger z = new BigInteger(1, decryptedBytes);
BigInteger r = z.modPow(d.subtract(ONE).divide(TWO), n);
BigInteger y2 = r.modInverse(n).multiply(z).mod(n);
byte[] decryptedBytes2 = y2.toByteArray();
// 去除填充字节
index = 0;
for (int i = 0; i < decryptedBytes2.length; i++) {
if (decryptedBytes2[i] != 0) {
index = i;
break;
}
}
decryptedBytes2 = Arrays.copyOfRange(decryptedBytes2, index, decryptedBytes2.length);
// 输出每一轮解密结果
System.out.println("Round 2: " + new BigInteger(1, decryptedBytes2).toString(16));
return decryptedBytes2;
}
private static BigInteger generateRandomNumber(int numBits) {
BigInteger r;
do {
r = new BigInteger(numBits, new Random());
} while (r.mod(TWO).equals(BigInteger.ZERO) || r.mod(THREE).equals(BigInteger.ZERO));
return r;
}
private static byte[] concat(byte[] a, byte[] b) {
byte[] result = new byte[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
}
```
在这个例子中,我们使用了RSA填充方案(RSAES-OAEP),并且进行了两轮加密和两轮解密,以确保RSA填充方案的安全性。在每一轮加密和解密完成后,我们都会输出结果,以便查看每一轮的加密和解密结果。
阅读全文
相关推荐















