public class JM {public static void main(String[] args) {String a = AuthCodeUtil.authcodeEncode(\"abcdefg\", \"123\");System.out.println(a);System.out.println(AuthCodeUtil.authcodeDecode(a, \"123\"));}}//function authcode($string, $operation = \'DECODE\', $key = \'\', $expiry = 0) {// $ckey_length = 4;// $key = md5($key != \'\' ? $key : \'globalkey\');// $keya = md5(substr($key, 0, 16));// $keyb = md5(substr($key, 16, 16));// $keyc = $ckey_length ? ($operation == \'DECODE\' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : \'\';//// $cryptkey = $keya.md5($keya.$keyc);// $key_length = strlen($cryptkey);//// $string = $operation == \'DECODE\' ? base64_decode(substr($string, $ckey_length)) : sprintf(\'%010d\', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;// $string_length = strlen($string);//// $result = \'\';// $box = range(0, 255);//// $rndkey = array();// for($i = 0; $i <= 255; $i++) {// $rndkey[$i] = ord($cryptkey[$i % $key_length]);// }//// for($j = $i = 0; $i < 256; $i++) {// $j = ($j + $box[$i] + $rndkey[$i]) % 256;// $tmp = $box[$i];// $box[$i] = $box[$j];// $box[$j] = $tmp;// }//// for($a = $j = $i = 0; $i < $string_length; $i++) {// $a = ($a + 1) % 256;// $j = ($j + $box[$a]) % 256;// $tmp = $box[$a];// $box[$a] = $box[$j];// $box[$j] = $tmp;// $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));// }//// if($operation == \'DECODE\') {// if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {// return substr($result, 26);// } else {// return \'\';// }// } else {// return $keyc.str_replace(\'=\', \'\', base64_encode($result));// }//}
[/code]
import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Calendar;import java.util.Random;import Decoder.BASE64Decoder;import Decoder.BASE64Encoder;public class AuthCodeUtil {private static BASE64Encoder encoder = new BASE64Encoder();private static BASE64Decoder decoder = new BASE64Decoder();public enum DiscuzAuthcodeMode {Encode, Decode};public static String CutString(String str, int startIndex, int length){if (startIndex >= 0) {if (length < 0) {length = length * -1;if(startIndex - length < 0) {length = startIndex;startIndex = 0;} else {startIndex = startIndex - length;}}if (startIndex > str.length()) {return \"\";}} else {if (length < 0) {return \"\";} else {if (length + startIndex > 0) {length = length + startIndex;startIndex = 0;} else {return \"\";}}}if (str.length() - startIndex < length) {length = str.length() - startIndex;}return str.substring(startIndex, startIndex + length);}public static String CutString(String str, int startIndex) {return CutString(str, startIndex, str.length());}public static String MD5(String pass) {byte[] defaultBytes = pass.getBytes();try{MessageDigest algorithm = MessageDigest.getInstance(\"MD5\");algorithm.reset();algorithm.update(defaultBytes);byte messageDigest[] = algorithm.digest();StringBuffer hexString = new StringBuffer();for (int i=0;i<messageDigest.length;i++) {String hex = Integer.toHexString(0xFF & messageDigest[i]);if(hex.length()==1)hexString.append(\"0\");hexString.append(hex);}return hexString.toString();}catch(NoSuchAlgorithmException nsae){}return \"\";}public static boolean StrIsNullOrEmpty(String str) {//#if NET1if (str == null || str.trim().equals(\"\")) {return true;}return false;}static private byte[] GetKey(byte[] pass, int kLen) {byte[] mBox = new byte[kLen];for (int i = 0; i < kLen; i++) {mBox[i] = (byte) i;}int j = 0;for (int i = 0; i < kLen; i++) {j = (j + (int) ((mBox[i] + 256) % 256) + pass[i % pass.length])% kLen;byte temp = mBox[i];mBox[i] = mBox[j];mBox[j] = temp;}return mBox;}public static String RandomString(int lens) {char[] CharArray = { \'a\', \'b\', \'c\', \'d\', \'e\', \'f\', \'g\', \'h\', \'j\', \'k\',\'l\', \'m\', \'n\', \'o\', \'p\', \'q\', \'r\', \'s\', \'t\', \'u\', \'v\', \'w\',\'x\', \'y\', \'z\', \'0\', \'1\', \'2\', \'3\', \'4\', \'5\', \'6\', \'7\', \'8\', \'9\' };int clens = CharArray.length;String sCode = \"\";Random random = new Random();for (int i = 0; i < lens; i++) {sCode += CharArray[Math.abs(random.nextInt(clens))];}return sCode;}public static String authcodeEncode(String source, String key) {return authcode(source, key, DiscuzAuthcodeMode.Encode );}public static String authcodeDecode(String source, String key) {return authcode(source, key, DiscuzAuthcodeMode.Decode );}public static String authcode(String source, String key,DiscuzAuthcodeMode operation ) {try {if (source == null || key == null) {return \"\";}int ckey_length = 4;String keya, keyb, keyc, cryptkey, result;key = MD5(key);keya = MD5(CutString(key, 0, 16));keyb = MD5(CutString(key, 16, 16));keyc = ckey_length > 0 ? (operation == DiscuzAuthcodeMode.Decode ? CutString(source, 0, ckey_length): RandomString(ckey_length)): \"\";cryptkey = keya + MD5(keya + keyc);if (operation == DiscuzAuthcodeMode.Decode) {byte[] temp;temp = decoder.decodeBuffer(CutString(source, ckey_length));result = new String(RC4(temp, cryptkey));if (CutString(result, 10, 16).equals(CutString(MD5(CutString(result, 26) + keyb), 0, 16))) {return CutString(result, 26);} else {temp = decoder.decodeBuffer(CutString(source+\"=\", ckey_length));result = new String(RC4(temp, cryptkey));if (CutString(result, 10, 16).equals(CutString(MD5(CutString(result, 26) + keyb), 0, 16))) {return CutString(result, 26);} else {temp = decoder.decodeBuffer(CutString(source+\"==\", ckey_length));result = new String(RC4(temp, cryptkey));if (CutString(result, 10, 16).equals(CutString(MD5(CutString(result, 26) + keyb), 0, 16))) {return CutString(result, 26);}else{return \"\";}}}} else {source = \"0000000000\" + CutString(MD5(source + keyb), 0, 16)+ source;byte[] temp = RC4(source.getBytes(\"utf-8\"), cryptkey);return keyc + encoder.encodeBuffer(temp);}} catch (Exception e) {return \"\";}}private static byte[] RC4(byte[] input, String pass) {if (input == null || pass == null)return null;byte[] output = new byte[input.length];byte[] mBox = GetKey(pass.getBytes(), 256);int i = 0;int j = 0;for (int offset = 0; offset < input.length; offset++) {i = (i + 1) % mBox.length;j = (j + (int) ((mBox[i] + 256) % 256)) % mBox.length;byte temp = mBox[i];mBox[i] = mBox[j];mBox[j] = temp;byte a = input[offset];byte b = mBox[(toInt(mBox[i]) + toInt(mBox[j])) % mBox.length];output[offset] = (byte) ((int) a ^ (int) toInt(b));}return output;}public static int toInt(byte b) {return (int) ((b + 256) % 256);}public long getUnixTimestamp() {Calendar cal = Calendar.getInstance();return cal.getTimeInMillis() / 1000;}}
[/code]
转载于:Android开发/578888