Created
December 6, 2013 15:35
-
-
Save deckerego/7826638 to your computer and use it in GitHub Desktop.
Simple Native Encryption with Java 6
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
byte[] input = args[0].getBytes(); | |
byte[] key = args[1].getBytes(); | |
SecretKeySpec aesKey = new SecretKeySpec(key, "AES"); | |
Cipher cipher = Cipher.getInstance("AES", "SunJCE"); | |
System.out.println("Input : " + new String(input)); | |
cipher.init(Cipher.ENCRYPT_MODE, aesKey); | |
byte[] encryptedText = new byte[cipher.getOutputSize(input.length)]; | |
int encryptedTextLength = cipher.update(input, 0, input.length, encryptedText, 0); | |
encryptedTextLength += cipher.doFinal(encryptedText, encryptedTextLength); | |
System.out.println("Encrypted: " + new String(encryptedText)); | |
cipher.init(Cipher.DECRYPT_MODE, aesKey); | |
byte[] unencryptedText = new byte[cipher.getOutputSize(encryptedTextLength)]; | |
int unencryptedTextLength = cipher.update(encryptedText, 0, encryptedTextLength, unencryptedText, 0); | |
unencryptedTextLength += cipher.doFinal(unencryptedText, unencryptedTextLength); | |
System.out.println("Unencrypted: " + new String(unencryptedText)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment