Search in sources :

Example 1 with GCMParameterSpec

use of com.ingrian.security.nae.GCMParameterSpec in project CipherTrust_Application_Protection by thalescpl-io.

the class AESGCMUpdateSample method main.

public static void main(String[] args) {
    if (args.length != 7) {
        System.err.println("Usage: java AESGCMUpdateSample user password keyname " + "authTagLength iv aad data");
        System.exit(-1);
    }
    String username = args[0];
    String password = args[1];
    String keyName = args[2];
    int authTagLength = Integer.parseInt(args[3]);
    String iv = args[4];
    String aad = args[5];
    String data = args[6];
    /**
     * Note: For AES-GCM algorithm, same combination of nonce (IV) and key must not be reused
     * during encryption/decryption operations.
     */
    byte[] ivBytes = IngrianProvider.hex2ByteArray(iv);
    byte[] aadBytes = IngrianProvider.hex2ByteArray(aad);
    byte[] dataBytes = data.getBytes();
    System.out.println("iv: " + IngrianProvider.byteArray2Hex(ivBytes));
    System.out.println("AAD: " + IngrianProvider.byteArray2Hex(aadBytes));
    NAESession session = null;
    try {
        session = NAESession.getSession(username, password.toCharArray(), "hello".toCharArray());
        NAEKey key = NAEKey.getSecretKey(keyName, session);
        GCMParameterSpec encSpec = new GCMParameterSpec(authTagLength, ivBytes, aadBytes);
        Cipher encryptCipher = NAECipher.getNAECipherInstance("AES/GCM/NoPadding", "IngrianProvider");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key, encSpec);
        byte[] encryptdoFinal = null, encryptUpdate = null, encryptedText;
        encryptUpdate = encryptCipher.update(dataBytes);
        encryptdoFinal = encryptCipher.doFinal();
        if (encryptUpdate == null)
            encryptedText = encryptdoFinal;
        else
            encryptedText = ArrayUtils.addAll(encryptUpdate, encryptdoFinal);
        System.out.println("Encrypt: " + IngrianProvider.byteArray2Hex(encryptedText));
        GCMParameterSpec decSpec = new GCMParameterSpec(authTagLength, ivBytes, aadBytes);
        decSpec.setAuthTag(encSpec.getAuthTag());
        byte[] decryptdoFinal = null, decryptUpdate = null, decryptedText;
        Cipher decryptCipher = NAECipher.getNAECipherInstance("AES/GCM/NoPadding", "IngrianProvider");
        decryptCipher.init(Cipher.DECRYPT_MODE, key, decSpec);
        decryptUpdate = decryptCipher.update(encryptedText);
        decryptdoFinal = decryptCipher.doFinal();
        if (decryptUpdate == null)
            decryptedText = decryptdoFinal;
        else
            decryptedText = ArrayUtils.addAll(decryptUpdate, decryptdoFinal);
        System.out.println("data: " + new String(decryptedText));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // releasing session
        if (session != null) {
            session.closeSession();
        }
    }
}
Also used : NAEKey(com.ingrian.security.nae.NAEKey) GCMParameterSpec(com.ingrian.security.nae.GCMParameterSpec) NAECipher(com.ingrian.security.nae.NAECipher) Cipher(javax.crypto.Cipher) NAESession(com.ingrian.security.nae.NAESession)

Example 2 with GCMParameterSpec

use of com.ingrian.security.nae.GCMParameterSpec in project CipherTrust_Application_Protection by thalescpl-io.

the class FileEncryptionSampleUsingGCM method main.

public static void main(String[] args) {
    if (args.length != 10) {
        System.err.println("Usage: java FileEncryptionSampleUsingGCM user password keyname fileToEncrypt " + "encryptedFile decryptedFile authTagLength iv aad blockSize");
        System.exit(-1);
    }
    String username = args[0];
    String password = args[1];
    String keyName = args[2];
    String srcName = args[3];
    String dstName = args[4];
    String decrName = args[5];
    int authTagLength = Integer.parseInt(args[6]);
    String iv = args[7];
    String aad = args[8];
    int blockSize = Integer.parseInt(args[9]);
    /**
     * Note: For AES-GCM algorithm, same combination of nonce (IV) and key must not be reused
     * during encryption/decryption operations.
     */
    byte[] ivBytes = iv.getBytes();
    byte[] aadBytes = aad.getBytes();
    System.out.println("iv: " + IngrianProvider.byteArray2Hex(ivBytes));
    System.out.println("AAD: " + IngrianProvider.byteArray2Hex(aadBytes));
    Security.addProvider(new IngrianProvider());
    NAESession session = null;
    try {
        session = NAESession.getSession(username, password.toCharArray());
        NAEKey key = NAEKey.getSecretKey(keyName, session);
        GCMParameterSpec spec = new GCMParameterSpec(authTagLength, ivBytes, aadBytes);
        NAECipher cipher = NAECipher.getNAECipherInstance("AES/GCM/NoPadding", "IngrianProvider");
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        NAEAESGCMCipher gcm = cipher.get_spi();
        gcm.update(srcName, dstName, blockSize, cipher);
        cipher.init(Cipher.DECRYPT_MODE, key, spec);
        gcm = cipher.get_spi();
        gcm.update(dstName, decrName, blockSize, cipher);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.closeSession();
        }
    }
}
Also used : NAEKey(com.ingrian.security.nae.NAEKey) NAECipher(com.ingrian.security.nae.NAECipher) GCMParameterSpec(com.ingrian.security.nae.GCMParameterSpec) NAEAESGCMCipher(com.ingrian.security.nae.NAEAESGCMCipher) NAESession(com.ingrian.security.nae.NAESession) IngrianProvider(com.ingrian.security.nae.IngrianProvider)

