Created
April 4, 2018 15:17
-
-
Save JulienGenoud/3aede8cdc229b3d52df6ff0f9453c6e9 to your computer and use it in GitHub Desktop.
AES SAMPLE
This file contains 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 str = "coucou"; | |
byte[] keyStart = "this is a key".getBytes(); | |
KeyGenerator kgen = KeyGenerator.getInstance("AES"); | |
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); | |
sr.setSeed(keyStart); | |
kgen.init(256, sr); // 192 and 256 bits may not be available | |
SecretKey skey = kgen.generateKey(); | |
SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(), "AES"); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); | |
byte[] encrypted = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)); | |
SecretKeySpec skeySpec2 = new SecretKeySpec(skey.getEncoded(), "AES"); | |
Cipher cipher2 = Cipher.getInstance("AES"); | |
cipher2.init(Cipher.DECRYPT_MODE, skeySpec2); | |
byte[] decrypted = cipher2.doFinal(encrypted); | |
new String(decrypted, StandardCharsets.UTF_8); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment