Search in sources :

Example 6 with GCMParameterSpec

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;
}
Also used : Scanner(java.util.Scanner) NAEException(com.ingrian.security.nae.NAEException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NAECipher(com.ingrian.security.nae.NAECipher) GCMParameterSpec(com.ingrian.security.nae.GCMParameterSpec) NAEAESGCMCipher(com.ingrian.security.nae.NAEAESGCMCipher) InvalidKeyException(java.security.InvalidKeyException) NAEKey(com.ingrian.security.nae.NAEKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey)

Aggregations

GCMParameterSpec (com.ingrian.security.nae.GCMParameterSpec)6 NAECipher (com.ingrian.security.nae.NAECipher)5 NAEKey (com.ingrian.security.nae.NAEKey)5 NAEAESGCMCipher (com.ingrian.security.nae.NAEAESGCMCipher)3 NAESession (com.ingrian.security.nae.NAESession)3 NAEException (com.ingrian.security.nae.NAEException)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 Key (java.security.Key)2 PrivateKey (java.security.PrivateKey)2 PublicKey (java.security.PublicKey)2 Scanner (java.util.Scanner)2 Cipher (javax.crypto.Cipher)2 SecretKey (javax.crypto.SecretKey)2 IngrianProvider (com.ingrian.security.nae.IngrianProvider)1