Skip to content

Instantly share code, notes, and snippets.

@deckerego
Created December 6, 2013 15:35
Show Gist options
  • Save deckerego/7826638 to your computer and use it in GitHub Desktop.
Save deckerego/7826638 to your computer and use it in GitHub Desktop.
Simple Native Encryption with Java 6
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