Created
July 16, 2021 07:25
-
-
Save jshiell/e1178c4386d4756b25664da01a999d8f to your computer and use it in GitHub Desktop.
AES Example in Java
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
import javax.crypto.Cipher; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.io.ByteArrayOutputStream; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.util.Base64; | |
public class Encrypter { | |
private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; | |
private static final String KEY_ALGORITHM = "AES"; | |
public String makeMeAKey() throws NoSuchAlgorithmException { | |
SecretKey key = getAESKey(); | |
return Base64.getEncoder().encodeToString(key.getEncoded()); | |
} | |
private SecretKey getAESKey() throws NoSuchAlgorithmException { | |
KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM); | |
keyGen.init(256, SecureRandom.getInstanceStrong()); | |
return keyGen.generateKey(); | |
} | |
public String encrypt(String input, String encodedKey) throws Exception { | |
IvParameterSpec iv = gimmeGimmeGimmeAnIvAfterMidnight(); | |
Cipher cipher = Cipher.getInstance(ALGORITHM); | |
cipher.init(Cipher.ENCRYPT_MODE, keyFromString(encodedKey), iv); | |
byte[] cypherText = cipher.doFinal(input.getBytes()); | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
out.write(iv.getIV()); | |
out.write(cypherText); | |
return Base64.getEncoder().encodeToString(out.toByteArray()); | |
} | |
private SecretKey keyFromString(String base64Key) { | |
byte[] decodedKey = Base64.getDecoder().decode(base64Key); | |
return new SecretKeySpec(decodedKey, 0, decodedKey.length, KEY_ALGORITHM); | |
} | |
private IvParameterSpec gimmeGimmeGimmeAnIvAfterMidnight() { | |
byte[] iv = new byte[16]; | |
new SecureRandom().nextBytes(iv); | |
return new IvParameterSpec(iv); | |
} | |
public String decrypt(String cypherText, String encodedKey) throws Exception { | |
byte[] cyperPlusIv = Base64.getDecoder().decode(cypherText); | |
IvParameterSpec iv = new IvParameterSpec(cyperPlusIv, 0, 16); | |
ByteArrayOutputStream cipherOnly = new ByteArrayOutputStream(); | |
cipherOnly.write(cyperPlusIv, 16, cyperPlusIv.length - 16); | |
Cipher cipher = Cipher.getInstance(ALGORITHM); | |
cipher.init(Cipher.DECRYPT_MODE, keyFromString(encodedKey), iv); | |
byte[] plainText = cipher.doFinal(cipherOnly.toByteArray()); | |
return new String(plainText); | |
} | |
public static void main(String[] args) { | |
try { | |
Encrypter encrypter = new Encrypter(); | |
String secretKey = encrypter.makeMeAKey(); | |
System.err.println("My key is " + secretKey); | |
String plaintext = "Now is the winter of your discontent"; | |
System.err.println("Plaintext is " + plaintext); | |
String encrypted = encrypter.encrypt(plaintext, secretKey); | |
System.err.println("Encrypted is " + encrypted); | |
String decrypted = encrypter.decrypt(encrypted, secretKey); | |
System.err.println("Descrypted is " + decrypted); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment