use of com.ingrian.security.nae.GCMParameterSpec in project CipherTrust_Application_Protection by thalescpl-io.
the class CryptoTool method doDecryptGCM.
private static boolean doDecryptGCM(String keyName, String algName, byte[] iv, NAESession session, String authTagLength, String aad, String inFile, String outFile) throws Exception {
Key key = NAEKey.getSecretKey(keyName, session);
boolean isAADSpecified = false;
if ((null != aad) && !EMPTYSTRING.equals(aad)) {
isAADSpecified = true;
}
Integer authtaglength = Integer.parseInt(authTagLength);
if (authtaglength == null) {
System.err.println("Unknown AuthTagLength");
}
NAECipher cipher = NAECipher.getNAECipherInstance("AES/GCM/NoPadding", "IngrianProvider");
if (isAADSpecified) {
byte[] aadBytes = IngrianProvider.hex2ByteArray(aad);
GCMParameterSpec gcmSpec = new GCMParameterSpec(authtaglength.intValue(), iv, aadBytes);
try {
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec);
} catch (InvalidAlgorithmParameterException e) {
throw new NAEException("Decrypt: failed - " + e.getMessage());
// e.printStackTrace();
} catch (InvalidKeyException e) {
throw e;
}
} else {
try {
GCMParameterSpec gcmSpec = new GCMParameterSpec(authtaglength.intValue(), iv);
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec);
} catch (InvalidKeyException e) {
throw e;
} catch (InvalidAlgorithmParameterException e) {
throw e;
}
}
inputscanner = new Scanner(is);
String result = inputscanner.nextLine();
if (inFile != null && outFile != null) {
NAEAESGCMCipher gcm = cipher.get_spi();
gcm.update(inFile, outFile, 1024, cipher);
} else {
while (EMPTYSTRING.equals(result.trim())) result = inputscanner.hasNext() ? inputscanner.nextLine() : null;
os.write(cipher.doFinal(IngrianProvider.hex2ByteArray(result)));
}
return true;
}
Aggregations