Skip to content

Commit 18b36a9

Browse files
authored
AES Encryption/Decryption
1 parent d7fcdf4 commit 18b36a9

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

ciphers/AESEncryption

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import javax.crypto.Cipher;
2+
import javax.crypto.KeyGenerator;
3+
import javax.crypto.SecretKey;
4+
import javax.xml.bind.DatatypeConverter;
5+
6+
/**
7+
* This example program shows how AES encryption and decryption can be done in Java.
8+
* Please note that secret key and encrypted text is unreadable binary and hence
9+
* in the following program we display it in hexadecimal format of the underlying bytes.
10+
*/
11+
public class AESEncryption {
12+
13+
/**
14+
* 1. Generate a plain text for encryption
15+
* 2. Get a secret key (printed in hexadecimal form). In actual use this must
16+
* by encrypted and kept safe. The same key is required for decryption.
17+
*
18+
*/
19+
public static void main(String[] args) throws Exception {
20+
String plainText = "Hello World";
21+
SecretKey secKey = getSecretEncryptionKey();
22+
byte[] cipherText = encryptText(plainText, secKey);
23+
String decryptedText = decryptText(cipherText, secKey);
24+
25+
System.out.println("Original Text:" + plainText);
26+
System.out.println("AES Key (Hex Form):"+bytesToHex(secKey.getEncoded()));
27+
System.out.println("Encrypted Text (Hex Form):"+bytesToHex(cipherText));
28+
System.out.println("Descrypted Text:"+decryptedText);
29+
30+
}
31+
32+
/**
33+
* gets the AES encryption key. In your actual programs, this should be safely
34+
* stored.
35+
* @return
36+
* @throws Exception
37+
*/
38+
public static SecretKey getSecretEncryptionKey() throws Exception{
39+
KeyGenerator generator = KeyGenerator.getInstance("AES");
40+
generator.init(128); // The AES key size in number of bits
41+
SecretKey secKey = generator.generateKey();
42+
return secKey;
43+
}
44+
45+
/**
46+
* Encrypts plainText in AES using the secret key
47+
* @param plainText
48+
* @param secKey
49+
* @return
50+
* @throws Exception
51+
*/
52+
public static byte[] encryptText(String plainText,SecretKey secKey) throws Exception{
53+
// AES defaults to AES/ECB/PKCS5Padding in Java 7
54+
Cipher aesCipher = Cipher.getInstance("AES");
55+
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
56+
byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());
57+
return byteCipherText;
58+
}
59+
60+
/**
61+
* Decrypts encrypted byte array using the key used for encryption.
62+
* @param byteCipherText
63+
* @param secKey
64+
* @return
65+
* @throws Exception
66+
*/
67+
public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws Exception {
68+
// AES defaults to AES/ECB/PKCS5Padding in Java 7
69+
Cipher aesCipher = Cipher.getInstance("AES");
70+
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
71+
byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
72+
return new String(bytePlainText);
73+
}
74+
75+
/**
76+
* Convert a binary byte array into readable hex form
77+
* @param hash
78+
* @return
79+
*/
80+
private static String bytesToHex(byte[] hash) {
81+
return DatatypeConverter.printHexBinary(hash);
82+
}
83+
}

0 commit comments

Comments
 (0)