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

时间:2015-12-02 16:29:49

标签: java encryption rsa

这里的代码属于我的安全作业。我正在尝试使用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'字符给我错误的输出。此外,当我给素数更大时,输出完全错误。我在哪里做错了?

1 个答案:

答案 0 :(得分:1)

RSA加密是取幂模n。你的n是35,但问题是你试着用这一行将0到34范围内的密文转换成0到25范围内的密文:

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

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

cipherText += alphabet[(int) cipherTextDouble];

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