Example 3 with GCMParameterSpec

use of com.ingrian.security.nae.GCMParameterSpec in project CipherTrust_Application_Protection by thalescpl-io.

the class AESGCMEncryptionDecryptionSample method main.

public static void main(String[] args) {
    if (args.length != 7) {
        System.err.println("Usage: java AESGCMEncryptionDecryptionSample user password keyname " + "authTagLength iv aad data");
        System.exit(-1);
    }
    String username = args[0];
    String password = args[1];
    String keyName = args[2];
    int authTagLength = Integer.parseInt(args[3]);
    String iv = args[4];
    String aad = args[5];
    String data = args[6];
    /**
     * Note: For AES-GCM algorithm, same combination of nonce (IV) and key must not be reused
     * during encryption/decryption operations.
     */
    byte[] ivBytes = IngrianProvider.hex2ByteArray(iv);
    byte[] aadBytes = IngrianProvider.hex2ByteArray(aad);
    byte[] dataBytes = data.getBytes();
    System.out.println("iv: " + IngrianProvider.byteArray2Hex(ivBytes));
    System.out.println("AAD: " + IngrianProvider.byteArray2Hex(aadBytes));
    NAESession session = null;
    try {
        session = NAESession.getSession(username, password.toCharArray(), "hello".toCharArray());
        NAEKey key = NAEKey.getSecretKey(keyName, session);
        GCMParameterSpec spec = new GCMParameterSpec(authTagLength, ivBytes, aadBytes);
        Cipher encryptCipher = NAECipher.getNAECipherInstance("AES/GCM/NoPadding", "IngrianProvider");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] encrypt = null;
        encrypt = encryptCipher.doFinal(dataBytes);
        System.out.println("Encrypt: " + IngrianProvider.byteArray2Hex(encrypt));
        Cipher decryptCipher = NAECipher.getNAECipherInstance("AES/GCM/NoPadding", "IngrianProvider");
        decryptCipher.init(Cipher.DECRYPT_MODE, key, spec);
        byte[] decrypt = decryptCipher.doFinal(encrypt);
        System.out.println("data: " + new String(decrypt));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // releasing session
        if (session != null) {
            session.closeSession();
        }
    }
}
Also used : NAEKey(com.ingrian.security.nae.NAEKey) GCMParameterSpec(com.ingrian.security.nae.GCMParameterSpec) NAECipher(com.ingrian.security.nae.NAECipher) Cipher(javax.crypto.Cipher) NAESession(com.ingrian.security.nae.NAESession)

Example 4 with GCMParameterSpec

use of com.ingrian.security.nae.GCMParameterSpec in project CipherTrust_Application_Protection by thalescpl-io.

the class BulkOperationSample method WriteFileContentIntoArrays.

/**
 * This will write the respective data into the array objects.
 */
private static void WriteFileContentIntoArrays(RandomAccessFile file, byte[][] data, AlgorithmParameterSpec[] spec) throws NumberFormatException, IOException {
    int dataIndex = 0;
    int specIndex = 0;
    String line = null;
    while ((line = file.readLine()) != null) {
        if (line.length() == 0) {
            break;
        }
        String[] words = line.split(",");
        int tagLength = Integer.parseInt(words[1].trim());
        data[dataIndex++] = words[0].trim().getBytes();
        spec[specIndex++] = new GCMParameterSpec(tagLength, IngrianProvider.hex2ByteArray(words[2].trim()), words[3].trim().getBytes());
    }
}
Also used : GCMParameterSpec(com.ingrian.security.nae.GCMParameterSpec)

Example 5 with GCMParameterSpec

use of com.ingrian.security.nae.GCMParameterSpec in project CipherTrust_Application_Protection by thalescpl-io.

the class CryptoTool method doEncryptGCM.

private static boolean doEncryptGCM(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.ENCRYPT_MODE, key, gcmSpec);
        } catch (InvalidAlgorithmParameterException e) {
            throw new NAEException("Encrypt: failed - " + e.getMessage());
        } catch (InvalidKeyException e) {
            throw e;
        }
    } else {
        try {
            GCMParameterSpec gcmSpec = new GCMParameterSpec(authtaglength.intValue(), iv);
            cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec);
        } catch (InvalidKeyException e) {
            throw e;
        } catch (InvalidAlgorithmParameterException e) {
            throw e;
        }
    }
    String result = null;
    inputscanner = new Scanner(is);
    result = inputscanner.nextLine();
    if (inFile != null && outFile != null) {
        NAEAESGCMCipher gcm = cipher.get_spi();
        gcm.update(inFile, outFile, 1024, cipher);
    } else {
        while (EMPTYSTRING.equals(result)) result = inputscanner.hasNext() ? inputscanner.nextLine() : null;
        os.writeBytes(IngrianProvider.byteArray2Hex(cipher.doFinal(result.getBytes())));
    }
    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