use of com.ingrian.security.nae.NAECipher in project CipherTrust_Application_Protection by thalescpl-io.
the class FileEncryptionSampleUsingGCM method main.
public static void main(String[] args) {
if (args.length != 10) {
System.err.println("Usage: java FileEncryptionSampleUsingGCM user password keyname fileToEncrypt " + "encryptedFile decryptedFile authTagLength iv aad 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];
int authTagLength = Integer.parseInt(args[6]);
String iv = args[7];
String aad = args[8];
int blockSize = Integer.parseInt(args[9]);
/**
* Note: For AES-GCM algorithm, same combination of nonce (IV) and key must not be reused
* during encryption/decryption operations.
*/
byte[] ivBytes = iv.getBytes();
byte[] aadBytes = aad.getBytes();
System.out.println("iv: " + IngrianProvider.byteArray2Hex(ivBytes));
System.out.println("AAD: " + IngrianProvider.byteArray2Hex(aadBytes));
Security.addProvider(new IngrianProvider());
NAESession session = null;
try {
session = NAESession.getSession(username, password.toCharArray());
NAEKey key = NAEKey.getSecretKey(keyName, session);
GCMParameterSpec spec = new GCMParameterSpec(authTagLength, ivBytes, aadBytes);
NAECipher cipher = NAECipher.getNAECipherInstance("AES/GCM/NoPadding", "IngrianProvider");
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
NAEAESGCMCipher gcm = cipher.get_spi();
gcm.update(srcName, dstName, blockSize, cipher);
cipher.init(Cipher.DECRYPT_MODE, key, spec);
gcm = cipher.get_spi();
gcm.update(dstName, decrName, blockSize, cipher);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.closeSession();
}
}
}
use of com.ingrian.security.nae.NAECipher in project CipherTrust_Application_Protection by thalescpl-io.
the class FileEncryptionSampleUsingSEED method main.
public static void main(String[] args) {
if (args.length != 8) {
System.err.println("Usage: java FileEncryptionSampleUsingSEED 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 = "SEED/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);
NAESEEDCipher seed = cipher.get_seed();
seed.update(srcName, dstName, blockSize, cipher);
cipher.init(Cipher.DECRYPT_MODE, key);
seed = cipher.get_seed();
seed.update(dstName, decrName, blockSize, cipher);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.closeSession();
}
}
}
use of com.ingrian.security.nae.NAECipher in project CipherTrust_Application_Protection by thalescpl-io.
the class CryptoTool method doEncryptGCM.
private static boolean doEncryptGCM(String keyName, String algName, byte[] iv, NAESession session, String authTagLength, String aad, String inFile, String outFile) throws Exception {
Key key = NAEKey.getSecretKey(keyName, session);
boolean isAADSpecified = false;
if ((null != aad) && !EMPTYSTRING.equals(aad)) {
isAADSpecified = true;
}
Integer authtaglength = Integer.parseInt(authTagLength);
if (authtaglength == null) {
System.err.println("Unknown AuthTagLength");
}
NAECipher cipher = NAECipher.getNAECipherInstance("AES/GCM/NoPadding", "IngrianProvider");
if (isAADSpecified) {
byte[] aadBytes = IngrianProvider.hex2ByteArray(aad);
GCMParameterSpec gcmSpec = new GCMParameterSpec(authtaglength.intValue(), iv, aadBytes);
try {
cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec);
} catch (InvalidAlgorithmParameterException e) {
throw new NAEException("Encrypt: failed - " + e.getMessage());
} catch (InvalidKeyException e) {
throw e;
}
} else {
try {
GCMParameterSpec gcmSpec = new GCMParameterSpec(authtaglength.intValue(), iv);
cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec);
} catch (InvalidKeyException e) {
throw e;
} catch (InvalidAlgorithmParameterException e) {
throw e;
}
}
String result = null;
inputscanner = new Scanner(is);
result = inputscanner.nextLine();
if (inFile != null && outFile != null) {
NAEAESGCMCipher gcm = cipher.get_spi();
gcm.update(inFile, outFile, 1024, cipher);
} else {
while (EMPTYSTRING.equals(result)) result = inputscanner.hasNext() ? inputscanner.nextLine() : null;
os.writeBytes(IngrianProvider.byteArray2Hex(cipher.doFinal(result.getBytes())));
}
return true;
}
use of com.ingrian.security.nae.NAECipher in project CipherTrust_Application_Protection by thalescpl-io.
the class CryptoTool method doDecryptGCM.
private static boolean doDecryptGCM(String keyName, String algName, byte[] iv, NAESession session, String authTagLength, String aad, String inFile, String outFile) throws Exception {
Key key = NAEKey.getSecretKey(keyName, session);
boolean isAADSpecified = false;
if ((null != aad) && !EMPTYSTRING.equals(aad)) {
isAADSpecified = true;
}
Integer authtaglength = Integer.parseInt(authTagLength);
if (authtaglength == null) {
System.err.println("Unknown AuthTagLength");
}
NAECipher cipher = NAECipher.getNAECipherInstance("AES/GCM/NoPadding", "IngrianProvider");
if (isAADSpecified) {
byte[] aadBytes = IngrianProvider.hex2ByteArray(aad);
GCMParameterSpec gcmSpec = new GCMParameterSpec(authtaglength.intValue(), iv, aadBytes);
try {
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec);
} catch (InvalidAlgorithmParameterException e) {
throw new NAEException("Decrypt: failed - " + e.getMessage());
// e.printStackTrace();
} catch (InvalidKeyException e) {
throw e;
}
} else {
try {
GCMParameterSpec gcmSpec = new GCMParameterSpec(authtaglength.intValue(), iv);
cipher.init(Cipher.DECRYPT_MODE, key, gcmSpec);
} catch (InvalidKeyException e) {
throw e;
} catch (InvalidAlgorithmParameterException e) {
throw e;
}
}
inputscanner = new Scanner(is);
String result = inputscanner.nextLine();
if (inFile != null && outFile != null) {
NAEAESGCMCipher gcm = cipher.get_spi();
gcm.update(inFile, outFile, 1024, cipher);
} else {
while (EMPTYSTRING.equals(result.trim())) result = inputscanner.hasNext() ? inputscanner.nextLine() : null;
os.write(cipher.doFinal(IngrianProvider.hex2ByteArray(result)));
}
return true;
}
use of com.ingrian.security.nae.NAECipher in project CipherTrust_Application_Protection by thalescpl-io.
the class MultiThreadSample method run.
public void run() {
try {
System.out.println("[" + Thread.currentThread().getName() + "] starting sample.");
// get IV
SecureRandom rng = SecureRandom.getInstance("IngrianRNG", "IngrianProvider");
byte[] iv = new byte[16];
rng.nextBytes(iv);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
NAECipher cipher = NAECipher.getNAECipherInstance("AES/CBC/PKCS5Padding", "IngrianProvider");
cipher.init(Cipher.ENCRYPT_MODE, _key, ivSpec);
int inputSize = data.length;
int outputSize = cipher.getOutputSize(inputSize);
byte[] outbuf = new byte[outputSize];
cipher.setMessage("Thread " + Thread.currentThread().getName());
int numBytes = cipher.doFinal(data, 0, inputSize, outbuf);
} catch (Exception e) {
System.out.println("Got exception: " + e);
e.printStackTrace();
}
}
Aggregations