Search in sources :

Example 1 with AbstractNAECipher

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

the class BulkOperationSample method main.

public static void main(String[] args) {
    if (args.length != 4) {
        System.out.println("Usage: java BulkOperationSample <username>" + " <password>" + " <keyname> <datafile>");
        System.exit(-1);
    }
    String userName = args[0];
    String password = args[1];
    String keyName = args[2];
    String fileName = args[3];
    NAESession session = null;
    try {
        // Getting session and key
        session = NAESession.getSession(userName, password.toCharArray());
        NAEKey key = NAEKey.getSecretKey(keyName, session);
        // Getting instance for the bulk operation. Should be called
        // whenever bulk operation needs to be performed.
        AbstractNAECipher encryptCipher = NAECipher.getInstanceForBulkData("AES/GCM/NoPadding", "IngrianProvider");
        // read the contents from the file and write into the arrays
        readContentsFromFileAndWriteToArrays(fileName);
        // initializing the cipher for encrypt operation
        encryptCipher.init(Cipher.ENCRYPT_MODE, key, spec[0]);
        // Map to store exceptions while encryption
        Map<Integer, String> encryptedErrorMap = new HashMap<Integer, String>();
        // performing bulk operation
        byte[][] encryptedData = encryptCipher.doFinalBulk(data, spec, encryptedErrorMap);
        // displaying the encrypted data
        displayData(encryptedData, "Encrypted data");
        // cipher instance for decryption
        AbstractNAECipher decryptCipher = NAECipher.getInstanceForBulkData("AES/GCM/NoPadding", "IngrianProvider");
        // initializing the cipher for decrypt operation
        decryptCipher.init(Cipher.DECRYPT_MODE, key, spec[0]);
        // Map to store exceptions while decryption
        Map<Integer, String> decryptedErrorMap = new HashMap<Integer, String>();
        // performing bulk operation
        byte[][] decryptedData = decryptCipher.doFinalBulk(encryptedData, spec, decryptedErrorMap);
        // displaying the decrypted data
        displayData(decryptedData, "Decrypted Data ");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // releasing session
        if (session != null) {
            session.closeSession();
        }
    }
}
Also used : NAEKey(com.ingrian.security.nae.NAEKey) AbstractNAECipher(com.ingrian.security.nae.AbstractNAECipher) HashMap(java.util.HashMap) NAESession(com.ingrian.security.nae.NAESession) IOException(java.io.IOException)

Aggregations

AbstractNAECipher (com.ingrian.security.nae.AbstractNAECipher)1 NAEKey (com.ingrian.security.nae.NAEKey)1 NAESession (com.ingrian.security.nae.NAESession)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1