Encrypt and Decrypt data with Meteor and Java

QuestionsCategory: QuestionsEncrypt and Decrypt data with Meteor and Java
Brightn-n-Fresh Staff asked 1 year ago

I have my application's UI built in Meteor and it gets and send the data from REST API (Spring CXF). I would like to encrypt the data in Meteor, and decrypt the same in REST API code. I am using AES for encryption and Decryption. In Meteor i am using https://atmospherejs.com/jparker/crypto-aes package for encryption. I have written the below code in java for decryption the encryption key send by Meteor.

    public class AESTest {
           private static String AESStr = "<Encrypted KEY>";
           public static void main(String[] args) throws Exception {
                    Security.addProvider(new    org.bouncycastle.jce.provider.BouncyCastleProvider());
                System.out.println(decrypt(AESStr, "Test"));
           }
        public static String decrypt(String responseStr, String passPhrase) throws        GeneralSecurityException {
             String decryptedStr = "";
             try {
                        Cipher cipher = getCipher(Cipher.DECRYPT_MODE, passPhrase);
                        byte[] decoded = Base64.decodeBase64(responseStr.getBytes());
                        byte[] decryptedWithKey = cipher.doFinal(decoded);
                        byte[] decrypted = Arrays.copyOfRange(decryptedWithKey, 16, decryptedWithKey.length);
                        decryptedStr = new String(decrypted, "UTF-8");
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return decryptedStr;
        }

        private static Cipher getCipher(int mode, String passPhrase) throws Exception {
                SecretKeySpec secretKeySpec = new SecretKeySpec(passPhrase.getBytes(), "AES");
                byte[] IV = new byte[16];
                new Random().nextBytes(IV);
                AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV);
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
                cipher.init(mode, secretKeySpec, paramSpec);
                return cipher;
        }
}

When I run the code i am getting below exception

javax.crypto.BadPaddingException: pad block corrupted
    at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(Unknown Source)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)
    at com.tph.r3.EncodeTest.decrypt(EncodeTest.java:37)
    at com.tph.r3.EncodeTest.main(EncodeTest.java:26)

Can anyone guide me with the issue?

(146)