gpt4 book ai didi

java - RSA加密和解密产生一些错误字符

转载 作者:行者123 更新时间:2023-12-01 10:48:30 26 4
gpt4 key购买 nike

这里的代码属于我的安全作业。我正在尝试使用 RSAEncrypt 函数加密消息,并使用 RSADecrypt 函数按字符解密消息。我的字母表数组有 26 个元素。

例如:

RSAEncryption(lorem) -> Ciphertext = ?????
RSADecryption(?????) -> Plaintext = lorem

代码在这里:

static char alphabet[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

public static String RSAEncrypt() {

int[] keys;
String message, cipherText = "";
int p, q, n, x, e, d, index = 0;

System.out.println("Enter two prime integers: ");
p = sc.nextInt();
q = sc.nextInt();

n = p * q; //Modulus
x = (p - 1) * (q - 1); //φ(n)

if (n < 26) {
System.out.print("Please enter two prime numbers that their multiplication "
+ "is bigger than 26(Alphabet Lenght)!");
} else {
System.out.println("Enter a message to encrypt: ");
message = sc.next();

keys = RSAKeyGeneration(x);
e = keys[0]; //Public Key
d = keys[1]; //Private Key

for (int i = 0; i < message.length(); i++) {
char character = message.charAt(i);
for (int j = 0; j < alphabet.length; j++) {
if (character == alphabet[j]) {
index = j;
break;
}
}

double cipherTextDouble = (Math.pow(index, e) % n);
cipherText += alphabet[(int) cipherTextDouble % 26];
}

System.out.print("Original Message = " + message + ", Modulus = " + n + ", Public Key = " + e
+ ", Private Key = " + d + ", Ciphertext = ");

return cipherText;
}
return "";
}

public static String RSADecrypt() {

String cipherText, plainText = "";
int d, n;

System.out.println("Enter the encrypted message: ");
cipherText = sc.next();

System.out.println("Enter the private key(d and n(modulus)): ");
d = sc.nextInt();
n = sc.nextInt();

for (int i = 0; i < cipherText.length(); i++) {
for (int j = 0; j < 26; j++) {
if (cipherText.charAt(i) == alphabet[j]) {
int temp = 1;
for (int z = 0; z < d; z++) {
temp *= j;
temp = (temp % n);
}
plainText += alphabet[(temp % 26)];
}
}
}
return plainText;
}

public static int[] RSAKeyGeneration(int x) {
int[] keys = new int[2];

for (int i = x; i > 0; i--) {
if (i % x != 0 && x % i != 0) {
keys[0] = i; //Public Key
}
}

keys[1] = findInverse(keys[0], x); //Private Key

return keys;
}

问题是当我给出素数 5 和 7 (Mod = 35, Totient = 24, e = 5, d = 5) 时,它给出了错误的明文。

RSAEncryption(abcdefghijklmnopqrstuvwxyz) -> Ciphertext = abghjkghiefqrnoplmxyuvwste
RSADecryption(abghjkghiefqrnoplmxyuvwste) -> Plaintext = abghefghijklmnopqrstuvwxyj

为什么“c”、“d”、“z”字符给我错误的输出。另外,当我给出更大的素数时,输出是完全错误的。我哪里做错了?

最佳答案

RSA 加密是对 n 取模的幂。您的 n 是 35,但问题是您随后尝试使用以下行将 0 到 34 范围内的密文转换为 0 到 25 范围内的密文:

cipherText += alphabet[(int) cipherTextDouble % 26];

这意味着大约 25% ((35-26)/35) 的密文字符将不正确。您需要将字母表增加到 35 个条目并使用

cipherText += alphabet[(int) cipherTextDouble];

此外,当您尝试增加素数时,您可能会很快遇到用 double 表示整数的精度问题。您必须切换到 BigInteger 类。

关于java - RSA加密和解密产生一些错误字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34047899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com