[Java] java AES加密解密代码,key是16位 →→→→→进入此内容的聊天室

来自 , 2020-12-11, 写在 Java, 查看 104 次.
URL http://www.code666.cn/view/0a4dc6da
  1. import javax.crypto.*;
  2. import javax.crypto.spec.*;
  3.  
  4. public class MyAES {
  5.     public static void main(String[] args) throws Exception {
  6.         /*
  7.          * 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
  8.          */
  9.         String cKey = "1234567890abcDEF";
  10.         // 需要加密的字串
  11.         String cSrc = "LUZAIMOU";
  12.         // 加密
  13.         long lStart = System.currentTimeMillis();
  14. //        String enString = MyAES.encrypt(cSrc, cKey);
  15. //        System.out.println("加密后的字串是:" + enString);
  16.         long lUseTime = System.currentTimeMillis() - lStart;
  17.         System.out.println("加密耗时:" + lUseTime + "毫秒");
  18.         // 解密
  19.         lStart = System.currentTimeMillis();
  20. //        String DeString = MyAES.decrypt(enString, cKey);
  21. //        System.out.println("解密后的字串是:" + DeString);
  22.         lUseTime = System.currentTimeMillis() - lStart;
  23.         System.out.println("解密耗时:" + lUseTime + "毫秒");
  24.     }
  25.  
  26.     public static byte[] decrypt(byte[] sSrc, String sKey) throws Exception {
  27.         try {
  28.             // 判断Key是否正确
  29.             if (sKey == null) {
  30.                 System.out.print("Key为空null");
  31.                 return null;
  32.             }
  33.             // 判断Key是否为16位
  34.             if (sKey.length() != 16) {
  35.                 System.out.print("Key长度不是16位");
  36.                 return null;
  37.             }
  38.             byte[] raw = sKey.getBytes("ASCII");
  39.             SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  40.             Cipher cipher = Cipher.getInstance("AES");
  41.             cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  42.             byte[] encrypted1 = sSrc;
  43.             try {
  44.                 byte[] original = cipher.doFinal(encrypted1);
  45.                 return original;
  46.             } catch (Exception e) {
  47.                 System.out.println(e.toString());
  48.                 return null;
  49.             }
  50.         } catch (Exception ex) {
  51.             System.out.println(ex.toString());
  52.             return null;
  53.         }
  54.     }
  55.  
  56.     // 判断Key是否正确
  57.     public static byte[] encrypt(byte[] sSrc, String sKey) throws Exception {
  58.         if (sKey == null) {
  59.             System.out.print("Key为空null");
  60.             return null;
  61.         }
  62.         // 判断Key是否为16位
  63.         if (sKey.length() != 16) {
  64.             System.out.print("Key长度不是16位");
  65.             return null;
  66.         }
  67.         byte[] raw = sKey.getBytes("ASCII");
  68.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  69.         Cipher cipher = Cipher.getInstance("AES");
  70.         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  71.         byte[] encrypted = cipher.doFinal(sSrc);
  72.         return encrypted;
  73.     }
  74.  
  75.     public static byte[] parseHexStr2Byte(String strhex) {
  76.         if (strhex == null) {
  77.             return null;
  78.         }
  79.         int l = strhex.length();
  80.         if (l % 2 == 1) {
  81.             return null;
  82.         }
  83.         byte[] b = new byte[l / 2];
  84.         for (int i = 0; i != l / 2; i++) {
  85.             b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
  86.                     16);
  87.         }
  88.         return b;
  89.     }
  90.  
  91.     public static String parseByte2HexStr(byte[] b) {
  92.         String hs = "";
  93.         String stmp = "";
  94.         for (int n = 0; n < b.length; n++) {
  95.             stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
  96.             if (stmp.length() == 1) {
  97.                 hs = hs + "0" + stmp;
  98.             } else {
  99.                 hs = hs + stmp;
  100.             }
  101.         }
  102.         return hs.toUpperCase();
  103.     }
  104. }
  105. //java/5888

回复 "java AES加密解密代码,key是16位"

这儿你可以回复上面这条便签

captcha