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 ) ) );
}