use of com.ingrian.security.nae.NAEIvAndTweakDataParameter in project CipherTrust_Application_Protection by thalescpl-io.
the class CryptoTool method doDecryptFPE.
private static boolean doDecryptFPE(String keyName, String algName, byte[] iv, NAESession session, String tweakData, String tweakAlgo) throws Exception {
Key key = NAEKey.getSecretKey(keyName, session);
IvParameterSpec ivSpec = null;
NAEIvAndTweakDataParameter ivtweak = null;
if (iv == null) {
ivtweak = new NAEIvAndTweakDataParameter(tweakData, tweakAlgo);
} else {
ivSpec = new IvParameterSpec(iv);
// Initializes IV and tweak parameters
ivtweak = new NAEIvAndTweakDataParameter(ivSpec, tweakData, tweakAlgo);
}
// get a cipher
Cipher cipher = null;
try {
if (algName.toUpperCase().endsWith("CARD10"))
cipher = NAECipher.getNAECipherInstance("FPE/AES/CARD10", "IngrianProvider");
else if (algName.toUpperCase().endsWith("CARD26"))
cipher = NAECipher.getNAECipherInstance("FPE/AES/CARD26", "IngrianProvider");
else if (algName.toUpperCase().endsWith("CARD62"))
cipher = NAECipher.getNAECipherInstance("FPE/AES/CARD62", "IngrianProvider");
} catch (NoSuchAlgorithmException e) {
throw e;
}
// initialize cipher to encrypt.
cipher.init(Cipher.DECRYPT_MODE, key, ivtweak);
inputscanner = new Scanner(is);
String result = inputscanner.nextLine();
while (EMPTYSTRING.equals(result)) {
result = inputscanner.hasNext() ? inputscanner.nextLine() : null;
}
os.write(cipher.doFinal(result.getBytes()));
return true;
}
use of com.ingrian.security.nae.NAEIvAndTweakDataParameter in project CipherTrust_Application_Protection by thalescpl-io.
the class CryptoTool method doEncryptFPE.
private static boolean doEncryptFPE(String keyName, String algName, byte[] iv, NAESession session, String tweakData, String tweakAlgo) throws Exception {
Key key = NAEKey.getSecretKey(keyName, session);
IvParameterSpec ivSpec = null;
NAEIvAndTweakDataParameter ivtweak = null;
if (iv == null) {
ivtweak = new NAEIvAndTweakDataParameter(tweakData, tweakAlgo);
} else {
ivSpec = new IvParameterSpec(iv);
// Initializes IV and tweak parameters
ivtweak = new NAEIvAndTweakDataParameter(ivSpec, tweakData, tweakAlgo);
}
// get a cipher
Cipher cipher = null;
try {
if (algName.toUpperCase().endsWith("CARD10"))
cipher = NAECipher.getNAECipherInstance("FPE/AES/CARD10", "IngrianProvider");
else if (algName.toUpperCase().endsWith("CARD26"))
cipher = NAECipher.getNAECipherInstance("FPE/AES/CARD26", "IngrianProvider");
else if (algName.toUpperCase().endsWith("CARD62"))
cipher = NAECipher.getNAECipherInstance("FPE/AES/CARD62", "IngrianProvider");
} catch (NoSuchAlgorithmException e) {
throw e;
}
// initialize cipher to encrypt.
cipher.init(Cipher.ENCRYPT_MODE, key, ivtweak);
String result = null;
inputscanner = new Scanner(is);
result = inputscanner.nextLine();
while (EMPTYSTRING.equals(result)) {
result = inputscanner.hasNext() ? inputscanner.nextLine() : null;
}
os.write(cipher.doFinal(result.getBytes()));
return true;
}
use of com.ingrian.security.nae.NAEIvAndTweakDataParameter in project CipherTrust_Application_Protection by thalescpl-io.
the class FPEEncryptionDecryptionSample method main.
public static void main(String[] args) throws Exception {
if (args.length != 6) {
System.err.println("Usage: java FPEEncryptionDecryptionSample user password keyname IV TweakAlgorithm(Optional) TweakData(Optional)");
System.err.println("Mention null for optional parameter if you don't want to pass it");
/*
* Usage: keyname Supports AES Non-versioned key
* Usage: IV Must be 56 bytes Hex format string for AES key. IV must be of cardinality-10 that means each two characters (byte) of HEX IV must be 00-09
* IV must be provided when length of the data exceeds 56 bytes. FPE algorithm breaks the long data into 56 s-integer blocks and
* uses block chaining algorithm very similar to CBC mode to encrypt and chain the long data.
* when length of the data does not exceed MAXb value, the IV must be absent.
* Usage: TweakAlgorithm(Optional) must be from SHA1, SHA256 or None
* Usage: TweakData(Optional) If, tweak data algorithm is 'None' or absent,
the value must be HEX encoded string representing 64 bit long. In case of valid tweak Algorithm,
the tweak data value can be any ASCII string (not necessarily HEX).
Tweak Data is first processed using Tweak Hash Algorithm and the result is truncated to 64 bits
for input to the FPE algorithm
*/
System.exit(-1);
}
String username = args[0];
String password = args[1];
String keyName = args[2];
String _iv = args[3];
String tweakAlgo = null;
if (!args[4].contains("null")) {
tweakAlgo = args[4];
}
String tweakData = null;
if (!args[5].contains("null")) {
tweakData = args[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 (Provider provider : providers) {
System.out.println(provider.getInfo());
}
String dataToEncrypt = "36253865463254715234987125394785127934571235487631254876512837451827635487123564875216384728347";
System.out.println("Data to encrypt \"" + dataToEncrypt + "\"");
NAESession session = null;
try {
// create NAE Session: pass in Key Manager user name and password
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);
byte[] iv = null;
NAESecureRandom rng;
iv = IngrianProvider.hex2ByteArray(_iv);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
// Initializes IV and tweak parameters
NAEIvAndTweakDataParameter ivtweak = null;
ivtweak = new NAEIvAndTweakDataParameter(ivSpec, tweakData, tweakAlgo);
// get a cipher
Cipher encryptCipher = Cipher.getInstance("FPE/AES/CARD10", "IngrianProvider");
// initialize cipher to encrypt.
encryptCipher.init(Cipher.ENCRYPT_MODE, key, ivtweak);
// encrypt data
byte[] outbuf = encryptCipher.doFinal(dataToEncrypt.getBytes());
System.out.println("encrypted data data \"" + new String(outbuf) + "\"");
Cipher decryptCipher = Cipher.getInstance("FPE/AES/CARD10", "IngrianProvider");
// to decrypt data, initialize cipher to decrypt
decryptCipher.init(Cipher.DECRYPT_MODE, key, ivtweak);
// decrypt data
byte[] newbuf = decryptCipher.doFinal(outbuf);
System.out.println("Decrypted data \"" + new String(newbuf) + "\"");
// close the session
session.closeSession();
} catch (Exception e) {
System.out.println("The Cause is " + e.getMessage() + ".");
throw e;
} finally {
if (session != null) {
session.closeSession();
}
}
}
Aggregations