MD5加密

来源:互联网 发布:广电总局限制网络电视 编辑:程序博客网 时间:2024/06/07 04:43
import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.security.Security;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;public class MD5 {//定义 加密算法,可用DES,DESede,Blowfishprivate static String Algorithm = "DES";public static final String KEY_SHA = "SHA";public static final String KEY_MD5 = "MD5";private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5","6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };static boolean debug = false;static {Security.addProvider(new com.sun.crypto.provider.SunJCE());}/** * 生成密钥, 注意此步骤时间比较长 */public static byte[] getKey() throws Exception {KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);SecretKey deskey = keygen.generateKey();if (debug)System.out.println("生成密钥:" + byte2hex(deskey.getEncoded()));return deskey.getEncoded();}/** * * 函数功能说明:加密可以解密* 创建者名字和日期 :* 参数: @param input 加密前字符串* 参数: @param key* 参数: @return:加密数据* 参数: @throws Exception    * 返回值: byte[]   * @throws */public static byte[] encode(byte[] input, byte[] key) throws Exception {SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, Algorithm);if (debug) {System.out.println("加密前的二进串:" + byte2hex(input));System.out.println("加密前的字符串:" + new String(input));}Cipher c1 = Cipher.getInstance(Algorithm);c1.init(Cipher.ENCRYPT_MODE, deskey);byte[] cipherByte = c1.doFinal(input);if (debug)System.out.println("加密后的二进串:" + byte2hex(cipherByte));return cipherByte;}/** * * 函数功能说明:解密用encode加密的密钥* 创建者名字和日期 :* 参数: @param input 解密前字符串* 参数: @param key* 参数: @return:解密后数据* 参数: @throws Exception    * 返回值: byte[]   * @throws */public static byte[] decode(byte[] input, byte[] key) throws Exception {SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, Algorithm);if (debug)System.out.println("解密前的信息:" + byte2hex(input));Cipher c1 = Cipher.getInstance(Algorithm);c1.init(Cipher.DECRYPT_MODE, deskey);byte[] clearByte = c1.doFinal(input);if (debug) {System.out.println("解密后的二进串:" + byte2hex(clearByte));System.out.println("解密后的字符串:" + (new String(clearByte)));}return clearByte;}/** * 二进制转成16进制字符 * @param b * @return */private static String byteToHexString(byte b) {return hexDigits[(b & 0xf0) >> 4] + hexDigits[b & 0x0f];}/** * 转换字节数组为16进制字串 * @param b 字节数组 * @return 16进制字串 */private static String byteArrayToHexString(byte[] b) {StringBuffer buf = new StringBuffer();for (int i = 0; i < b.length; i++) {buf.append(byteToHexString(b[i]));}return buf.toString();}/** * * 函数功能说明:MD5加密方法* 创建者名字和日期 :* 参数: @param origin* 参数: @return    * 返回值: String   * @throws */public static String encryptMD5(String str) {String resultString = new String(str);try {MessageDigest md = MessageDigest.getInstance("MD5");resultString = byteArrayToHexString(md.digest(resultString.getBytes()));} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return resultString;}/** * md5()信息摘要, 不可逆 */public static byte[] md5(byte[] input) throws Exception {java.security.MessageDigest alg = java.security.MessageDigest.getInstance("MD5"); // or "SHA-1"if (debug) {System.out.println("摘要前的二进串:" + byte2hex(input));System.out.println("摘要前的字符串:" + new String(input));}alg.update(input);byte[] digest = alg.digest();if (debug)System.out.println("摘要后的二进串:" + byte2hex(digest));return digest;}/** * 字节码转换成16进制字符串 */public static String byte2hex(byte[] b) {String hs = "";String stmp = "";for (int n = 0; n < b.length; n++) {stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));if (stmp.length() == 1)hs = hs + "0" + stmp;elsehs = hs + stmp;if (n < b.length - 1)hs = hs + ":";}return hs.toUpperCase();}/** * MD5加密 *  * @param data * @return * @throws Exception */public static byte[] encryptMD5(byte[] data) throws Exception {MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);md5.update(data);return md5.digest();}/** * SHA加密 *  * @param data * @return * @throws Exception */public static byte[] encryptSHA(byte[] data) throws Exception {MessageDigest sha = MessageDigest.getInstance(KEY_SHA);sha.update(data);return sha.digest();}/** * SHA加密算法32 *  * @param inputStr * @return */public static String shaEncrypt(String inputStr) {byte[] inputData = inputStr.getBytes();String returnString = "";try {// 将二进制转换成十六进制字符串returnString = byte2hex(encryptSHA(inputData));} catch (Exception e) {e.printStackTrace();}return returnString;}@SuppressWarnings("unused")private static String encrypte256(String plainText, String algorithm) {algorithm = "SHA-256";MessageDigest md = null;try {md = MessageDigest.getInstance(algorithm);} catch (NoSuchAlgorithmException e) {e.printStackTrace();}md.update(plainText.getBytes());byte[] b = md.digest();StringBuilder output = new StringBuilder(32);for (int i = 0; i < b.length; i++) {String temp = Integer.toHexString(b[i] & 0xff);if (temp.length() < 2) {output.append("0");}output.append(temp);}return output.toString();}public static void main(String[] args) throws Exception {debug = true;//byte[] key = getKey(); byte[] key = "好好学习".getBytes();decode(encode("测试加密".getBytes(), key), key); //md5("测试加密".getBytes());//System.out.println(MD5Encode("21218cca77804d2ba1922c33e0151105"));//System.out.println(encryptMD5("143214125123"));//update am_user set loginpwd='44ffe44097bbce02fbaa42734e92ae04' where tag='1'/* * 摘要前的二进串:B2:E2:CA:D4:BC:D3:C3:DC   摘要前的字符串:测试加密   摘要后的二进串:DE:BE:ED:B7:1E:75:77:51:0E:BF:12:BE:03:2C:0F:B5   7d83cf144fdbf9972831401a657acf72 */}}

原创粉丝点击