This code here belongs to my security homework. I'm trying to encrypt a message with the RSAEncrypt function and decrypt a message with the RSADecrypt function character-wise. My alphabet array has 26 elements.
For example:
RSAEncryption(lorem) -> Ciphertext = ?????
RSADecryption(?????) -> Plaintext = lorem
Code is here:
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;
}
The Problem is when I give the prime numbers 5 and 7 (Mod = 35, Totient = 24, e = 5, d = 5) It gives me wrong plaintext.
RSAEncryption(abcdefghijklmnopqrstuvwxyz) -> Ciphertext = abghjkghiefqrnoplmxyuvwste
RSADecryption(abghjkghiefqrnoplmxyuvwste) -> Plaintext = abghefghijklmnopqrstuvwxyj
Why 'c', 'd', 'z' characters are giving me wrong output. Also when I give prime numbers bigger the output is totally wrong. Where am I doing wrong?
 
    