Search in sources :

Example 6 with NAESecureRandom

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

the class FileEncryptionSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 6) {
        System.err.println("Usage: java FileEncryptionSample user password keyname fileToEncrypt encryptedFile decryptedFile");
        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];
    // how many bytes of data to read from the input stream - can be any size
    int BUFSIZE = 512;
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    // get the list of all registered JCE providers
    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
        System.out.println(provider.getInfo());
    }
    // create NAE Session: pass in Key Manager user name and password
    NAESession session = null;
    try {
        session = NAESession.getSession(username, password.toCharArray());
        // Get SecretKey (just a handle to it, key data does not leave the Key Manager
        NAEKey key = NAEKey.getSecretKey(keyName, session);
        // get IV
        NAESecureRandom rng = new NAESecureRandom(session);
        byte[] iv = new byte[16];
        rng.nextBytes(iv);
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        // get a cipher
        Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "IngrianProvider");
        // initialize cipher to encrypt.
        encryptCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
        // create CipherInputStream that will read in data from file and encrypt it
        CipherInputStream cis = new CipherInputStream(new FileInputStream(srcName), encryptCipher);
        FileOutputStream fos = new FileOutputStream(dstName);
        // Read the file as blocks of data
        byte[] inbuf = new byte[BUFSIZE];
        for (int inlen = 0; (inlen = cis.read(inbuf)) != -1; ) {
            fos.write(inbuf, 0, inlen);
        }
        System.out.println("Done encrypting file.  Closing files");
        cis.close();
        fos.close();
        Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "IngrianProvider");
        // initialize cipher to decrypt.
        decryptCipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
        // create CipherInputStream that will read in data from file and decrypt it
        cis = new CipherInputStream(new FileInputStream(dstName), decryptCipher);
        fos = new FileOutputStream(decrName);
        for (int inlen = 0; (inlen = cis.read(inbuf)) != -1; ) {
            fos.write(inbuf, 0, inlen);
        }
        System.out.println("Done decrypting file.  Closing files");
        cis.close();
        fos.close();
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        throw e;
    } finally {
        if (session != null) {
            session.closeSession();
        }
    }
}
Also used : NAEKey(com.ingrian.security.nae.NAEKey) NAESecureRandom(com.ingrian.security.nae.NAESecureRandom) CipherInputStream(javax.crypto.CipherInputStream) FileInputStream(java.io.FileInputStream) IngrianProvider(com.ingrian.security.nae.IngrianProvider) IngrianProvider(com.ingrian.security.nae.IngrianProvider) Provider(java.security.Provider) FileOutputStream(java.io.FileOutputStream) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NAESession(com.ingrian.security.nae.NAESession)

Aggregations

IngrianProvider (com.ingrian.security.nae.IngrianProvider)6 NAESecureRandom (com.ingrian.security.nae.NAESecureRandom)6 NAESession (com.ingrian.security.nae.NAESession)6 NAEKey (com.ingrian.security.nae.NAEKey)5 Cipher (javax.crypto.Cipher)5 IvParameterSpec (javax.crypto.spec.IvParameterSpec)5 Provider (java.security.Provider)4 NAEParameterSpec (com.ingrian.security.nae.NAEParameterSpec)2 NAESecretKey (com.ingrian.security.nae.NAESecretKey)2 KeyGenerator (javax.crypto.KeyGenerator)2 SecretKey (javax.crypto.SecretKey)2 CustomAttributes (com.ingrian.security.nae.CustomAttributes)1 KMIPAttributes (com.ingrian.security.nae.KMIPAttributes)1 KMIPSession (com.ingrian.security.nae.KMIPSession)1 KeyExportData (com.ingrian.security.nae.KeyExportData)1 KeyInfoData (com.ingrian.security.nae.KeyInfoData)1 NAEClientCertificate (com.ingrian.security.nae.NAEClientCertificate)1 NAEException (com.ingrian.security.nae.NAEException)1 NAEIvAndTweakDataParameter (com.ingrian.security.nae.NAEIvAndTweakDataParameter)1 NAEPermission (com.ingrian.security.nae.NAEPermission)1