Search code examples
javaencryptioncryptographyaesrsa

RSA with AES encryption and decryption


What is the problem of my decryption for RSA?

here is the code for encryption :

    try {
        //Get the public key from the keyStore and set up the Cipher object
        PublicKey publicKey = getPubKey(keyStore,keyName);
        Cipher rsaCipher = Cipher.getInstance("RSA");
        rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);

        //Read the plainText
        System.out.println("Loading plaintext file: "+inFile); 
        RandomAccessFile rawDataFromFile = new RandomAccessFile(inFile, "r");
        byte[] plainText = new byte[(int)rawDataFromFile.length()];
        rawDataFromFile.read(plainText);

        // Generate a symmetric key to encrypt the data and initiate the AES Cipher Object
        System.out.println("Generating AES key"); 
        KeyGenerator sKenGen = KeyGenerator.getInstance("AES"); 
        Key aesKey = sKenGen.generateKey();
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);

        // Encrypt the symmetric AES key with the public RSA key
        System.out.println("Encrypting Data"); 
        byte[] encodedKey = rsaCipher.doFinal(aesKey.getEncoded()); 
        // Encrypt the plaintext with the AES key
        byte[] cipherText = aesCipher.doFinal(plainText);

        //Write the encrypted AES key and Ciphertext to the file.
        System.out.println("Writting to file: "+outFile);
        FileOutputStream outToFile = new FileOutputStream(outFile);
        outToFile.write(encodedKey);
        outToFile.write(cipherText);

        System.out.println("Closing Files");
        rawDataFromFile.close();
        outToFile.close();
    }
    catch (Exception e) { 
        System.out.println("Doh: "+e); 
    }

and here is my code for decryption, i thought it will work quite well but it didnt. anyone can help me ?

it kept having the error : javax.crypto.BadPaddingException: Decryption error

dont really know what to do, anyone can give me some advices?

private static void decryptRSA() {
    try {
        System.out.println("Loading plaintext file: "+inFile); 
        RandomAccessFile rawDataFromFile = new RandomAccessFile(inFile, "r");
        byte[] cipherText = new byte[(int)rawDataFromFile.length()];
        byte encodedkey[] = new byte[256];
        rawDataFromFile.read(encodedkey, 0, 256);
        rawDataFromFile.read(cipherText);

        PublicKey publicKey = getPubKey(keyStore,keyName);
        Cipher rsaCipher = Cipher.getInstance("RSA");
        rsaCipher.init(Cipher.DECRYPT_MODE, publicKey);

        byte[] aeskey = rsaCipher.doFinal(encodedkey);
        SecretKeySpec aesKey = new SecretKeySpec(aeskey, "AES");
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, aesKey);

        byte[] plainText = aesCipher.doFinal(cipherText);

        System.out.println("Writting to file: "+outFile);
        FileOutputStream outToFile = new FileOutputStream(outFile);
        outToFile.write(plainText);
        System.out.println("Closing Files");
        rawDataFromFile.close();
        outToFile.close();
    }
    catch (Exception e) { 
        System.out.println("Doh: "+e); 
    }
}

Solution

    1. RSA decryption is done with the private key, not the public key.

    2. The length of the cipherText array in the decryption code isn't correct. You should subtract 256, or pass the actual read length to Cipher.doFinal(), or in fact both.

    NB Despite the message you're printing, your decrypt step is really reading from the ciphertext file, not the plaintext file.