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();}}
阅读全文
0 0
- java 下的 AES org.bouncycastle.crypto 包加密解密算法示例
- java 下的 AES javax.crypto包 加密解密算法示例
- C# 引用Org.BouncyCastle.Crypto 签名&解密
- Java AES 加密 解密 示例
- Java AES加密解密示例
- AES加密解密算法的Java实现
- Java加密解密算法-AES加密解密
- crypto前台AES加密,后台AES解密
- Java AES加密解密算法
- Aes加密/解密示例
- Java的AES加密解密
- JAVA实现AES的加密和解密算法
- JAVA 下的 pgp加密解密示例
- AES加密/解密算法
- AES加密解密算法
- JAVA AES对称加密解密算法
- java AES 128 位加密解密算法
- AES加密解密算法Java实现
- SQL For Test
- 合并两个有序的单链表
- iOS积累-iOS图片填充UIImageView(contentMode)
- UI测试常见bug
- TJU训练赛—D
- java 下的 AES org.bouncycastle.crypto 包加密解密算法示例
- Opencv在Release和Debug不同模式下的配置
- Java class 文件信息
- spark on yarn cluster 与 client 模式的区别
- HTML小结及css基础属性
- 未来币nxt 启动分析(1)
- 轮播图的实现原理
- Mysql 忘记root密码的解决方法
- 监控运行中jar包的jvm信息