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();
}
}
}
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();
}
}
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();
}
}
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();
}
}
}
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();
}
}
Aggregations