Encrypt/Decrypt Email (Masking Domain)
Encrypting Email
Create an
NAESessionobject. Pass Key Manager username and password as arguments.Obtain an instance of the key you want to use for the encryption operation.
Get
EmailDomainFormatfromFPEFormat.Set the start and end values.
Create
FPEParameterAndFormatSpecwithEmailDomainFormat.Create a cipher
encryptCipherand initialize it withENCRYPT_MODE, key, andFPEParameterAndFormatSpec.Call the
doFinalonencryptCipherto obtain the masked email.
Code sample
This sample shows how to encrypt email address.
String dataToEncryptEmail = "steve_johnson@sample.com";
NAESession session = NAESession.getSession(username, password.toCharArray());
NAEKey key = NAEKey.getSecretKey(keyName, session);
FPEFormat formatEmail = FPEFormat.getEmailDomainFormat();
formatEmail.setNumberOfElementsBeforeEnd(2);
formatEmail.setNumberOfElementsFromStart(2);
FPEParameterAndFormatSpec paramSpec = new FPEParameterAndFormatBuilder(tweakData).set_tweakAlgorithm(tweakAlgo).setFpeFormat(formatEmail).build();
Cipher encryptCipher = Cipher.getInstance("FPE/AES/CARD62", "IngrianProvider");
encryptCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] maskedEmail = encryptCipher.doFinal(dataToEncryptEmail.getBytes());
Output
stotn_boesvon@sample.com
Decrypting Email
Create an
NAESessionobject. Pass Key Manager username and password as arguments.Obtain an instance of the key you want to use for the decryption operation.
Get
EmailDomainFormatfromFPEFormat.Set the start and end values.
Create
FPEParameterAndFormatSpecwithEmailDomainFormat.Create a cipher
decryptCipherand initialize it withENCRYPT_MODE, key, andFPEParameterAndFormatSpec.Call the
doFinalondecryptCipherto obtain the masked email.
Code sample
This sample shows how to decrypt email address.
String dataToDecryptEmail = "stotn_boesvon@sample.com";
NAESession session = NAESession.getSession(username, password.toCharArray());
NAEKey key = NAEKey.getSecretKey(keyName, session);
FPEFormat formatEmail = FPEFormat.getEmailDomainFormat();
formatEmail.setNumberOfElementsBeforeEnd(2);
formatEmail.setNumberOfElementsFromStart(2);
FPEParameterAndFormatSpec paramSpec = new FPEParameterAndFormatBuilder(tweakData).set_tweakAlgorithm(tweakAlgo).setFpeFormat(formatEmail).build();
Cipher decryptCipher = Cipher.getInstance("FPE/AES/CARD62", "IngrianProvider");
decryptCipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
byte[] maskedEmail = decryptCipher.doFinal(dataToEncryptEmail.getBytes());
Output
steve_johnson@sample.com
References
- The sample for Masking domain in email addresses algorithm is available on Github.