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

RSA encryption and decryption produces some wrong characters

这里的这段代码属于我的安全作业。我正在尝试使用 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];

此外,当您尝试增加素数时,您可能 运行 很快就会陷入用双精度表示整数的精度问题。您将不得不切换到 BigInteger class.