Skip to content

Instantly share code, notes, and snippets.

@tmarthal
Last active May 12, 2017 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tmarthal/6dbec6cc358ab78ea090747499c57981 to your computer and use it in GitHub Desktop.
Save tmarthal/6dbec6cc358ab78ea090747499c57981 to your computer and use it in GitHub Desktop.
Spring Security AES Encryption Setup/Usage Blog Code
byte[] iv = this.ivGenerator.generateKey();
byte[] encrypted = doFinal(this.encryptor, bytes);
return this.ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted) : encrypted;
102: PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256);
e3673e05560e1d24f26b4a6ed300a298c17cb9d7be9a52f11d9358421a79c015
98937740c652f6a17febce5013d7d13aed67a8867365e5a89374e658ad74f742
d7f8253bf30e3af957b0b8b2bf62bca693db08cff80ef49c98dcf4485d067446
String password = "password";
String salt = "5c0744940b5c369b"; // from the spring-crypto library tests
TextEncryptor encryptor = Encryptors.text(password, salt);
for (int i=0; i<3; i++) {
System.out.println(encryptor.encrypt("foo"));
}
36: public String encrypt(String text) { return new String(Hex.encode(encryptor.encrypt(Utf8.encode(text)))); }
a070fa1b40dafa6d7c55ee697c4cb448
a070fa1b40dafa6d7c55ee697c4cb448
a070fa1b40dafa6d7c55ee697c4cb448
TextEncryptor encryptor = Encryptors.queryableText(password, salt);
for (int i=0; i<3; i++) {
System.out.println(encryptor.encrypt("foo"));
}
import org.springframework.security.crypto.encrypt.Encryptors
import org.springframework.security.crypto.encrypt.TextEncryptor
TextEncryptor encryptor = Encryptors.text(password, salt);
String encryptedText = encryptor.encrypt(plainText);
String decrypted = encryptor.decrypt(encryptedText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment