Created
November 1, 2018 03:36
-
-
Save bmcculley/8d3b3721c357d479ebb5fa4680d5dd2e to your computer and use it in GitHub Desktop.
Taken from https://stackoverflow.com/a/34098587
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 java.util.Base64; | |
import javax.crypto.Cipher; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
public class EncryptionDecryptionAES { | |
static Cipher cipher; | |
public static void main(String[] args) throws Exception { | |
/* | |
create key | |
If we need to generate a new key use a KeyGenerator | |
If we have existing plaintext key use a SecretKeyFactory | |
*/ | |
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); | |
keyGenerator.init(128); // block size is 128bits | |
SecretKey secretKey = keyGenerator.generateKey(); | |
/* | |
Cipher Info | |
Algorithm : for the encryption of electronic data | |
mode of operation : to avoid repeated blocks encrypt to the same values. | |
padding: ensuring messages are the proper length necessary for certain ciphers | |
mode/padding are not used with stream cyphers. | |
*/ | |
cipher = Cipher.getInstance("AES"); //SunJCE provider AES algorithm, mode(optional) and padding schema(optional) | |
String plainText = "AES Symmetric Encryption Decryption"; | |
System.out.println("Plain Text Before Encryption: " + plainText); | |
String encryptedText = encrypt(plainText, secretKey); | |
System.out.println("Encrypted Text After Encryption: " + encryptedText); | |
String decryptedText = decrypt(encryptedText, secretKey); | |
System.out.println("Decrypted Text After Decryption: " + decryptedText); | |
} | |
public static String encrypt(String plainText, SecretKey secretKey) | |
throws Exception { | |
byte[] plainTextByte = plainText.getBytes(); | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey); | |
byte[] encryptedByte = cipher.doFinal(plainTextByte); | |
Base64.Encoder encoder = Base64.getEncoder(); | |
String encryptedText = encoder.encodeToString(encryptedByte); | |
return encryptedText; | |
} | |
public static String decrypt(String encryptedText, SecretKey secretKey) | |
throws Exception { | |
Base64.Decoder decoder = Base64.getDecoder(); | |
byte[] encryptedTextByte = decoder.decode(encryptedText); | |
cipher.init(Cipher.DECRYPT_MODE, secretKey); | |
byte[] decryptedByte = cipher.doFinal(encryptedTextByte); | |
String decryptedText = new String(decryptedByte); | |
return decryptedText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment