package com.test.test1.util; import java.security.MessageDigest; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class MD5 { //md5加密 public static String string2MD5(String inStr){ MessageDigest md5 = null; try{ md5 = MessageDigest.getInstance("MD5"); }catch (Exception e){ System.out.println(e.toString()); e.printStackTrace(); return ""; } char[] charArray = inStr.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++){ byteArray[i] = (byte) charArray[i]; } byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++){ int val = ((int) md5Bytes[i]) & 0xff; if (val < 16){ hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } /** * 加密解密算法 执行一次加密,两次解密 */ public static String convertMD5(String inStr){ char[] a = inStr.toCharArray(); for (int i = 0; i < a.length; i++){ a[i] = (char) (a[i] ^ 't'); } String s = new String(a); return s; } /** * AES加密算法 */ public static byte[] encrypt(String content, String password) { try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128, new SecureRandom(password.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); byte[] byteContent = content.getBytes("utf-8"); cipher.init(1, key); return cipher.doFinal(byteContent); } catch (Exception e) { e.printStackTrace(); } return null; } /** * AES解密算法 */ public static byte[] decrypt(byte[] content, String password) { try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128, new SecureRandom(password.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(2, key); return cipher.doFinal(content); } catch (Exception e) { e.printStackTrace(); } return null; } private static String aesKey="16位密钥"; private static String deskey = "任意密钥"; //AES加密PKCS5Padding public static String encryptAES(String source) throws Exception{ SecretKeySpec skeySpec = new SecretKeySpec(aesKey.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(source.getBytes("utf8")); String data=Base64.encodeBase64String(encrypted); return data; } //AES加密并转换为16进制字符,对应C#PKcs7padding加密 public static String encryptAESTo16(String source) throws Exception{ SecretKeySpec skeySpec = new SecretKeySpec(("16位密钥").getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(source.getBytes("utf8")); StringBuilder sb = new StringBuilder(encrypted.length * 2); for (byte b : encrypted) { sb.append(String.format("%02X", new Integer(b & 0xff))); } return sb.toString(); } //AES解密PKCS5Padding public static String decryptAES(String encData) throws Exception{ SecretKeySpec skeySpec = new SecretKeySpec(aesKey.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decrypted = cipher.doFinal(Base64.decodeBase64(encData)); String json=new String(decrypted,"utf8"); return json; } /** * DES加密 */ public static String encodeDES(String datasource){ try{ SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(deskey.getBytes()); //创建一个密匙工厂,然后用它把DESKeySpec转换成 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); //Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); //用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, random); //现在,获取数据并加密 return Base64.encodeBase64String(cipher.doFinal(datasource.getBytes())); }catch(Throwable e){ e.printStackTrace(); return null; } } }
/** * DES解密 */ public String decode(String src) throws Exception { // DES算法要求有一个可信任的随机数源 SecureRandom random = new SecureRandom(); // 创建一个DESKeySpec对象 DESKeySpec desKey = new DESKeySpec(deskey.getBytes()); // 创建一个密匙工厂 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 将DESKeySpec对象转换成SecretKey对象 SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, random); // 真正开始解密操作 return IOUtils.toString(cipher.doFinal(Base64.decodeBase64(src)),"UTF-8"); }