Search in sources :

Example 41 with IngrianProvider

use of com.ingrian.security.nae.IngrianProvider 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 42 with IngrianProvider

use of com.ingrian.security.nae.IngrianProvider 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 43 with IngrianProvider

use of com.ingrian.security.nae.IngrianProvider 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 44 with IngrianProvider

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

the class KMIPQuerySample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        usage();
    }
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    KMIPSession session = null;
    try {
        session = KMIPSession.getSession(new NAEClientCertificate(args[0], args[1].toCharArray()));
        // create list of Key Manager properties to query
        ArrayList<Query> query = new ArrayList<Query>();
        query.add(Query.QueryObjects);
        query.add(Query.QueryOperations);
        query.add(Query.QueryServerInformation);
        /* execute the query on the session */
        Map<Query, ArrayList<String>> queryResult2 = session.query(query);
        /* view the results */
        for (Query answer : queryResult2.keySet()) {
            System.out.println(answer.getPrintName() + ": " + queryResult2.get(answer));
        }
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : Query(com.ingrian.security.nae.KMIPQueryFunction.Query) ArrayList(java.util.ArrayList) NAEClientCertificate(com.ingrian.security.nae.NAEClientCertificate) IngrianProvider(com.ingrian.security.nae.IngrianProvider) KMIPSession(com.ingrian.security.nae.KMIPSession)

Example 45 with IngrianProvider

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

the class KMIPSecretDataSample method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        usage();
    }
    String keyName = args.length == 3 ? args[2] : "KMIPSecretData";
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    KMIPSession session = KMIPSession.getSession(new NAEClientCertificate(args[0], args[1].toCharArray()));
    try {
        // generate the secret data (the bytes of a public key)
        // For IBM Java, change the provider from "SUN/SunRsaSign" to "IBMJCE"
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "SunRsaSign");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        keyGen.initialize(1024, random);
        KeyPair keyPair = keyGen.generateKeyPair();
        PublicKey pub = keyPair.getPublic();
        byte[] data = pub.getEncoded();
        // create NAE Session: pass in Key Manager user name and password
        // KMIPSession session  = KMIPSession.getSession(new NAEClientCertificate( args[0],  args[1]));
        // create secret data managed object ParameterSpec
        KMIPAttributes initialAttributes = new KMIPAttributes();
        initialAttributes.add(KMIPAttribute.CryptographicUsageMask, (int) (UsageMask.Verify.getValue()));
        NAEParameterSpec spec = new NAEParameterSpec(keyName, 1024, (KMIPAttributes) initialAttributes, session);
        // create the secret data object as a KMIP secret data Password type
        KMIPSecretData secretDataManagedObject = new KMIPSecretData(keyName, KMIPSecretData.SecretDataType.Password, session);
        // register the secret data bytes
        secretDataManagedObject.register(data, spec);
        // now export() a copy of the secret data back from the Key Manager
        byte[] exportedSecretData = secretDataManagedObject.export();
        // compare the original and exported bytes
        if ((exportedSecretData != null) && Arrays.equals(exportedSecretData, data))
            System.out.println("Exported secret data equals original");
        else {
            System.out.println("Uh-oh!");
        }
        // print the bytes and close the session
        System.out.println("original: " + TTLVUtil.toHexString(data));
        System.out.println("exported: " + TTLVUtil.toHexString(exportedSecretData));
    } catch (Exception e) {
        System.out.println("The Cause is " + e.getMessage() + ".");
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : KeyPair(java.security.KeyPair) KMIPAttributes(com.ingrian.security.nae.KMIPAttributes) NAEParameterSpec(com.ingrian.security.nae.NAEParameterSpec) PublicKey(java.security.PublicKey) SecureRandom(java.security.SecureRandom) KMIPSecretData(com.ingrian.security.nae.KMIPSecretData) KeyPairGenerator(java.security.KeyPairGenerator) NAEClientCertificate(com.ingrian.security.nae.NAEClientCertificate) IngrianProvider(com.ingrian.security.nae.IngrianProvider) KMIPSession(com.ingrian.security.nae.KMIPSession)

Aggregations

IngrianProvider (com.ingrian.security.nae.IngrianProvider)45 NAESession (com.ingrian.security.nae.NAESession)27 NAEKey (com.ingrian.security.nae.NAEKey)25 KMIPSession (com.ingrian.security.nae.KMIPSession)19 NAEClientCertificate (com.ingrian.security.nae.NAEClientCertificate)19 Provider (java.security.Provider)19 NAEParameterSpec (com.ingrian.security.nae.NAEParameterSpec)17 NAEPrivateKey (com.ingrian.security.nae.NAEPrivateKey)17 NAEPublicKey (com.ingrian.security.nae.NAEPublicKey)17 KMIPAttributes (com.ingrian.security.nae.KMIPAttributes)16 KeyGenerator (javax.crypto.KeyGenerator)11 Cipher (javax.crypto.Cipher)10 NAEException (com.ingrian.security.nae.NAEException)9 KMIPSecretData (com.ingrian.security.nae.KMIPSecretData)8 NAESecretKey (com.ingrian.security.nae.NAESecretKey)8 SecretKey (javax.crypto.SecretKey)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)7 NAESecureRandom (com.ingrian.security.nae.NAESecureRandom)6 KeyPair (java.security.KeyPair)6 NAEPermission (com.ingrian.security.nae.NAEPermission)5