java 下的 AES org.bouncycastle.crypto 包加密解密算法示例

来源:互联网 发布:化学反应软件 编辑:程序博客网 时间:2024/04/30 05:48


算法模式:CBC
填充模式:PKCS5
初始化向量IV:0x31, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x38, 0x27, 0x36, 0x35, 0x33, 0x23, 0x32, 0x33,IV值一定是128位的(16字节).
为提高加密性能,建议使用Bouncy Castle Inc.公司提供的算法包,以下样例中使用的就是该公司的算法包。其运算速度是jdk自带的10倍以上。

包下载链接,按照自己的jdk版本选择算法包:http://www.bouncycastle.org/latest_releases.html


主要代码:

private static final byte[] INIT_VECTOR = { 0x31, 0x37, 0x36, 0x35,                                        0x34, 0x33, 0x32, 0x31,                                        0x38, 0x27, 0x36, 0x35,                                        0x33, 0x23, 0x32, 0x33 };public static String encrypt(String content, String apiKey)throws Exception {if (apiKey == null) {throw new IllegalArgumentException("Key cannot be null!");}String encrypted = null;byte[] keyBytes = apiKey.getBytes();if (keyBytes.length != 32 && keyBytes.length != 24&& keyBytes.length != 16) {throw new IllegalArgumentException("Key length must be   128/192/256 bits!");}byte[] encryptedBytes = null;encryptedBytes = encrypt(content.getBytes(), keyBytes, INIT_VECTOR);encrypted = new String(Hex.encode(encryptedBytes));return encrypted;}public static String decrypt(String content, String apiKey)throws Exception {if (apiKey == null) {throw new IllegalArgumentException("Key cannot be null!");}String decrypted = null;byte[] encryptedContent = Hex.decode(content);byte[] keyBytes = apiKey.getBytes();byte[] decryptedBytes = null;if (keyBytes.length != 32 && keyBytes.length != 24&& keyBytes.length != 16) {throw new IllegalArgumentException("Key length must be   128/192/256 bits!");}decryptedBytes = decrypt(encryptedContent, keyBytes, INIT_VECTOR);decrypted = new String(decryptedBytes);return decrypted;}private static byte[] encrypt(byte[] plain, byte[] key, byte[] iv)throws Exception {PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key),iv);aes.init(true, ivAndKey);return cipherData(aes, plain);}private static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv)throws Exception {PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key),iv);aes.init(false, ivAndKey);return cipherData(aes, cipher);}private static byte[] cipherData(PaddedBufferedBlockCipher cipher,byte[] data) throws Exception {int minSize = cipher.getOutputSize(data.length);byte[] outBuf = new byte[minSize];int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);int length2 = cipher.doFinal(outBuf, length1);int actualLength = length1 + length2;byte[] result = new byte[actualLength];System.arraycopy(outBuf, 0, result, 0, result.length);return result;}






使用:


public static void main(String[] args) {try {String apiKey = "222b8f353b79afb361e27b3523967928";String content = "test12345";long start = System.currentTimeMillis();String encrypt = encrypt(content, apiKey);System.out.println(System.currentTimeMillis() - start + "   ms");System.out.println(encrypt);String decrypt = decrypt(encrypt, apiKey);System.out.println(decrypt);} catch (Exception e) {e.printStackTrace();}}