AES-ECB-128加密算法

要加密或解密的数据必须是16的倍数。
Java代码 复制代码 收藏代码
  1. package com.hx.dlms.cipher;
  2. import java.security.InvalidKeyException;
  3. import java.security.NoSuchAlgorithmException;
  4. import javax.crypto.BadPaddingException;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.IllegalBlockSizeException;
  7. import javax.crypto.NoSuchPaddingException;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import cn.hexing.fk.utils.HexDump;
  10. public class AESECB128 {
  11. private static final byte[] staticKey = HexDump.toByteBuffer("00000000000000000000000000000000").array();
  12. public static byte[] encrypt(byte[] plainText) {
  13. return encrypt(plainText,staticKey);
  14. }
  15. /**
  16. * 加密
  17. *
  18. * @param content
  19. * 需要加密的内容
  20. * @param enckey
  21. * 加密密码
  22. * @return
  23. */
  24. public static byte[] encrypt(byte[] content, byte[] enckey) {
  25. try {
  26. SecretKeySpec key = new SecretKeySpec(enckey, "AES");
  27. Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");// 创建密码器
  28. cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
  29. byte[] result = cipher.doFinal(content);
  30. return result; // 加密
  31. } catch (NoSuchAlgorithmException e) {
  32. e.printStackTrace();
  33. } catch (NoSuchPaddingException e) {
  34. e.printStackTrace();
  35. } catch (InvalidKeyException e) {
  36. e.printStackTrace();
  37. } catch (IllegalBlockSizeException e) {
  38. e.printStackTrace();
  39. } catch (BadPaddingException e) {
  40. e.printStackTrace();
  41. }
  42. return null;
  43. }
  44. public static byte[] decrypt(byte[] cipherText) {
  45. return decrypt(cipherText,staticKey);
  46. }
  47. /**
  48. * 解密
  49. *
  50. * @param content
  51. * 待解密内容
  52. * @param deckey
  53. * 解密密钥
  54. * @return
  55. */
  56. public static byte[] decrypt(byte[] content, byte[] deckey) {
  57. try {
  58. SecretKeySpec key = new SecretKeySpec(deckey, "AES");
  59. Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");// 创建密码器
  60. cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
  61. byte[] result = cipher.doFinal(content);
  62. return result; // 加密
  63. } catch (NoSuchAlgorithmException e) {
  64. e.printStackTrace();
  65. } catch (NoSuchPaddingException e) {
  66. e.printStackTrace();
  67. } catch (InvalidKeyException e) {
  68. e.printStackTrace();
  69. } catch (IllegalBlockSizeException e) {
  70. e.printStackTrace();
  71. } catch (BadPaddingException e) {
  72. e.printStackTrace();
  73. }
  74. return null;
  75. }
  76. public static void main(String[] args) {
  77. String content = "6789:;<=>?@AB012";
  78. String password = "00000000000000000000000000000000";
  79. byte[] mykey = HexDump.toByteBuffer(password).array();
  80. // 加密
  81. System.out.println("加密前:" + content);
  82. byte[] encryptResult = encrypt(content.getBytes(), mykey);
  83. System.out.println("cipher text: " + HexDump.hexDumpCompact(encryptResult,0,encryptResult.length));
  84. // 解密
  85. byte[] decryptResult = decrypt(encryptResult, mykey);
  86. System.out.println("解密后:" + new String(decryptResult));
  87. }
  88. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值