1 public classAES {2 /**
3 * AES加密4 *5 *@paramplaintext 明文6 *@paramKey 密钥7 *@paramEncryptMode AES加密模式,CBC或ECB8 *@return该字符串的AES密文值9 */
10 public staticString 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(newBASE64Encoder().encode(encrypted));29 returnencryptedStr;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 *@paramcipertext 密文41 *@paramKey 密钥42 *@paramEncryptMode AES加密模式,CBC或ECB43 *@return该密文的明文44 */
45 public staticString 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 returnoriginalString;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 *@params 要进行MD5转换的字符串85 *@return该字符串的MD5值的8-24位86 */
87 public staticString 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 }