JAVA实现的AZDG加密-解密
importjava.io.IOException;
importjava.security.MessageDigest;
publicclassAzdg {
publicstaticvoidmain(String[] args) {
Azdg azdg =newAzdg();
System.out.println(System.currentTimeMillis()/1000);
String clearText ="no=sh0105&time=12319399372223`112r3r223";//明文
String cipherText ="";//密文
String cleanText ="";//解密后的
String key ="1234567890";//密钥,可自定义
System.out.println("明文为:"+ clearText);
cipherText = azdg.encrypt(clearText, key);
System.out.println("加密后为:"+cipherText);
cleanText = azdg.decrypt(cipherText, key);
System.out.println("解密后为:"+cleanText);
}
/**
*加密算法
**/
publicString encrypt(String txt, String key) {
String encrypt_key ="0f9cfb7a9acced8a4167ea8006ccd098";
intctr = 0;
String tmp ="";
inti;
for(i = 0; i < txt.length(); i++) {
ctr = (ctr == encrypt_key.length()) ? 0 : ctr;
tmp = tmp + encrypt_key.charAt(ctr)
+ (char)(txt.charAt(i) ^ encrypt_key.charAt(ctr));
ctr++;
}
returnbase64_encode(key(tmp, key));
}
/**
*解密算法
*/
publicString decrypt(String cipherText, String key) {
// base64解码
cipherText = base64_decode(cipherText);
cipherText = key(cipherText, key);
String tmp ="";
for(inti = 0; i < cipherText.length(); i++) {
intc = cipherText.charAt(i) ^ cipherText.charAt(i + 1);
String x =""+ (char) c;
tmp += x;
i++;
}
returntmp;
}
publicString key(String txt, String encrypt_key) {
encrypt_key = strMD5(encrypt_key);
intctr = 0;
String tmp ="";
for(inti = 0; i < txt.length(); i++) {
ctr = (ctr == encrypt_key.length()) ? 0 : ctr;
intc = txt.charAt(i) ^ encrypt_key.charAt(ctr);
String x =""+ (char) c;
tmp = tmp + x;
ctr++;
}
returntmp;
}
publicString base64_encode(String str) {
returnnewsun.misc.BASE64Encoder().encode(str.getBytes());
}
publicString base64_decode(String str) {
sun.misc.BASE64Decoder decoder =newsun.misc.BASE64Decoder();
if(str ==null)
returnnull;
try{
returnnewString(decoder.decodeBuffer(str));
}catch(IOException e) {
e.printStackTrace();
returnnull;
}
}
publicstaticfinalString strMD5(String s) {
charhexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f'};
try{
byte[] strTemp = s.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
intj = md.length;
charstr[] =newchar[j * 2];
intk = 0;
for(inti = 0; i < j; i++) {
bytebyte0 = md[i];