Created
August 18, 2011 02:18
-
-
Save simple/1153147 to your computer and use it in GitHub Desktop.
AES sample code
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
public class AesTest { | |
@Test | |
public void stringCanBeEncripted() throws NoSuchAlgorithmException, NoSuchProviderException, | |
NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException { | |
String original = "Kiwiple Inc."; | |
AesToy aes = new AesToy(); | |
String encrypted = aes.encrypt(original); | |
System.out.println("encrypted: " + encrypted); | |
String decrypted = aes.decrypt(encrypted); | |
assertEquals(decrypted, original); | |
} | |
} | |
public class AesToy { | |
private SecretKeySpec key; | |
private Cipher cipher; | |
public AesToy() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException { | |
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); | |
byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, | |
0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; | |
key = new SecretKeySpec(keyBytes, "AES"); | |
cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); | |
} | |
public String encrypt(String original) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException, | |
InvalidKeyException { | |
byte[] orig = original.getBytes(); | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
byte[] cipherText = new byte[cipher.getOutputSize(orig.length)]; | |
int ctLength = cipher.update(orig, 0, orig.length, cipherText, 0); | |
ctLength += cipher.doFinal(cipherText, ctLength); | |
String encoded; | |
try { | |
encoded = encode(Arrays.copyOfRange(cipherText, 0, ctLength)); | |
} catch (UnsupportedEncodingException e) { | |
encoded = ""; | |
} | |
return encoded; | |
} | |
public String decrypt(String encryptedString) throws ShortBufferException, InvalidKeyException, | |
IllegalBlockSizeException, BadPaddingException { | |
cipher.init(Cipher.DECRYPT_MODE, key); | |
byte[] encrypted = decode(encryptedString); | |
byte[] plainText = new byte[cipher.getOutputSize(encrypted.length)]; | |
int ptLength = cipher.update(encrypted, 0, encrypted.length, plainText, 0); | |
ptLength += cipher.doFinal(plainText, ptLength); | |
return new String(Arrays.copyOfRange(plainText, 0, ptLength)); | |
} | |
private String encode(byte[] bytes) throws UnsupportedEncodingException { | |
byte[] encoded = Base64.encodeBase64(bytes); | |
return new String(encoded); | |
} | |
private byte[] decode(String str) { | |
byte[] encoded = str.getBytes(); | |
return Base64.decodeBase64(encoded); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bouncy Castle Crypto API http://www.bouncycastle.org/latest_releases.html
commons codec http://commons.apache.org/codec/