Search in sources :

Example 36 with NAESession

use of com.ingrian.security.nae.NAESession 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)

Example 37 with NAESession

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

the class HMACSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        System.err.println("Usage: java HMACSample user password hmacKeyName");
        System.exit(-1);
    }
    String username = args[0];
    String password = args[1];
    String keyName = args[2];
    // 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 (int i = 0; i < providers.length; i++) System.out.println(providers[i].getInfo());
    String dataToMac = "2D2D2D2D2D424547494E2050455253495354454E54204346EB17960";
    System.out.println("Data to mac \"" + dataToMac + "\"");
    NAESession session = null;
    try {
        // create HMAC key on the server
        // create NAE Session: pass in Key Manager user name and password
        session = NAESession.getSession(username, password.toCharArray());
        // create key which is exportable and deletable,
        // key owner is passed in Key Manager user.
        // For HmacSHA1 key length 160 bits
        // For HmacSHA256 key length is 256 bits
        // For HmacSHA384 key length is 384 bits
        // For HmacSHA512 key length is 512 bits
        NAEParameterSpec spec = new NAEParameterSpec(keyName, true, true, 160, session);
        KeyGenerator kg = KeyGenerator.getInstance("HmacSHA1", "IngrianProvider");
        kg.init(spec);
        SecretKey secret_key = kg.generateKey();
        // get the handle to created key
        NAEKey key = NAEKey.getSecretKey(keyName, session);
        // create MAC instance to get the message authentication code
        Mac mac = Mac.getInstance("HmacSHA1", "IngrianProvider");
        mac.init(key);
        byte[] macValue = mac.doFinal(dataToMac.getBytes());
        // create MAC instance to verify the message authentication code
        Mac macV = Mac.getInstance("HmacSHA1Verify", "IngrianProvider");
        macV.init(key, new MACValue(macValue));
        byte[] result = macV.doFinal(dataToMac.getBytes());
        // check verification result
        if (result.length != 1 || result[0] != 1) {
            System.out.println("Invalid MAC.");
        } else {
            System.out.println("MAC Verified OK.");
        }
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        throw e;
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : NAEParameterSpec(com.ingrian.security.nae.NAEParameterSpec) NAEKey(com.ingrian.security.nae.NAEKey) Mac(javax.crypto.Mac) IngrianProvider(com.ingrian.security.nae.IngrianProvider) IngrianProvider(com.ingrian.security.nae.IngrianProvider) Provider(java.security.Provider) MACValue(com.ingrian.security.nae.MACValue) SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator) NAESession(com.ingrian.security.nae.NAESession)

Example 38 with NAESession

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

