This example program shows how AES encryption and decryption can be done in Java. Please note that secret key and encrypted text is unreadable binary and hence in the following program we display it in hexadecimal format of the underlying bytes.
| 18 | * underlying bytes. |
| 19 | */ |
| 20 | public final class AESEncryption { |
| 21 | private AESEncryption() { |
| 22 | } |
| 23 | |
| 24 | private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); |
| 25 | private static Cipher aesCipher; |
| 26 | |
| 27 | /** |
| 28 | * 1. Generate a plain text for encryption 2. Get a secret key (printed in |
| 29 | * hexadecimal form). In actual use this must be encrypted and kept safe. |
| 30 | * The same key is required for decryption. |
| 31 | */ |
| 32 | public static void main(String[] args) throws Exception { |
| 33 | String plainText = "Hello World"; |
| 34 | SecretKey secKey = getSecretEncryptionKey(); |
| 35 | byte[] cipherText = encryptText(plainText, secKey); |
| 36 | String decryptedText = decryptText(cipherText, secKey); |
| 37 | |
| 38 | System.out.println("Original Text:" + plainText); |
| 39 | System.out.println("AES Key (Hex Form):" + bytesToHex(secKey.getEncoded())); |
| 40 | System.out.println("Encrypted Text (Hex Form):" + bytesToHex(cipherText)); |
| 41 | System.out.println("Decryption successful. Decrypted text matches original: " + decryptedText.equals(plainText)); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * gets the AES encryption key. In your actual programs, this should be |
| 46 | * safely stored. |
| 47 | * |
| 48 | * @return secKey (Secret key that we encrypt using it) |
| 49 | * @throws NoSuchAlgorithmException (from KeyGenrator) |
| 50 | */ |
| 51 | public static SecretKey getSecretEncryptionKey() throws NoSuchAlgorithmException { |
| 52 | KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES"); |
| 53 | aesKeyGenerator.init(128); // The AES key size in number of bits |
| 54 | return aesKeyGenerator.generateKey(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Encrypts plainText in AES using the secret key |
| 59 | * |
| 60 | * @return byteCipherText (The encrypted text) |
| 61 | * @throws NoSuchPaddingException (from Cipher) |
| 62 | * @throws NoSuchAlgorithmException (from Cipher) |
| 63 | * @throws InvalidKeyException (from Cipher) |
| 64 | * @throws BadPaddingException (from Cipher) |
| 65 | * @throws IllegalBlockSizeException (from Cipher) |
| 66 | */ |
| 67 | public static byte[] encryptText(String plainText, SecretKey secKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { |
| 68 | // AES defaults to AES/ECB/PKCS5Padding in Java 7 |
| 69 | aesCipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 70 | aesCipher.init(Cipher.ENCRYPT_MODE, secKey); |
| 71 | return aesCipher.doFinal(plainText.getBytes()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Decrypts encrypted byte array using the key used for encryption. |
| 76 | * |
| 77 | * @return plainText |
nothing calls this directly
no outgoing calls
no test coverage detected