Skip to content

Instantly share code, notes, and snippets.

@prabhatkashyap
Created October 24, 2017 14:45
Show Gist options
  • Select an option

  • Save prabhatkashyap/cc53879c53069701f704ec31c9c89e97 to your computer and use it in GitHub Desktop.

Select an option

Save prabhatkashyap/cc53879c53069701f704ec31c9c89e97 to your computer and use it in GitHub Desktop.
static public String encrypt(String unencryptedString) {
String UNICODE_FORMAT = "UTF8"
String DESEDE_ENCRYPTION_SCHEME = "DESede"
String myEncryptionKey = "Provide your encryption key"
String myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME
byte[] arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT)
KeySpec ks = new DESedeKeySpec(arrayBytes)
SecretKeyFactory skf = SecretKeyFactory.getInstance(myEncryptionScheme)
Cipher cipher = Cipher.getInstance(myEncryptionScheme)
SecretKey key = skf.generateSecret(ks)
String encryptedString = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
byte[] encryptedText = cipher.doFinal(plainText);
encryptedString = new String(Base64.encodeBase64(encryptedText));
} catch (Exception e) {
e.printStackTrace();
}
return encryptedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment