亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

用Bouncy Castle實現AES-128-CBC加密解密

系統 1967 0

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加密解密

Java代碼 ? 收藏代碼
  1. package ?com.albertsong.aes;??
  2. ??
  3. import ?java.security.Key;??
  4. import ?java.security.Security;??
  5. ??
  6. import ?javax.crypto.Cipher;??
  7. import ?javax.crypto.spec.IvParameterSpec;??
  8. import ?javax.crypto.spec.SecretKeySpec;??
  9. ??
  10. import ?org.bouncycastle.jce.provider.BouncyCastleProvider;??
  11. import ?org.bouncycastle.util.encoders.Hex;??
  12. ??
  13. /** ?
  14. ?*?@author?Albert ?
  15. ?*?@version?1.0 ?
  16. ?*? ?
  17. ?*/ ??
  18. public ? class ?AESWithJCE?{??
  19. ??
  20. ???? /** ?
  21. ?????*?@param?args ?
  22. ?????*/ ??
  23. ???? public ? static ? void ?main(String[]?args)?{??
  24. ???????? byte []?keybytes?=?{? 0x31 ,? 0x32 ,? 0x33 ,? 0x34 ,? 0x35 ,? 0x36 ,? 0x37 ,? 0x38 ,??
  25. ???????????????? 0x31 ,? 0x32 ,? 0x33 ,? 0x34 ,? 0x35 ,? 0x36 ,? 0x37 ,? 0x38 ?};??
  26. ???????? byte []?iv?=?{? 0x38 ,? 0x37 ,? 0x36 ,? 0x35 ,? 0x34 ,? 0x33 ,? 0x32 ,? 0x31 ,? 0x38 ,??
  27. ???????????????? 0x37 ,? 0x36 ,? 0x35 ,? 0x34 ,? 0x33 ,? 0x32 ,? 0x31 ?};??
  28. ????????String?content?= "TEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;??
  29. ????????System.out.println( "Original?content:" );??
  30. ????????System.out.println(content);??
  31. ???????? try ?{??
  32. ????????????Security.addProvider( new ?BouncyCastleProvider());??
  33. ????????????Key?key?=? new ?SecretKeySpec(keybytes,? "AES" );??
  34. ????????????Cipher?in?=?Cipher.getInstance( "AES/CBC/PKCS7Padding" , "BC" );??
  35. ????????????in.init(Cipher.ENCRYPT_MODE,?key,? new ?IvParameterSpec(iv));??
  36. ???????????? byte []?enc?=?in.doFinal(content.getBytes());??
  37. ????????????System.out.println( "Encrypted?Content:" );??
  38. ????????????System.out.println( new ?String(Hex.encode(enc)));??
  39. ??????????????
  40. ????????????Cipher?out?=?Cipher.getInstance( "AES/CBC/PKCS7Padding" ,? "BC" );??
  41. ????????????out.init(Cipher.DECRYPT_MODE,?key,? new ?IvParameterSpec(iv));??
  42. ???????????? byte []?dec?=?out.doFinal(enc);??
  43. ????????????System.out.println( "Decrypted?Content:" );??
  44. ????????????System.out.println( new ?String(dec));??
  45. ????????}? catch ?(Exception?ex)?{??
  46. ????????????ex.printStackTrace();??
  47. ????????}??
  48. ??
  49. ????}??
  50. ??
  51. }??

?2)不使用JCE的AES-128-CBC加密解密,可以用于J2ME程序中。

Java代碼 ? 收藏代碼
  1. package ?com.albertsong.aes;??
  2. ??
  3. import ?org.bouncycastle.crypto.BufferedBlockCipher;??
  4. import ?org.bouncycastle.crypto.engines.AESFastEngine;??
  5. import ?org.bouncycastle.crypto.modes.CBCBlockCipher;??
  6. import ?org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;??
  7. import ?org.bouncycastle.crypto.params.KeyParameter;??
  8. import ?org.bouncycastle.crypto.params.ParametersWithIV;??
  9. import ?org.bouncycastle.util.encoders.Hex;??
  10. ??
  11. /** ?
  12. ?*?@author?Albert ?
  13. ?*?@version?1.0 ?
  14. ?* ?
  15. ?*/ ??
  16. public ? class ?AESWithoutJCE?{??
  17. ??
  18. ???? /** ?
  19. ?????*?@param?args ?
  20. ?????*/ ??
  21. ???? public ? static ? void ?main(String[]?args)?{??
  22. ???????? byte []?keybytes?=?{? 0x31 ,? 0x32 ,? 0x33 ,? 0x34 ,? 0x35 ,? 0x36 ,? 0x37 ,? 0x38 ,??
  23. ???????????????? 0x31 ,? 0x32 ,? 0x33 ,? 0x34 ,? 0x35 ,? 0x36 ,? 0x37 ,? 0x38 ?};??
  24. ???????? byte []?iv?=?{? 0x38 ,? 0x37 ,? 0x36 ,? 0x35 ,? 0x34 ,? 0x33 ,? 0x32 ,? 0x31 ,? 0x38 ,??
  25. ???????????????? 0x37 ,? 0x36 ,? 0x35 ,? 0x34 ,? 0x33 ,? 0x32 ,? 0x31 ?};??
  26. ????????String?content?= "TEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;??
  27. ????????System.out.println( "Original?content:" );??
  28. ????????System.out.println(content);??
  29. ???????? try ?{??
  30. ????????????BufferedBlockCipher?engine?=? new ?PaddedBufferedBlockCipher( new ?CBCBlockCipher( new ?AESFastEngine()));??
  31. ????????????engine.init( true ,? new ?ParametersWithIV( new ?KeyParameter(keybytes),iv));??
  32. ???????????? byte []?enc?=? new ? byte [engine.getOutputSize(content.getBytes().length)];??
  33. ???????????? int ?size1?=?engine.processBytes(content.getBytes(),? 0 ,?content.getBytes().length,?enc,? 0 );??
  34. ???????????? int ?size2?=?engine.doFinal(enc,?size1);??
  35. ????????????System.out.println( "size2?=" +size2);??
  36. ???????????? byte []?encryptedContent?= new ? byte [size1+size2];??
  37. ????????????System.arraycopy(enc,? 0 ,?encryptedContent,? 0 ,?encryptedContent.length);??
  38. ????????????System.out.println( "Encrypted?Content:" );??
  39. ????????????System.out.println( new ?String(Hex.encode(encryptedContent)));??
  40. ??????????????
  41. ??????????????
  42. ????????????engine.init( false ,? new ?ParametersWithIV( new ?KeyParameter(keybytes),iv));??
  43. ???????????? byte []?dec?=? new ? byte [engine.getOutputSize(encryptedContent.length)];??
  44. ????????????size1?=?engine.processBytes(encryptedContent,? 0 ,?encryptedContent.length,?dec,? 0 );??
  45. ????????????size2?=?engine.doFinal(dec,?size1);??
  46. ????????????System.out.println( "size2?=" +size2);??
  47. ???????????? byte []?decryptedContent?= new ? byte [size1+size2];??
  48. ????????????System.arraycopy(dec,? 0 ,?decryptedContent,? 0 ,?decryptedContent.length);??
  49. ????????????System.out.println( "Decrypted?Content:" );??
  50. ????????????System.out.println( new ?String(decryptedContent));??
  51. ??
  52. ????????}? catch ?(Exception?ex)?{??
  53. ????????????ex.printStackTrace();??
  54. ????????}??
  55. ??
  56. ????}??
  57. ??
  58. }?

用Bouncy Castle實現AES-128-CBC加密解密


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成视频 | 国产小视频在线观看免费 | 国产成人久久 | 国产福利一区视频 | 97久久久久国产精品嫩草影院 | 亚洲第一综合网站 | 99久久免费国产精品特黄 | 四虎.com| 99综合色 | 香蕉国产人午夜视频在线观看 | 精品国产成人 | 一级理论片免费观看在线 | 免费福利在线 | 四虎网址大全 | 永久黄网站色视频免费观看 | 七月婷婷精品视频在线观看 | 亚洲欧美日本国产综合在线 | 免费欧美在线视频 | 999精品免费视频 | 美女啪啪免费网站 | 狠狠综合视频精品播放 | 国产精品久久久久久久久鸭 | 久久国产精品久久 | 一本一本久久a久久精品综合麻豆 | 久久久精品国产四虎影视 | 中文字幕人成不卡一区 | 奇米影视久久777中文字幕 | 亚洲精品一区国产二区 | 五月情婷婷 | 国产精品久久久久久久伊一 | 久久精品国产亚洲欧美 | 国产精品一区视频 | 久久爱伊人一区二区三区小说 | 午夜欧美福利视频 | 激情综合网五月激情 | 狠狠色狠狠色很很综合很久久 | 国产日韩欧美在线观看不卡 | 黄色一级片免费网站 | 欧美成人观看视频在线 | 亚洲国产成人超福利久久精品 | 四虎家庭影院 |