0

I am getting this error while decrypting the data using AES.

I have also checked for bytes conversion might be losing some data but nothing helped.

The key is decrypted successfully.

But when the decryptfile function is called it throws the subjected exception.

Please help.

ENCRYPTION CODE

GenerateSymmetricKey symmetricKey = new GenerateSymmetricKey( 16, "AES" );
String encryptedKey = RSAEncryptPublic( symmetricKey.getKey().getEncoded(), receiverPublicKey );
String encryptedPaymentFile = AESEncryptFile( filePaymentRecord.getBytes("UTF-8") , symmetricKey.getKey() );

ENCRYPTION FUNCTIONS

public static String RSAEncryptPublic ( byte[] data, PublicKey publicKey ) {

        Cipher cipher = null;
        byte[] encryptedData = null;

        try {
            cipher = Cipher.getInstance( "RSA/ECB/PKCS1Padding" );

            cipher.init(Cipher.ENCRYPT_MODE, publicKey);

            encryptedData = cipher.doFinal( data );

        }
        catch ( Exception e ) {
            log.println( LOG + " Exception in Encryption of Key = " + e  );
        }

        return Base64.getEncoder().encodeToString( encryptedData );

    }

public static String AESEncryptFile( byte[] input, SecretKeySpec key ) throws IOException, GeneralSecurityException {

        Cipher cipher = Cipher.getInstance( "AES/ECB/PKCS5Padding" );

        cipher.init( Cipher.ENCRYPT_MODE, key );

        return Base64.getEncoder().encodeToString( cipher.doFinal( input ) );
    }

DECRYPTION CODE

byte[] data3 = getFileInBytes( new File ( path + "files\\key_01704346803_IFT_JAM01_UBL_Export_File_1_15042020_193115-0018.csv") ); // sign

String key = RSADecryptKey( data3, receiverPrivateKey );
SecretKeySpec aesKey = new SecretKeySpec( key.getBytes(), "AES" );          
String paymentFile = decryptFile( data4, aesKey );

DECRYPT FUNCTIONS

public static String RSADecryptKey( byte[] data, PrivateKey privateKey ) {

        Cipher cipher = null;
        String decryptedData = "";

        try {
            cipher = Cipher.getInstance( "RSA/ECB/PKCS1Padding" );

            cipher.init( Cipher.DECRYPT_MODE, privateKey );

            byte[] decodedData = Base64.getDecoder().decode( data );

            decryptedData = new String( cipher.doFinal( decodedData ), "UTF-8" );
        }
        catch ( Exception e ) {

            System.out.println(  " Exception in Decryption -> " + e );

        }
        return decryptedData;
    }

public String decryptFile(byte[] input, SecretKeySpec key) throws IOException, GeneralSecurityException { 

        Cipher cipher = Cipher.getInstance( "AES/ECB/PKCS5Padding" );

        cipher.init(Cipher.DECRYPT_MODE, key);

        return new String( cipher.doFinal( Base64.getDecoder().decode( input ) ) );
    }
Jawad-Dev
  • 274
  • 10
  • 31
  • `RSADecryptKey` tries to convert the decrypted key into a UTF8 string. This does not work. Either the decrypted data are returned as `byte[]` or in a suitable encoding like Base64. In `decryptFile` this happens again. Here it could work if the encrypted data are text data. But at least an encoding should be specified. – Topaco Apr 15 '20 at 15:43
  • I have removed the UTF-8 encode from ```RSADecryptKey```. But I didnt get your point for ```decryptFile```. Can you please specify in the code? – Jawad-Dev Apr 15 '20 at 15:56
  • However, I have removed UTF-8 encoding from both decryption methods, but the problem remains the same :( – Jawad-Dev Apr 15 '20 at 15:57
  • 1
    See [here](https://stackoverflow.com/a/9098905) for storing arbitrary bytes in strings. Also, post your changes in an update section. – Topaco Apr 15 '20 at 16:22

0 Answers0