|
| 1 | +package ciphers; |
| 2 | + |
| 3 | +import java.security.InvalidKeyException; |
| 4 | +import java.security.NoSuchAlgorithmException; |
| 5 | +import javax.crypto.BadPaddingException; |
1 | 6 | import javax.crypto.Cipher; |
| 7 | +import javax.crypto.IllegalBlockSizeException; |
2 | 8 | import javax.crypto.KeyGenerator; |
| 9 | +import javax.crypto.NoSuchPaddingException; |
3 | 10 | import javax.crypto.SecretKey; |
4 | 11 | import javax.xml.bind.DatatypeConverter; |
5 | 12 |
|
6 | 13 | /** |
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. |
| 14 | + * This example program shows how AES encryption and decryption can be done in |
| 15 | + * Java. Please note that secret key and encrypted text is unreadable binary and |
| 16 | + * hence in the following program we display it in hexadecimal format of the |
| 17 | + * underlying bytes. |
| 18 | + * |
10 | 19 | */ |
11 | 20 | 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{ |
| 21 | + |
| 22 | + /** |
| 23 | + * 1. Generate a plain text for encryption 2. Get a secret key (printed in |
| 24 | + * hexadecimal form). In actual use this must by encrypted and kept safe. The |
| 25 | + * same key is required for decryption. |
| 26 | + * |
| 27 | + */ |
| 28 | + public static void main(String[] args) throws Exception { |
| 29 | + String plainText = "Hello World"; |
| 30 | + SecretKey secKey = getSecretEncryptionKey(); |
| 31 | + byte[] cipherText = encryptText(plainText, secKey); |
| 32 | + String decryptedText = decryptText(cipherText, secKey); |
| 33 | + |
| 34 | + System.out.println("Original Text:" + plainText); |
| 35 | + System.out.println("AES Key (Hex Form):" + bytesToHex(secKey.getEncoded())); |
| 36 | + System.out.println("Encrypted Text (Hex Form):" + bytesToHex(cipherText)); |
| 37 | + System.out.println("Descrypted Text:" + decryptedText); |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * gets the AES encryption key. In your actual programs, this should be safely |
| 43 | + * stored. |
| 44 | + * |
| 45 | + * @return secKey (Secret key that we encrypt using it) |
| 46 | + * @throws NoSuchAlgorithmException |
| 47 | + * (from KeyGenrator) |
| 48 | + * |
| 49 | + */ |
| 50 | + public static SecretKey getSecretEncryptionKey() throws NoSuchAlgorithmException { |
| 51 | + KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES"); |
| 52 | + aesKeyGenerator.init(128); // The AES key size in number of bits |
| 53 | + SecretKey secKey = aesKeyGenerator.generateKey(); |
| 54 | + return secKey; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Encrypts plainText in AES using the secret key |
| 59 | + * |
| 60 | + * @param plainText |
| 61 | + * @param secKey |
| 62 | + * @return byteCipherText (The encrypted text) |
| 63 | + * @throws NoSuchPaddingException |
| 64 | + * (from Cipher) |
| 65 | + * @throws NoSuchAlgorithmException |
| 66 | + * (from Cipher) |
| 67 | + * @throws InvalidKeyException |
| 68 | + * (from Cipher) |
| 69 | + * @throws BadPaddingException |
| 70 | + * (from Cipher) |
| 71 | + * @throws IllegalBlockSizeException |
| 72 | + * (from Cipher) |
| 73 | + */ |
| 74 | + public static byte[] encryptText(String plainText, SecretKey secKey) throws NoSuchAlgorithmException, |
| 75 | + NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { |
53 | 76 | // 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 { |
| 77 | + Cipher aesCipher = Cipher.getInstance("AES"); |
| 78 | + aesCipher.init(Cipher.ENCRYPT_MODE, secKey); |
| 79 | + byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes()); |
| 80 | + return byteCipherText; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Decrypts encrypted byte array using the key used for encryption. |
| 85 | + * |
| 86 | + * @param byteCipherText |
| 87 | + * @param secKey |
| 88 | + * @return plainText |
| 89 | + * @throws NoSuchPaddingException |
| 90 | + * @throws NoSuchAlgorithmException |
| 91 | + * @throws InvalidKeyException |
| 92 | + * @throws BadPaddingException |
| 93 | + * @throws IllegalBlockSizeException |
| 94 | + */ |
| 95 | + public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws NoSuchAlgorithmException, |
| 96 | + NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { |
68 | 97 | // 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 | | - } |
| 98 | + Cipher aesCipher = Cipher.getInstance("AES"); |
| 99 | + aesCipher.init(Cipher.DECRYPT_MODE, secKey); |
| 100 | + byte[] bytePlainText = aesCipher.doFinal(byteCipherText); |
| 101 | + return new String(bytePlainText); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Convert a binary byte array into readable hex form |
| 106 | + * |
| 107 | + * @param hash |
| 108 | + * (in binary) |
| 109 | + * @return hexHash |
| 110 | + */ |
| 111 | + private static String bytesToHex(byte[] hash) { |
| 112 | + return DatatypeConverter.printHexBinary(hash); |
| 113 | + } |
83 | 114 | } |
0 commit comments