I am trying to encrypt the message from FE (React Native) and send to BE to decrypt(Java Spring).
I use this Hybrid Crypto library to generate cipher text. My logic in FE is using RSA public key to create AES key, and then use this AES key to encrypt my message. I send to my BE encrypted AES key + iv + cipher text
But in Java I cannot read Iv parameterspec, it's always show this message
java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long
This is my Iv, it always returns the same length with different AES standards (AES-ECB, AES-CBC, AES-CFB, AES-OFB, AES-CTR)
merltoPCJMCEu0/yEaMpriIG9Xl4hP3W+h1iNJUndu0=
I did my own research, and I found that the Iv length in Java must always be 16 bytes
But when I decode my String, it always returns 32
System.out.println(Base64.getDecoder().decode("merltoPCJMCEu0/yEaMpriIG9Xl4hP3W+h1iNJUndu0=").length);
This is all of my decrypt code
Security.addProvider(new BouncyCastleProvider());
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = generatePrivateKey(factory, RESOURCES_DIR + "private-key.pem");
Cipher decryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] keyb = decryptCipher.doFinal(Base64.getDecoder().decode("My AES key"));
SecretKeySpec skey = new SecretKeySpec(keyb, "AES");
IvParameterSpec ivspec = new IvParameterSpec(Base64.getDecoder().decode("My IV string"));
Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.DECRYPT_MODE, skey, ivspec);
byte[] decryptOut = ci.doFinal(Base64.getDecoder().decode("My cipher text"));
System.out.println(new String(decryptOut, StandardCharsets.UTF_8));
I really don't know where am I missing...