Bouncy Castle Crypto APIs 是一個開源的輕量級Java 加密解密包,實現了JCE/JCA的provider,支持AES等多種加密解密算法。
詳情請見主頁:http://www.bouncycastle.org/java.html
本文的示例代碼使用了http://www.bouncycastle.org/download/bcprov-jdk16-139.jar
1)使用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();??
- ????????}??
- ??
- ????}??
- ??
- }??
?2)不使用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();??
- ????????}??
- ??
- ????}??
- ??
-
}?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
