Search in sources :

Example 1 with ConcurrentPersistantEncryptingHashMap

use of com.ingrian.internal.cache.ConcurrentPersistantEncryptingHashMap in project CipherTrust_Application_Protection by thalescpl-io.

the class CachingSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        System.err.println("Usage: java CachingSample user password keyname");
        System.exit(-1);
    }
    String username = args[0];
    String password = args[1];
    String keyName = args[2];
    CachingSample sample = new CachingSample();
    // 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());
    }
    String dataToEncrypt = "1234567812345678";
    System.out.println("Data to encrypt \"" + dataToEncrypt + "\"");
    // create NAE Session: pass in Key Manager user name and password
    MyNAEKeyCachePassphrase m = sample.new MyNAEKeyCachePassphrase();
    NAESession session = null;
    try {
        session = NAESession.getSession(username, password.toCharArray(), m.getPassphrase(null));
        // Get SecretKey (just a handle to it, key data does not leave the Key Manager
        System.out.println("KEYNAME === " + keyName);
        sample.oneShotEncrypt(session, keyName, "AES/CBC/NoPadding", dataToEncrypt, "1234567812345678");
        sample.oneShotEncrypt(session, keyName, "AES/CBC/PKCS5Padding", dataToEncrypt, "1234567812345678");
        sample.oneShotEncrypt(session, keyName, "AES/CBC/PKCS5Padding", dataToEncrypt, null);
        sample.oneShotEncrypt(session, keyName, "AES/ECB/PKCS5Padding", dataToEncrypt, null);
        sample.oneShotEncrypt(session, keyName, "AES/ECB/NoPadding", dataToEncrypt, null);
        session.printCachingDetails();
        Thread.sleep(1000);
        System.out.println("Reading cache from disk to read");
        PersistentCache p = new PersistentCache();
        ConcurrentPersistantEncryptingHashMap map = p.readFromDisk(username, session.getPassphrase());
        if (map != null) {
            System.out.println("Size cache from disk is = " + map.size());
            Set set = map.keySet();
            Iterator<String> iter = set.iterator();
            while (iter.hasNext()) {
                String o = iter.next();
                System.out.println("Key cache from disk = " + o);
                NAECachedKey n = (NAECachedKey) map.get(o);
            }
        } else {
            System.out.println("Map from disk is null");
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("The Cause is " + e.getMessage() + ".");
        throw e;
    } finally {
        if (session != null) {
            session.closeSession();
        }
    }
}
Also used : Set(java.util.Set) IngrianProvider(com.ingrian.security.nae.IngrianProvider) Provider(java.security.Provider) IngrianProvider(com.ingrian.security.nae.IngrianProvider) ConcurrentPersistantEncryptingHashMap(com.ingrian.internal.cache.ConcurrentPersistantEncryptingHashMap) PersistentCache(com.ingrian.internal.cache.PersistentCache) NAECachedKey(com.ingrian.internal.cache.NAECachedKey) NAESession(com.ingrian.security.nae.NAESession)

Aggregations

ConcurrentPersistantEncryptingHashMap (com.ingrian.internal.cache.ConcurrentPersistantEncryptingHashMap)1 NAECachedKey (com.ingrian.internal.cache.NAECachedKey)1 PersistentCache (com.ingrian.internal.cache.PersistentCache)1 IngrianProvider (com.ingrian.security.nae.IngrianProvider)1 NAESession (com.ingrian.security.nae.NAESession)1 Provider (java.security.Provider)1 Set (java.util.Set)1