使用md5进行加密解密
生活随笔
收集整理的這篇文章主要介紹了
使用md5进行加密解密
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用方法已經在下面了,需要使用自己調用就可以了,一般用于cookie加密
import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Objects;/*** Created by Tony on 2017/9/20.*/ public class SecretUtils {private static final byte[] codes = new byte[256];static {for (int i = 0; i < 256; i++)codes[i] = -1;for (int i = 'A'; i <= 'Z'; i++)codes[i] = (byte) (i - 'A');for (int i = 'a'; i <= 'z'; i++)codes[i] = (byte) (26 + i - 'a');for (int i = '0'; i <= '9'; i++)codes[i] = (byte) (52 + i - '0');codes['+'] = 62;codes['/'] = 63;}private static final char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();/*** 字符串加密以及解密函數** @param $string(string) 原文或者密文* @param $operation(string) 操作(ENCODE | DECODE), 默認為 DECODE* @param $key (string) 密鑰* @param $expiry(int) 密文有效期, 加密時候有效, 單位 秒,0 為永久有效* @return string 處理后的 原文或者 經過 base64_encode 處理后的密文* @example* $a = authCode('abc', 'ENCODE', 'key');* $b = authCode($a, 'DECODE', 'key');//$b(abc)* $a = authCode('abc', 'ENCODE', 'key', 3600);* $b = authCode('abc', 'DECODE', 'key');//在一個小時內,$b(abc),否則 $b 為空*/private static String authCode(String $string, String $operation, String $key,int $expiry) {if($string != null && $operation.equals("DECODE")){int index =$string.indexOf(".");while (index!=-1){StringBuilder builder = new StringBuilder($string);String s = $string.substring(index,index+2);switch (s) {case ".0":builder.replace(index,index+2, " ");break;case ".1":builder.replace(index,index+2, "=");break;case ".2":builder.replace(index,index+2, "+");break;case ".3":builder.replace(index,index+2, "/");break;}$string = builder.toString();index =$string.indexOf(".");}}int $ckey_length = 4; //note 隨機密鑰長度 取值 0-32;//note 加入隨機密鑰,可以令密文無任何規律,即便是原文和密鑰完全相同,加密結果也會每次不同,增大破解難度。//note 取值越大,密文變動規律越大,密文變化 = 16 的 $ckey_length 次方//note 當此值為 0 時,則不產生隨機密鑰$key = md5( $key!=null ? $key : "123456");assert $key != null;String $keya = md5(substr($key, 0, 16));String $keyb = md5(substr($key, 16, 16));String $keyc;if ($operation.equals("DECODE")) {$keyc = substr(Objects.requireNonNull($string), 0, $ckey_length);} else {$keyc = substr(md5(microtime()), -$ckey_length);}String $cryptKey = $keya + md5( $keya + $keyc);int $key_length = $cryptKey.length();$string = $operation.equals("DECODE") ? base64_decode(substr($string, $ckey_length)) : sprintf($expiry>0 ? $expiry + time() : 0)+substr(Objects.requireNonNull(md5($string + $keyb)), 0, 16)+$string;int $string_length = $string.length();StringBuilder $result1 = new StringBuilder();int[] $box = new int[256];for(int i=0;i<256;i++){$box[i] = i;}int[] $rndKey = new int[256];for(int $i = 0; $i <= 255; $i++) {$rndKey[$i] = $cryptKey.charAt($i % $key_length);}int $j=0;for(int $i = 0; $i < 256; $i++) {$j = ($j + $box[$i] + $rndKey[$i]) % 256;int $tmp = $box[$i];$box[$i] = $box[$j];$box[$j] = $tmp;}$j=0;int $a=0;for(int $i = 0; $i < $string_length; $i++) {$a = ($a + 1) % 256;$j = ($j + $box[$a]) % 256;int $tmp = $box[$a];$box[$a] = $box[$j];$box[$j] = $tmp;$result1.append((char)( ((int)$string.charAt($i)) ^ ($box[($box[$a] + $box[$j]) % 256])));}if($operation.equals("DECODE")) {String $result = $result1.substring(0, $result1.length());if((Integer.parseInt(substr($result, 0, 10)) == 0 || Long.parseLong(substr($result, 0, 10)) - time() > 0) && substr($result, 10, 16).equals(substr(Objects.requireNonNull(md5(substr($result, 26) + $keyb)), 0, 16))) {return substr($result, 26);} else {return "";}} else {String str = $keyc+base64_encode($result1.toString());str = str.replaceAll(" ",".0");str = str.replaceAll("=",".1");str = str.replaceAll("\\+",".2");str = str.replaceAll("/",".3");return str;}}private static String md5(String input){MessageDigest md;try {md = MessageDigest.getInstance("MD5");} catch (NoSuchAlgorithmException e) {return null;}return byte2hex(md.digest(input.getBytes()));}private static String md5(long input){return md5(String.valueOf(input));}private static String byte2hex(byte[] b) {StringBuilder hs = new StringBuilder();String stmp = "";for (byte value : b) {stmp = (Integer.toHexString(value & 0XFF));if (stmp.length() == 1)hs.append("0").append(stmp);elsehs.append(stmp);}return hs.toString();}private static String substr(String input,int begin, int length){return input.substring(begin, begin+length);}private static String substr(String input,int begin){if(begin>0){return input.substring(begin);}else{return input.substring(input.length()+ begin);}}private static long microtime(){return System.currentTimeMillis();}private static long time(){return System.currentTimeMillis()/1000;}private static String sprintf(long input){String temp = "0000000000"+input;return temp.substring(temp.length()-10);}private static String base64_decode(String input){try {return new String(decode(input.toCharArray()), StandardCharsets.ISO_8859_1);} catch (Exception e) {return e.getMessage();}}private static byte[] decode(char[] data) {int tempLen = data.length;for (char datum : data) {if ((datum > 255) || codes[datum] < 0)--tempLen;}int len = (tempLen / 4) * 3;if ((tempLen % 4) == 3)len += 2;if ((tempLen % 4) == 2)len += 1;byte[] out = new byte[len];int shift = 0;int accum = 0;int index = 0;for (char datum : data) {int value = (datum > 255) ? -1 : codes[datum];if (value >= 0) {accum <<= 6;shift += 6;accum |= value;if (shift >= 8) {shift -= 8;out[index++] = (byte) ((accum >> shift) & 0xff);}}}if (index != out.length) {throw new Error("Miscalculated data length (wrote " +index + " instead of " + out.length + ")");}return out;}private static String base64_encode(String input){try {return new String(encode(input.getBytes(StandardCharsets.ISO_8859_1)));} catch (Exception e) {return e.getMessage();}}private static char[] encode(byte[] data) {char[] out = new char[((data.length + 2) / 3) * 4];for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {boolean quad = false;boolean trip = false;int val = (0xFF & data[i]);val <<= 8;if ((i + 1) < data.length) {val |= (0xFF & data[i + 1]);trip = true;}val <<= 8;if ((i + 2) < data.length) {val |= (0xFF & data[i + 2]);quad = true;}out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];val >>= 6;out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];val >>= 6;out[index + 1] = alphabet[val & 0x3F];val >>= 6;out[index] = alphabet[val & 0x3F];}return out;}private static final String ENCODE = "ENCODE";private static final String DECODE = "DECODE";public static String expiryEncrypt(String $text,String $salt,Integer $expiry){return authCode($text,ENCODE,$salt,$expiry);}/*** @param $text 加密文本* @param $salt 加密密碼* @return String*/public static String encrypt(String $text,String $salt){return expiryEncrypt($text, $salt,0);}public static String decrypt(String $encData,String $salt){return authCode($encData,DECODE,$salt,0);} }總結
以上是生活随笔為你收集整理的使用md5进行加密解密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何基于场景设计产品-笔记(201604
- 下一篇: 201671010458 种兴达 实验三