I'm trying to use AES/GCM/NoPadding within the Java code for decryption. Below is the code
public static byte[] decryptRes(final byte[] sessionKey, final String symetricKeyAlg,
final String pkiProvider, final String encXML, final String msgRefNo)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchProviderException, InvalidAlgorithmParameterException {
Security.addProvider(new BouncyCastleProvider());
final StringBuilder strBuilder = new StringBuilder(msgRefNo);
strBuilder.append("0000");
final SecretKeySpec symmKeySpec = new SecretKeySpec(sessionKey, "AES");
final Cipher symmCipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
symmCipher.init(2, symmKeySpec, new IvParameterSpec(strBuilder.toString().getBytes()));
final byte[] xmlData = symmCipher.doFinal(base64Decode(encXML));
return xmlData;
}
Now, when this code is invoked, it throws
Exception : java.security.NoSuchAlgorithmException: No such algorithm: AES/GCM/NoPadding
and not
NoSuchProviderException.
The necessary providers is already added but I still get the above mentioned exception.
Java version 7 being used.
Jars Used :
bcprov-jdk15to18-169.jar
bcprov-ext-jdk15to18-169.jar
The above jars are available in the application lib folder.
I could finally resolve the above mentioned issue. There is absolutely no issue with the code provided. However, I was facing the issue because of the BouncyCastle jar not registered as Service Provider. Updated the java.security file under
>$JAVAHOME/lib/security/java.security
Add the below line if the provided is not added
security.provider.<#>=org.bouncycastle.jce.provider.BouncyCastleProvider
Follow the link for details :
https://makeinjava.com/install-bouncy-castle-provider-configuring-java-runtime-example/