Encrypt/Decrypt Email (Masking Domain)
Encrypting Email
Create an
NAESession
object. Pass Key Manager username and password as arguments.Obtain an instance of the key you want to use for the encryption operation.
Get
EmailDomainFormat
fromFPEFormat
.Set the start and end values.
Create
FPEParameterAndFormatSpec
withEmailDomainFormat
.Create a cipher
encryptCipher
and initialize it withENCRYPT_MODE
, key, andFPEParameterAndFormatSpec
.Call the
doFinal
onencryptCipher
to 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
NAESession
object. Pass Key Manager username and password as arguments.Obtain an instance of the key you want to use for the decryption operation.
Get
EmailDomainFormat
fromFPEFormat
.Set the start and end values.
Create
FPEParameterAndFormatSpec
withEmailDomainFormat
.Create a cipher
decryptCipher
and initialize it withENCRYPT_MODE
, key, andFPEParameterAndFormatSpec
.Call the
doFinal
ondecryptCipher
to 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.