Javaweb Encryption and decryption--MD5

Source: Internet
Author: User
Tags md5 encryption

Main content: Front desk JS encryption + background Java decryption


First, the front desk JS encryption: The information will be sent to the background in JS MD5 encryption, get the key, and then send the information and key together to the background.

1. Introduce the MD5 JS Framework, the Md5.js file, as follows:

/**
* MD5 for JavaScript. (32bit)
* @ interface:md5 ("Input String")
* @ return:key for Input String

**/

var MD5 = function (string) {
function Rotateleft (LValue, ishiftbits) {
Return (lvalue<<ishiftbits) | (Lvalue>>> (32-ishiftbits));
}
function addunsigned (lx,ly) {
var Lx4,ly4,lx8,ly8,lresult;
LX8 = (LX & 0x80000000);
LY8 = (LY & 0x80000000);
LX4 = (LX & 0x40000000);
LY4 = (LY & 0x40000000);
LResult = (LX & 0x3fffffff) + (LY & 0X3FFFFFFF);
if (LX4 & lY4) {
Return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
}
if (lX4 | lY4) {
if (LResult & 0x40000000) {
Return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
} else {
Return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
}
} else {
Return (lResult ^ lX8 ^ lY8);
}
}
function F (x, Y, z) {return (&) | ((~x) & Z); }
function G (z/y) {return (X & Z) | (Y & (~z)); }
function H (z/y) {return (x ^ y ^ z);}
function I (x, Y, z) {return (y ^ | (~z))); }
function FF (A,B,C,D,X,S,AC) {
A = addunsigned (A, addunsigned (addunsigned (F (b, C, D), x), AC));
Return addunsigned (Rotateleft (A, s), b);
};
function GG (A,B,C,D,X,S,AC) {
A = addunsigned (A, addunsigned (addunsigned (G (b, C, D), x), AC));
Return addunsigned (Rotateleft (A, s), b);
};
function HH (A,B,C,D,X,S,AC) {
A = addunsigned (A, addunsigned (addunsigned (H (b, C, D), x), AC));
Return addunsigned (Rotateleft (A, s), b);
};
function II (A,B,C,D,X,S,AC) {
A = addunsigned (A, addunsigned (addunsigned (I (b, C, D), x), AC));
Return addunsigned (Rotateleft (A, s), b);
};
function Converttowordarray (string) {
var Lwordcount;
var lmessagelength = string.length;
var lnumberofwords_temp1=lmessagelength + 8;
var lnumberofwords_temp2= (lnumberofwords_temp1-(lnumberofwords_temp1% 64))/64;
var lnumberofwords = (lnumberofwords_temp2+1) *16;
var lwordarray=array (lNumberOfWords-1);
var lbyteposition = 0;
var lbytecount = 0;
while (Lbytecount < lmessagelength) {
Lwordcount = (lbytecount-(lbytecount% 4))/4;
lbyteposition = (lbytecount% 4) *8;
Lwordarray[lwordcount] = (Lwordarray[lwordcount] | (String.charcodeat (Lbytecount) <<lbyteposition));
lbytecount++;
}
Lwordcount = (lbytecount-(lbytecount% 4))/4;
lbyteposition = (lbytecount% 4) *8;
Lwordarray[lwordcount] = Lwordarray[lwordcount] | (0x80<<lbyteposition);
Lwordarray[lnumberofwords-2] = lmessagelength<<3;
LWORDARRAY[LNUMBEROFWORDS-1] = lmessagelength>>>29;
return lwordarray;
};
function Wordtohex (lValue) {
var wordtohexvalue= "", wordtohexvalue_temp= "", Lbyte,lcount;
for (LCount = 0;lcount<=3;lcount++) {
Lbyte = (lvalue>>> (lcount*8)) & 255;
Wordtohexvalue_temp = "0" + lbyte.tostring (16);
Wordtohexvalue = Wordtohexvalue + wordtohexvalue_temp.substr (wordtohexvalue_temp.length-2,2);
}
return wordtohexvalue;
};
function Utf8encode (string) {
String = String.Replace (/\r\n/g, "\ n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charcodeat (n);
if (c < 128) {
Utftext + = String.fromCharCode (c);
}
else if ((C > 127) && (C < 2048)) {
Utftext + = String.fromCharCode ((c >> 6) | 192);
Utftext + = String.fromCharCode ((C & 63) | 128);
}
else {
Utftext + = String.fromCharCode ((c >> 12) | 224);
Utftext + = String.fromCharCode (((c >> 6) & 63) | 128);
Utftext + = String.fromCharCode ((C & 63) | 128);
}
}
return utftext;
};
var x=array ();
var k,aa,bb,cc,dd,a,b,c,d;
var s11=7, s12=12, s13=17, s14=22;
var s21=5, s22=9, s23=14, s24=20;
var s31=4, s32=11, s33=16, s34=23;
var s41=6, s42=10, s43=15, s44=21;
String = Utf8encode (string);
x = Converttowordarray (string);
A = 0x67452301; b = 0xefcdab89; c = 0x98badcfe; D = 0x10325476;
for (k=0;k<x.length;k+=16) {
Aa=a; Bb=b; Cc=c; Dd=d;
A=FF (a,b,c,d,x[k+0], s11,0xd76aa478);
D=FF (d,a,b,c,x[k+1], s12,0xe8c7b756);
C=FF (c,d,a,b,x[k+2], s13,0x242070db);
B=FF (b,c,d,a,x[k+3], s14,0xc1bdceee);
A=FF (A,b,c,d,x[k+4], S11,0XF57C0FAF);
D=FF (d,a,b,c,x[k+5], s12,0x4787c62a);
C=FF (C,d,a,b,x[k+6], s13,0xa8304613);
B=FF (B,c,d,a,x[k+7], s14,0xfd469501);
A=FF (A,b,c,d,x[k+8], s11,0x698098d8);
D=FF (d,a,b,c,x[k+9], S12,0X8B44F7AF);
C=FF (C,D,A,B,X[K+10],S13,0XFFFF5BB1);
B=FF (B,C,D,A,X[K+11],S14,0X895CD7BE);
A=FF (a,b,c,d,x[k+12],s11,0x6b901122);
D=FF (d,a,b,c,x[k+13],s12,0xfd987193);
C=FF (c,d,a,b,x[k+14],s13,0xa679438e);
B=FF (b,c,d,a,x[k+15],s14,0x49b40821);
A=gg (a,b,c,d,x[k+1], s21,0xf61e2562);
D=gg (D,a,b,c,x[k+6], s22,0xc040b340);
C=gg (C,D,A,B,X[K+11],S23,0X265E5A51);
B=gg (b,c,d,a,x[k+0], S24,0XE9B6C7AA);
A=gg (a,b,c,d,x[k+5], s21,0xd62f105d);
D=gg (d,a,b,c,x[k+10],s22,0x2441453);
C=gg (c,d,a,b,x[k+15],s23,0xd8a1e681);
B=gg (B,c,d,a,x[k+4], S24,0XE7D3FBC8);
A=gg (a,b,c,d,x[k+9], s21,0x21e1cde6);
D=gg (D,A,B,C,X[K+14],S22,0XC33707D6);
C=gg (c,d,a,b,x[k+3], s23,0xf4d50d87);
B=gg (B,c,d,a,x[k+8], s24,0x455a14ed);
A=gg (a,b,c,d,x[k+13],s21,0xa9e3e905);
D=gg (d,a,b,c,x[k+2], s22,0xfcefa3f8);
C=gg (C,d,a,b,x[k+7], s23,0x676f02d9);
B=gg (B,C,D,A,X[K+12],S24,0X8D2A4C8A);
A=HH (a,b,c,d,x[k+5], s31,0xfffa3942);
D=HH (D,a,b,c,x[k+8], s32,0x8771f681);
C=HH (c,d,a,b,x[k+11],s33,0x6d9d6122);
B=HH (b,c,d,a,x[k+14],s34,0xfde5380c);
A=HH (a,b,c,d,x[k+1], s31,0xa4beea44);
D=HH (D,a,b,c,x[k+4], s32,0x4bdecfa9);
C=HH (C,d,a,b,x[k+7], s33,0xf6bb4b60);
B=HH (B,C,D,A,X[K+10],S34,0XBEBFBC70);
A=HH (A,B,C,D,X[K+13],S31,0X289B7EC6);
D=HH (d,a,b,c,x[k+0], S32,0XEAA127FA);
C=HH (c,d,a,b,x[k+3], s33,0xd4ef3085);
B=HH (B,c,d,a,x[k+6], s34,0x4881d05);
A=HH (a,b,c,d,x[k+9], s31,0xd9d4d039);
D=HH (D,A,B,C,X[K+12],S32,0XE6DB99E5);
C=HH (C,D,A,B,X[K+15],S33,0X1FA27CF8);
B=HH (b,c,d,a,x[k+2], s34,0xc4ac5665);
A=ii (a,b,c,d,x[k+0], s41,0xf4292244);
D=ii (D,a,b,c,x[k+7], s42,0x432aff97);
C=ii (C,D,A,B,X[K+14],S43,0XAB9423A7);
B=ii (b,c,d,a,x[k+5], s44,0xfc93a039);
A=ii (A,B,C,D,X[K+12],S41,0X655B59C3);
D=ii (d,a,b,c,x[k+3], s42,0x8f0ccc92);
C=ii (c,d,a,b,x[k+10],s43,0xffeff47d);
B=ii (b,c,d,a,x[k+1], S44,0X85845DD1);
A=ii (A,b,c,d,x[k+8], s41,0x6fa87e4f);
D=ii (D,A,B,C,X[K+15],S42,0XFE2CE6E0);
C=ii (C,d,a,b,x[k+6], s43,0xa3014314);
B=ii (B,C,D,A,X[K+13],S44,0X4E0811A1);
A=ii (A,b,c,d,x[k+4], s41,0xf7537e82);
D=ii (d,a,b,c,x[k+11],s42,0xbd3af235);
C=ii (c,d,a,b,x[k+2], S43,0X2AD7D2BB);
B=ii (b,c,d,a,x[k+9], s44,0xeb86d391);
A=addunsigned (A,AA);
B=addunsigned (B,BB);
C=addunsigned (C,CC);
D=addunsigned (D,DD);
}
var temp = Wordtohex (a) +wordtohex (b) +wordtohex (c) +wordtohex (d);
return Temp.tolowercase ();
}

2. Call the interface provided by Md5.js to get the key key, as follows:

<script>

var key = MD5 ("Input String");

</script>

Second, the background of Java decryption: backstage to get the information and keys from the foreground, and then use Java to encrypt the information MD5 to get a new key, and then with the front of the key to the comparison, if two keys are the same correct, if not consistent, the content was tampered with by hackers.

1. Receive information and keys from reception:

String Text = Request.getparameter ("Input String");

String Oldkey = Request.getparameter ("key");

2. Encrypt the information given in Java to get a new key


3. Compare two key keys for the same:

if (Oldkey==newkey) {

SYSTEM.OUT.PRINTLN ("The information is correct, not tampered with by hackers");

return true;

} else {

System.out.println ("Information error , has been tampered with by hackers");

return false;

}


NOTE: The MD5 encryption algorithm is divided into 16bit and 32bit, where the Md5.js code is the 32bit encryption method. Regardless of that, to ensure that the consistency of the front and back encryption method, that is, the foreground JS 32bit encryption, the background in Java with 32bit decryption, can not be mistaken.

Javaweb Encryption and decryption--MD5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.