Bouncy Castle是一种用于Java平台的开放源码的轻量级密码术包。它支持大量的密码术算法,并提供 JCE 1.2.1 的实现。Bouncy Castle是轻量级的,从J2SE 1.4到J2ME(包括MIDP)平台,它都可以运行。它是在MIDP上运行的唯一完整的密码术包。jar包下载:http:/ /www.bouncycastle.org/download/bcprov- jdk16-139.jar代码来自:http://albertsong.iteye.com/blog/196273
AESWithJCE.java
//使用JCE的AES-128-CBC加密解密 package com.albertsong.aes; import java.security.Key; import java.security.Security; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Hex; /** * @author Albert * @version 1.0 * */ public class AESWithJCE { /** * @param args */ public static void main(String[] args) { byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 }; byte[] iv = { 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31 }; String content ="TEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; System.out.println("Original content:"); System.out.println(content); try { Security.addProvider(new BouncyCastleProvider()); Key key = new SecretKeySpec(keybytes, "AES"); Cipher in = Cipher.getInstance("AES/CBC/PKCS7Padding","BC"); in.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); byte[] enc = in.doFinal(content.getBytes()); System.out.println("Encrypted Content:"); System.out.println(new String(Hex.encode(enc))); Cipher out = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); out.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); byte[] dec = out.doFinal(enc); System.out.println("Decrypted Content:"); System.out.println(new String(dec)); } catch (Exception ex) { ex.printStackTrace(); } } }
AESWithoutJCE.java
//不使用JCE的AES-128-CBC加密解密,可以用于J2ME程序中。 package com.albertsong.aes; import org.bouncycastle.crypto.BufferedBlockCipher; import org.bouncycastle.crypto.engines.AESFastEngine; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.ParametersWithIV; import org.bouncycastle.util.encoders.Hex; /** * @author Albert * @version 1.0 * */ public class AESWithoutJCE { /** * @param args */ public static void main(String[] args) { byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 }; byte[] iv = { 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31 }; String content ="TEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; System.out.println("Original content:"); System.out.println(content); try { BufferedBlockCipher engine = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); engine.init(true, new ParametersWithIV(new KeyParameter(keybytes),iv)); byte[] enc = new byte[engine.getOutputSize(content.getBytes().length)]; int size1 = engine.processBytes(content.getBytes(), 0, content.getBytes().length, enc, 0); int size2 = engine.doFinal(enc, size1); System.out.println("size2 ="+size2); byte[] encryptedContent =new byte[size1+size2]; System.arraycopy(enc, 0, encryptedContent, 0, encryptedContent.length); System.out.println("Encrypted Content:"); System.out.println(new String(Hex.encode(encryptedContent))); engine.init(false, new ParametersWithIV(new KeyParameter(keybytes),iv)); byte[] dec = new byte[engine.getOutputSize(encryptedContent.length)]; size1 = engine.processBytes(encryptedContent, 0, encryptedContent.length, dec, 0); size2 = engine.doFinal(dec, size1); System.out.println("size2 ="+size2); byte[] decryptedContent =new byte[size1+size2]; System.arraycopy(dec, 0, decryptedContent, 0, decryptedContent.length); System.out.println("Decrypted Content:"); System.out.println(new String(decryptedContent)); } catch (Exception ex) { ex.printStackTrace(); } } }
收藏
0人收藏
- 2012-11-25 17:10:41C# 实现的 AES 加密算法 by 自由魂
- 2012-11-06 22:45:02JAVA 实现MD5加密代码 by 古殇
- 2014-06-04 09:52:35JavaScript AES加密解密源代码 by GauSir
- 2014-07-25 12:36:55java AES加密解密代码,key是16位 by 香格里拉登
- 2014-10-20 14:13:25golang在CBC模式下的AES加密 by 好好学习啊
- 2013-06-25 16:13:51Blowfish加密算法 by Koon.LY
- 2014-07-10 21:54:06java AES加密解密代码,key是16位 by clt
- 2013-02-11 15:25:39C# 实现的 AES 加密算法 by 熬特洛
- 2014-03-10 09:25:31C#完全实现AES算法加密解密函数 by did0602
- 2015-06-25 09:00:37JAVA的AES加密步骤解释 by JustForFly
- 2017-12-11 17:37:06表变量,代替临时表 by xuleaper
相关聚客文章