Java实现AES加密解密

    原文地址:https://blog.csdn.net/spidermansun/article/details/84942010

  1 public class AES {
  2     /**
  3      * AES加密
  4      *
  5      * @param plaintext 明文
  6      * @param Key 密钥
  7      * @param EncryptMode AES加密模式,CBC或ECB
  8      * @return 该字符串的AES密文值
  9      */
 10     public static String AES_Encrypt(Object plaintext, String Key,String EncryptMode) {
 11         String PlainText=null;
 12         try {
 13             PlainText=plaintext.toString();
 14             if (Key == null) {
 15                 return null;
 16             }
 17             Key = getMD5(Key);
 18             byte[] raw = Key.getBytes("utf-8");
 19             SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
 20             Cipher cipher = Cipher.getInstance("AES/"+EncryptMode+"/PKCS5Padding");
 21             if(EncryptMode=="ECB") {
 22                 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
 23             }else {
 24                 IvParameterSpec iv = new IvParameterSpec(Key.getBytes("utf-8"));//使用CBC模式,需要一个向量iv,可增加加密算法的强度
 25                 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
 26             }
 27             byte[] encrypted = cipher.doFinal(PlainText.getBytes("utf-8"));
 28             String encryptedStr=new String(new BASE64Encoder().encode(encrypted));
 29             return encryptedStr;
 30             //return new String(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
 31         } catch (Exception ex) {
 32             System.out.println(ex.toString());
 33             return null;
 34         }
 35     }
 36  
 37     /**
 38      * AES解密
 39      *
 40      * @param cipertext 密文
 41      * @param Key 密钥
 42      * @param EncryptMode AES加密模式,CBC或ECB
 43      * @return 该密文的明文
 44      */
 45     public static String AES_Decrypt(Object cipertext, String Key,String EncryptMode) {
 46         String CipherText=null;
 47         try {
 48             CipherText=cipertext.toString();
 49             // 判断Key是否正确
 50             if (Key == null) {
 51                 //System.out.print("Key为空null");
 52                 return null;
 53             }
 54             Key=getMD5(Key);
 55             byte[] raw = Key.getBytes("utf-8");
 56             SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
 57             Cipher cipher=Cipher.getInstance("AES/"+EncryptMode+"/PKCS5Padding");
 58             if(EncryptMode=="ECB") {
 59                 cipher.init(Cipher.DECRYPT_MODE, skeySpec);
 60             }
 61             else
 62             {
 63                 IvParameterSpec iv = new IvParameterSpec(Key.getBytes("utf-8"));//使用CBC模式,需要一个向量iv,可增加加密算法的强度
 64                 cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
 65             }
 66             byte[] encrypted1 = new BASE64Decoder().decodeBuffer(CipherText);//先用base64解密
 67             //byte[] encrypted1 = CipherText.getBytes();
 68             try {
 69                 byte[] original = cipher.doFinal(encrypted1);
 70                 String originalString = new String(original,"utf-8");
 71                 return originalString;
 72             } catch (Exception e) {
 73                 System.out.println(e.toString());
 74                 return null;
 75             }
 76         } catch (Exception ex) {
 77             System.out.println(ex.toString());
 78             return null;
 79         }
 80     }
 81     /**
 82      * 进行MD5加密
 83      *
 84      * @param s 要进行MD5转换的字符串
 85      * @return 该字符串的MD5值的8-24位
 86      */
 87     public static String getMD5(String s){
 88         char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
 89  
 90         try {
 91             byte[] btInput = s.getBytes();
 92             // 获得MD5摘要算法的 MessageDigest 对象
 93             MessageDigest mdInst = MessageDigest.getInstance("MD5");
 94             // 使用指定的字节更新摘要
 95             mdInst.update(btInput);
 96             // 获得密文
 97             byte[] md = mdInst.digest();
 98             // 把密文转换成十六进制的字符串形式
 99             int j = md.length;
100             char str[] = new char[j * 2];
101             int k = 0;
102             for (int i = 0; i < j; i++) {
103                 byte byte0 = md[i];
104                 str[k++] = hexDigits[byte0 >>> 4 & 0xf];
105                 str[k++] = hexDigits[byte0 & 0xf];
106             }
107             return new String(str).substring(8,24);
108         } catch (Exception e) {
109             e.printStackTrace();
110             return null;
111         }
112     }
113 }
View Code
原文地址:https://www.cnblogs.com/CYCLearning/p/12187766.html