Last active
May 12, 2017 06:55
-
-
Save tmarthal/6dbec6cc358ab78ea090747499c57981 to your computer and use it in GitHub Desktop.
Spring Security AES Encryption Setup/Usage Blog Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
byte[] iv = this.ivGenerator.generateKey(); | |
byte[] encrypted = doFinal(this.encryptor, bytes); | |
return this.ivGenerator != NULL_IV_GENERATOR ? concatenate(iv, encrypted) : encrypted; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102: PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e3673e05560e1d24f26b4a6ed300a298c17cb9d7be9a52f11d9358421a79c015 | |
98937740c652f6a17febce5013d7d13aed67a8867365e5a89374e658ad74f742 | |
d7f8253bf30e3af957b0b8b2bf62bca693db08cff80ef49c98dcf4485d067446 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36: public String encrypt(String text) { return new String(Hex.encode(encryptor.encrypt(Utf8.encode(text)))); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a070fa1b40dafa6d7c55ee697c4cb448 | |
a070fa1b40dafa6d7c55ee697c4cb448 | |
a070fa1b40dafa6d7c55ee697c4cb448 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TextEncryptor encryptor = Encryptors.queryableText(password, salt); | |
for (int i=0; i<3; i++) { | |
System.out.println(encryptor.encrypt("foo")); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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