Search in sources :

Example 1 with NAEIvAndTweakDataParameter

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;
}
Also used : Scanner(java.util.Scanner) NAEIvAndTweakDataParameter(com.ingrian.security.nae.NAEIvAndTweakDataParameter) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NAECipher(com.ingrian.security.nae.NAECipher) NAEAESGCMCipher(com.ingrian.security.nae.NAEAESGCMCipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NAEKey(com.ingrian.security.nae.NAEKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey)

Example 2 with NAEIvAndTweakDataParameter

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;
}
Also used : Scanner(java.util.Scanner) NAEIvAndTweakDataParameter(com.ingrian.security.nae.NAEIvAndTweakDataParameter) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NAECipher(com.ingrian.security.nae.NAECipher) NAEAESGCMCipher(com.ingrian.security.nae.NAEAESGCMCipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NAEKey(com.ingrian.security.nae.NAEKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey)

Example 3 with NAEIvAndTweakDataParameter

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();
        }
    }
}
Also used : NAEKey(com.ingrian.security.nae.NAEKey) NAESecureRandom(com.ingrian.security.nae.NAESecureRandom) NAEIvAndTweakDataParameter(com.ingrian.security.nae.NAEIvAndTweakDataParameter) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NAESession(com.ingrian.security.nae.NAESession) IngrianProvider(com.ingrian.security.nae.IngrianProvider) IngrianProvider(com.ingrian.security.nae.IngrianProvider) Provider(java.security.Provider)

Aggregations

NAEIvAndTweakDataParameter (com.ingrian.security.nae.NAEIvAndTweakDataParameter)3 NAEKey (com.ingrian.security.nae.NAEKey)3 Cipher (javax.crypto.Cipher)3 IvParameterSpec (javax.crypto.spec.IvParameterSpec)3 NAEAESGCMCipher (com.ingrian.security.nae.NAEAESGCMCipher)2 NAECipher (com.ingrian.security.nae.NAECipher)2 Key (java.security.Key)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 PrivateKey (java.security.PrivateKey)2 PublicKey (java.security.PublicKey)2 Scanner (java.util.Scanner)2 SecretKey (javax.crypto.SecretKey)2 IngrianProvider (com.ingrian.security.nae.IngrianProvider)1 NAESecureRandom (com.ingrian.security.nae.NAESecureRandom)1 NAESession (com.ingrian.security.nae.NAESession)1 Provider (java.security.Provider)1