the class MultiThreadSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        System.err.println("Usage: java MultiThreadSample user password keyname");
        System.exit(-1);
    }
    String username = args[0];
    String password = args[1];
    String keyName = args[2];
    // this sample will create 5 threads
    int threadCount = 5;
    // 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 (int i = 0; i < providers.length; i++) System.out.println(providers[i].getInfo());
    MultiThreadSample[] list = new MultiThreadSample[threadCount];
    NAESession session = null;
    try {
        // create NAE Session: pass in Key Manager user name and password
        session = NAESession.getSession(username, password.toCharArray());
        // get the key
        SecretKey key = NAEKey.getSecretKey(keyName, session);
        for (int i = 0; i < threadCount; i++) {
            list[i] = new MultiThreadSample(key);
        }
        for (int i = 0; i < threadCount; i++) {
            list[i].start();
        }
        // wait for all threads to finish before closing sesson.
        for (int i = 0; i < threadCount; i++) {
            list[i].join();
        }
        session.closeSession();
    } catch (Exception e) {
        System.out.println("Got exception: " + e);
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : SecretKey(javax.crypto.SecretKey) NAESession(com.ingrian.security.nae.NAESession) IngrianProvider(com.ingrian.security.nae.IngrianProvider) IngrianProvider(com.ingrian.security.nae.IngrianProvider) Provider(java.security.Provider)

Example 39 with NAESession

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

the class FileEncryptionSampleUsingARIA method main.

public static void main(String[] args) {
    if (args.length != 8) {
        System.err.println("Usage: java FileEncryptionSampleUsingARIA user password keyname fileToEncrypt " + "encryptedFile decryptedFile iv 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];
    String iv = args[6];
    int blockSize = Integer.parseInt(args[7]);
    byte[] ivBytes = iv.getBytes();
    System.out.println("iv: " + IngrianProvider.byteArray2Hex(ivBytes));
    String Algo = "ARIA/CBC/PKCS5Padding";
    Security.addProvider(new IngrianProvider());
    NAESession session = null;
    try {
        session = NAESession.getSession(username, password.toCharArray());
        NAEKey key = NAEKey.getSecretKey(keyName, session);
        // IvParameterSpec ivSpec = new
        // IvParameterSpec(IngrianProvider.hex2ByteArray(iv));
        NAECipher cipher = NAECipher.getNAECipherInstance(Algo, "IngrianProvider");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        NAEARIACipher aria = cipher.get_aria();
        aria.update(srcName, dstName, blockSize, cipher);
        cipher.init(Cipher.DECRYPT_MODE, key);
        aria = cipher.get_aria();
        aria.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) NAESession(com.ingrian.security.nae.NAESession) NAEARIACipher(com.ingrian.security.nae.NAEARIACipher) IngrianProvider(com.ingrian.security.nae.IngrianProvider)

Example 40 with NAESession

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

the class MultiplePropertyFileSample method main.

public static void main(String[] args) {
    if (args.length != 6) {
        System.err.println("Usage: java MultiplePropertyFileSample local_config_user local_config_password " + "local_propertyfile_path global_config_user global_config_password keyname");
        System.exit(-1);
    }
    NAESession localsession = null;
    NAESession globalsession = null;
    NAESecretKey localsessionKey = null;
    NAEKey globalsessionKey = null;
    String data = "Test Data";
    try {
        localsession = NAESession.getSession(args[0], args[1].toCharArray(), new SessionLevelConfig(args[2]));
        globalsession = NAESession.getSession(args[3], args[4].toCharArray());
        NAEParameterSpec spec = new NAEParameterSpec(args[5], true, true, false, 192, null, localsession);
        localsessionKey = generateKey(spec);
        boolean isExported = exportKeyToGlobalSession(globalsession, localsessionKey);
        if (isExported) {
            byte[] encrytedText = encryptWithLocalConfig(data, localsessionKey);
            globalsessionKey = NAEKey.getSecretKey(localsessionKey.getName(), globalsession);
            byte[] decryptText = decryptWithGLobalConfig(encrytedText, globalsessionKey);
            if (data.equals(new String(decryptText))) {
                System.out.println("Key  is exported successfully to global Key Manager.");
            } else {
                System.out.println("Key is not exported successfully to global Key Manager.");
            }
        } else {
            System.out.println("Key is not exported successfully to global Key Manager.");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (localsessionKey != null)
            localsessionKey.delete();
        if (globalsessionKey != null)
            globalsessionKey.delete();
    }
}
Also used : NAEParameterSpec(com.ingrian.security.nae.NAEParameterSpec) NAEKey(com.ingrian.security.nae.NAEKey) NAESecretKey(com.ingrian.security.nae.NAESecretKey) SessionLevelConfig(com.ingrian.security.nae.SessionLevelConfig) NAESession(com.ingrian.security.nae.NAESession) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

NAESession (com.ingrian.security.nae.NAESession)40 IngrianProvider (com.ingrian.security.nae.IngrianProvider)27 NAEKey (com.ingrian.security.nae.NAEKey)20 Provider (java.security.Provider)19 Cipher (javax.crypto.Cipher)14 NAEParameterSpec (com.ingrian.security.nae.NAEParameterSpec)11 NAEPublicKey (com.ingrian.security.nae.NAEPublicKey)10 NAEPrivateKey (com.ingrian.security.nae.NAEPrivateKey)9 KeyGenerator (javax.crypto.KeyGenerator)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)8 NAESecureRandom (com.ingrian.security.nae.NAESecureRandom)6 IOException (java.io.IOException)6 SecretKey (javax.crypto.SecretKey)6 NAECipher (com.ingrian.security.nae.NAECipher)5 NAEPermission (com.ingrian.security.nae.NAEPermission)5 NAESecretKey (com.ingrian.security.nae.NAESecretKey)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 GCMParameterSpec (com.ingrian.security.nae.GCMParameterSpec)3 NAEException (com.ingrian.security.nae.NAEException)3