The
encountered the requirements of token verification, and then a demo, because I only used the token generation and verification, so according to their own needs to organize a bit, the original demo download: http://download.csdn.net/detail/ qierkang/9792660
1. Cryptographic tool class, Xxteautil
Import org.apache.commons.codec.binary.Base64; public class Xxteautil {/** * encrypts data with key * @param plain * @param key * @return/Public
Static byte[] Encrypt (byte[] plain, byte[] key {if (Plain.length = = 0) {return plain;
Return Tobytearray (Encrypt (Tointarray (plain, true), Tointarray (key, False), false); /** * Use key to decrypt * @param cipher * @param key * @return/public static byte[] Decrypt
Te[] cipher, byte[] key {if (Cipher.length = = 0) {return cipher;
Return Tobytearray (Decrypt (Tointarray (cipher, False), Tointarray (key, False)), true); /** * Use key to encrypt data * @param v * @param k * @return/public static int[] Encrypt (int[) v,
Int[] k) {int n = v.length-1;
if (n < 1) {return V;
} if (K.length < 4) {int[] key = new INT[4]; System.arraycopy (k, 0, key, 0, k.length);
K = key;
int z = v[n], y = v[0], delta = 0x9e3779b9, sum = 0, E;
int p, q = 6 +/(n + 1);
while (q--> 0) {sum = sum + Delta;
e = Sum >>> 2 & 3;
for (p = 0; p < n; p++) {y = v[p + 1];
z = v[p] + + (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
} y = v[0];
z = v[n] + + (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
} return v; /** * Use key to decrypt data * @param v * @param k * @return/public static int[] Decrypt (int[) v,
Int[] k) {int n = v.length-1;
if (n < 1) {return V;
} if (K.length < 4) {int[] key = new INT[4]; System. arraycopy (k, 0, key, 0, k.length);
K = key;
int z = v[n], y = v[0], delta = 0x9e3779b9, SUM, E;
int p, q = 6 +/(n + 1);
sum = q * DELTA;
while (sum!= 0) {e = Sum >>> 2 & 3;
for (p = n; p > 0; p--) {z = v[p-1];
y = v[p]-= (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
} z = v[n];
y = v[0]-= (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
sum = Sum-delta;
} return v; /** * byte array converted to integer array * @param data * @param includelength * @return/private static int [] Tointarray (byte[] Data, Boolean includelength) {int n = ((Data.length & 3) = = 0)?
(Data.length >>> 2): ((data.length >>> 2) + 1));
Int[] result; if (includelength) {result = new Int[n + 1];
Result[n] = data.length;
else {result = new Int[n];
} n = data.length; for (int i = 0; i < n; i++) {result[i >>> 2] |= (0X000000FF & Data[i]) << ((I & 3)
<< 3);
return result; /** * integer array converted to byte array * @param data * @param includelength * @return/private static BYT
E[] Tobytearray (int[] Data, Boolean includelength) {int n = data.length << 2;
if (includelength) {int m = data[data.length-1];
if (M > N) {return null;
else {n = m;
} byte[] result = new Byte[n]; for (int i = 0; i < n; i++) {Result[i] = (byte) (Data[i >>> 2] >>> ((I & 3) << ;
3) & 0xff);
return result;
}
/** * First Xxxtea encryption, BASE64 encryption * @param plain * @param key * @return/public static String encrypt (S
Tring Plain, string key) {String cipher = "";
byte[] k = Key.getbytes ();
byte[] v = plain.getbytes ();
cipher = new String (base64.encodebase64 (Xxteautil.encrypt (V, k)));
cipher = cipher.replace (' + ', '-');
cipher = Cipher.replace ('/', ' _ ');
cipher = cipher.replace (' = ', '. ');
return cipher; /** * Base64 decrypted first, after Xxxtea decryption * @param cipher * @param key * @return/public static Stri
Ng Decrypt (string cipher, String key) {string plain = "";
cipher = Cipher.replace ('-', ' + ');
cipher = Cipher.replace (' _ ', '/');
cipher = Cipher.replace ('. ', ' = ');
byte[] k = Key.getbytes ();
byte[] v = base64.decodebase64 (cipher);
Plain = new String (Xxteautil.decrypt (V, k));
return plain; }
}
2. Token generation and validation
Import Java.util.Random;
Import Java.util.concurrent.TimeUnit;
Import Org.apache.commons.lang.StringUtils; public class Tokenutil {private static final string[] Codebase= {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C" , "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "B", "C", "D", "
E "," F "," G "," H "," I "," J "," K "," L "," M "," N "," O "," P "," Q "," R "," s "," T "," U "," V "," w "," X "," Y "," Z "};
private static Random rand= new Random ();
/** Xxtea Encryption decryption key/private static String Seckey = "Captcha";
/** token timeout threshold (days) */private static long expire = 30;
/** Authentication Code character number/private static int charcount = 4;
public static final String Gentoken () {StringBuffer sb= new StringBuffer ();
for (int i=0; i<charcount; i++) {int randint= math.abs (Rand.nextint ());
Sb.append (codebase[randint% codebase.length]);
Long timestamp= system.currenttimemillis (); String token=Null
token= String.Format ("%s_%d", sb.tostring (), timestamp);
System.out.println ("Unencrypted token:" +token);
Token= Xxteautil.encrypt (token, seckey);
return token; public static Final Boolean Verificationtoken (String token) throws statusinfoexception{String plaintext= X
Xteautil.decrypt (token, seckey);
if (Stringutils.isblank (plaintext)) {throw new IllegalStateException ("Decryption failed, token may be tampered with");
} string[] Plaintextarr= plaintext.split ("_");
if (plaintextarr.length!=2) {throw new IllegalStateException ("Token Data format error");
Long timestamp= 0;
try{timestamp= Long.parselong (plaintextarr[1]);
}catch (NumberFormatException e) {throw new IllegalStateException ("Invalid timestamp");
} if ((System.currenttimemillis ()-timestamp) >timeunit.milliseconds.convert (expire+5, timeunit.days)) { throw new IllegalStateException ("token expired");
return true; }
}
What jar packages are used specifically to download demo to see