import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import sun.misc.BASE64Decoder;
public class DiscuzPassport {
public static String encrypt(String src, String key) {
Random random = new Random();
random.setSeed(System.currentTimeMillis());
String rand = "" + random.nextInt() % 32000;
String encKey = Encryption.generateKey(rand, "MD5");
int ctr = 0;
String tmp = "";
for (int i = 0; i < src.length(); i++) {
ctr = (ctr == encKey.length() ? 0 : ctr);
tmp += encKey.charAt(ctr);
char c = (char) (src.charAt(i) ^ encKey.charAt(ctr));
tmp += c;
ctr++;
}
String passportKey = passportKey(tmp, key);
return new sun.misc.BASE64Encoder().encode(passportKey.getBytes());
}
public static String decrypt(String src, String key) {
byte[] bytes = null;
try {
bytes = new BASE64Decoder().decodeBuffer(src);
src = new String(bytes);
} catch (Exception e) {
return null;
}
src = passportKey(src, key);
String tmp = "";
for (int i = 0; i < src.length(); ++i) {
char c = (char) (src.charAt(i) ^ src.charAt(++i));
tmp += c;
}
return tmp;
}
public static String passportKey(String src, String key) {
String encKey = Encryption.generateKey(key, "MD5");
int ctr = 0;
String tmp = "";
for (int i = 0; i < src.length(); ++i) {
ctr = (ctr == encKey.length() ? 0 : ctr);
char c = (char) (src.charAt(i) ^ encKey.charAt(ctr));
tmp += c;
ctr++;
}
return tmp;
}
public static String passportEncode(Map data) {
Set keys = data.keySet();
String key = "";
String ret = "";
Iterator iterator = keys.iterator();
while (iterator.hasNext()) {
key = (String) iterator.next();
try
{
ret += key + "=" + (String) data.get(key) + "&";
}
catch (Exception e)
{
return "";
}
}
if (ret.length() > 0)
return ret.substring(0, ret.length() - 1);
return "";
}
}
------------------------------
-----Encryption.java-----
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Encryption {
public static String generateKey(String src, String algorithm) {
MessageDigest m = null;
try
{
m = MessageDigest.getInstance(algorithm);
m.update(src.getBytes("UTF8"));
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
byte s[] = m.digest();
String result = "";
for (int i = 0; i < s.length; i++)
{
result += Integer.toHexString(
(0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
}
return result;
}
}
<%
String username = null;
String email = null;
String password = null;
//为上面三个变量赋值
//......
//......
Map mb = new LinkedHashMap();
mb.put("time", "" + System.currentTimeMillis());
mb.put("username", username);
mb.put("password", password);
if(email.length() == 0)
{
mb.put("email", "null");
}
else
{
mb.put("email", email);
}
String key = privateKey; //私钥
String enc=DiscuzPassport.passportEncode(mb);
String auth = DiscuzPassport.encrypt(enc, key);
String verify = "login" + auth + request.getParameter("forward") + key;
verify = Encryption.generateKey(verify, "MD5");
String location = "http://bbs.com/api/passport.php?action=login&auth=" + java.net.URLEncoder.encode(auth, "UTF-8") + "&forward=" + java.net.URLEncoder.encode(request.getParameter("forward"), "UTF-8") + "&verify=" + verify;
response.sendRedirect(location);
%>
------------------------------
{
String key = privateKey; //私钥
String verify = "logout" + request.getParameter("forward") + key;
verify = Encryption.generateKey(verify, "MD5");
String location = "http://bbs.com/api/passport.php?action=logout&" + "&forward=" + java.net.URLEncoder.encode(request.getParameter("forward"), "UTF-8") + "&verify=" + verify;
response.sendRedirect(location);
}
<tag:LoginCheck login="false">
<%
response.sendRedirect("http://bbs.com/");
%>
</tag:LoginCheck>
<tag:LoginCheck login="true">
<%
response.sendRedirect("logined.jsp?forward=http://bbs.com/&bbsRedirect=true");
%>
</tag:LoginCheck>