|
1 | 1 | package Ciphers;
|
2 | 2 |
|
| 3 | +import javax.crypto.*; |
3 | 4 | import java.security.InvalidKeyException;
|
4 | 5 | import java.security.NoSuchAlgorithmException;
|
5 |
| -import javax.crypto.BadPaddingException; |
6 |
| -import javax.crypto.Cipher; |
7 |
| -import javax.crypto.IllegalBlockSizeException; |
8 |
| -import javax.crypto.KeyGenerator; |
9 |
| -import javax.crypto.NoSuchPaddingException; |
10 |
| -import javax.crypto.SecretKey; |
11 | 6 |
|
12 | 7 | /**
|
13 | 8 | * This example program shows how AES encryption and decryption can be done in Java. Please note
|
|
16 | 11 | */
|
17 | 12 | public class AESEncryption {
|
18 | 13 |
|
19 |
| - private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); |
20 |
| - /** |
21 |
| - * 1. Generate a plain text for encryption 2. Get a secret key (printed in hexadecimal form). In |
22 |
| - * actual use this must by encrypted and kept safe. The same key is required for decryption. |
23 |
| - */ |
24 |
| - public static void main(String[] args) throws Exception { |
25 |
| - String plainText = "Hello World"; |
26 |
| - SecretKey secKey = getSecretEncryptionKey(); |
27 |
| - byte[] cipherText = encryptText(plainText, secKey); |
28 |
| - String decryptedText = decryptText(cipherText, secKey); |
| 14 | + private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); |
29 | 15 |
|
30 |
| - System.out.println("Original Text:" + plainText); |
31 |
| - System.out.println("AES Key (Hex Form):" + bytesToHex(secKey.getEncoded())); |
32 |
| - System.out.println("Encrypted Text (Hex Form):" + bytesToHex(cipherText)); |
33 |
| - System.out.println("Descrypted Text:" + decryptedText); |
34 |
| - } |
| 16 | + /** |
| 17 | + * 1. Generate a plain text for encryption 2. Get a secret key (printed in hexadecimal form). In |
| 18 | + * actual use this must by encrypted and kept safe. The same key is required for decryption. |
| 19 | + */ |
| 20 | + public static void main(String[] args) throws Exception { |
| 21 | + String plainText = "Hello World"; |
| 22 | + SecretKey secKey = getSecretEncryptionKey(); |
| 23 | + byte[] cipherText = encryptText(plainText, secKey); |
| 24 | + String decryptedText = decryptText(cipherText, secKey); |
35 | 25 |
|
36 |
| - /** |
37 |
| - * gets the AES encryption key. In your actual programs, this should be safely stored. |
38 |
| - * |
39 |
| - * @return secKey (Secret key that we encrypt using it) |
40 |
| - * @throws NoSuchAlgorithmException (from KeyGenrator) |
41 |
| - */ |
42 |
| - public static SecretKey getSecretEncryptionKey() throws NoSuchAlgorithmException { |
43 |
| - KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES"); |
44 |
| - aesKeyGenerator.init(128); // The AES key size in number of bits |
45 |
| - return aesKeyGenerator.generateKey(); |
46 |
| - } |
| 26 | + System.out.println("Original Text:" + plainText); |
| 27 | + System.out.println("AES Key (Hex Form):" + bytesToHex(secKey.getEncoded())); |
| 28 | + System.out.println("Encrypted Text (Hex Form):" + bytesToHex(cipherText)); |
| 29 | + System.out.println("Descrypted Text:" + decryptedText); |
| 30 | + } |
47 | 31 |
|
48 |
| - /** |
49 |
| - * Encrypts plainText in AES using the secret key |
50 |
| - * |
51 |
| - * @return byteCipherText (The encrypted text) |
52 |
| - * @throws NoSuchPaddingException (from Cipher) |
53 |
| - * @throws NoSuchAlgorithmException (from Cipher) |
54 |
| - * @throws InvalidKeyException (from Cipher) |
55 |
| - * @throws BadPaddingException (from Cipher) |
56 |
| - * @throws IllegalBlockSizeException (from Cipher) |
57 |
| - */ |
58 |
| - public static byte[] encryptText(String plainText, SecretKey secKey) |
59 |
| - throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, |
60 |
| - IllegalBlockSizeException, BadPaddingException { |
61 |
| - // AES defaults to AES/ECB/PKCS5Padding in Java 7 |
62 |
| - Cipher aesCipher = Cipher.getInstance("AES"); |
63 |
| - aesCipher.init(Cipher.ENCRYPT_MODE, secKey); |
64 |
| - return aesCipher.doFinal(plainText.getBytes()); |
65 |
| - } |
| 32 | + /** |
| 33 | + * gets the AES encryption key. In your actual programs, this should be safely stored. |
| 34 | + * |
| 35 | + * @return secKey (Secret key that we encrypt using it) |
| 36 | + * @throws NoSuchAlgorithmException (from KeyGenrator) |
| 37 | + */ |
| 38 | + public static SecretKey getSecretEncryptionKey() throws NoSuchAlgorithmException { |
| 39 | + KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES"); |
| 40 | + aesKeyGenerator.init(128); // The AES key size in number of bits |
| 41 | + return aesKeyGenerator.generateKey(); |
| 42 | + } |
66 | 43 |
|
67 |
| - /** |
68 |
| - * Decrypts encrypted byte array using the key used for encryption. |
69 |
| - * |
70 |
| - * @return plainText |
71 |
| - */ |
72 |
| - public static String decryptText(byte[] byteCipherText, SecretKey secKey) |
73 |
| - throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, |
74 |
| - IllegalBlockSizeException, BadPaddingException { |
75 |
| - // AES defaults to AES/ECB/PKCS5Padding in Java 7 |
76 |
| - Cipher aesCipher = Cipher.getInstance("AES"); |
77 |
| - aesCipher.init(Cipher.DECRYPT_MODE, secKey); |
78 |
| - byte[] bytePlainText = aesCipher.doFinal(byteCipherText); |
79 |
| - return new String(bytePlainText); |
80 |
| - } |
| 44 | + /** |
| 45 | + * Encrypts plainText in AES using the secret key |
| 46 | + * |
| 47 | + * @return byteCipherText (The encrypted text) |
| 48 | + * @throws NoSuchPaddingException (from Cipher) |
| 49 | + * @throws NoSuchAlgorithmException (from Cipher) |
| 50 | + * @throws InvalidKeyException (from Cipher) |
| 51 | + * @throws BadPaddingException (from Cipher) |
| 52 | + * @throws IllegalBlockSizeException (from Cipher) |
| 53 | + */ |
| 54 | + public static byte[] encryptText(String plainText, SecretKey secKey) |
| 55 | + throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, |
| 56 | + IllegalBlockSizeException, BadPaddingException { |
| 57 | + // AES defaults to AES/ECB/PKCS5Padding in Java 7 |
| 58 | + Cipher aesCipher = Cipher.getInstance("AES"); |
| 59 | + aesCipher.init(Cipher.ENCRYPT_MODE, secKey); |
| 60 | + return aesCipher.doFinal(plainText.getBytes()); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Decrypts encrypted byte array using the key used for encryption. |
| 65 | + * |
| 66 | + * @return plainText |
| 67 | + */ |
| 68 | + public static String decryptText(byte[] byteCipherText, SecretKey secKey) |
| 69 | + throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, |
| 70 | + IllegalBlockSizeException, BadPaddingException { |
| 71 | + // AES defaults to AES/ECB/PKCS5Padding in Java 7 |
| 72 | + Cipher aesCipher = Cipher.getInstance("AES"); |
| 73 | + aesCipher.init(Cipher.DECRYPT_MODE, secKey); |
| 74 | + byte[] bytePlainText = aesCipher.doFinal(byteCipherText); |
| 75 | + return new String(bytePlainText); |
| 76 | + } |
81 | 77 |
|
82 |
| - /** |
83 |
| - * Convert a binary byte array into readable hex form Old library is deprecated on OpenJdk 11 and |
84 |
| - * this is faster regarding other solution is using StringBuilder |
85 |
| - * |
86 |
| - * @return hexHash |
87 |
| - */ |
88 |
| - public static String bytesToHex(byte[] bytes) { |
89 |
| - char[] hexChars = new char[bytes.length * 2]; |
90 |
| - for (int j = 0; j < bytes.length; j++) { |
91 |
| - int v = bytes[j] & 0xFF; |
92 |
| - hexChars[j * 2] = HEX_ARRAY[v >>> 4]; |
93 |
| - hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; |
| 78 | + /** |
| 79 | + * Convert a binary byte array into readable hex form Old library is deprecated on OpenJdk 11 and |
| 80 | + * this is faster regarding other solution is using StringBuilder |
| 81 | + * |
| 82 | + * @return hexHash |
| 83 | + */ |
| 84 | + public static String bytesToHex(byte[] bytes) { |
| 85 | + char[] hexChars = new char[bytes.length * 2]; |
| 86 | + for (int j = 0; j < bytes.length; j++) { |
| 87 | + int v = bytes[j] & 0xFF; |
| 88 | + hexChars[j * 2] = HEX_ARRAY[v >>> 4]; |
| 89 | + hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; |
| 90 | + } |
| 91 | + return new String(hexChars); |
94 | 92 | }
|
95 |
| - return new String(hexChars); |
96 |
| - } |
97 | 93 | }
|
0 commit comments