MD5 Encryption
/*************************************** *******************
* Md5.js
*
* A JavaScript Implementation of the RSA Data Security, Inc. MD5
* Message-Digest algorithm.
*
* Copyright (c) Paul johnaston 1999. distributed under the lgpl.
**************************************** *******************/
/* To convert strings to a list of ASCII values */
VaR sascii = "! /"# $ % & '() * +,-./0123456789:; <=>? @ Abcdefghijklmnopqrstuvwxyz"
VaR sascii = sascii + "[//] ^ _ 'abcdefghijklmnopqrstuvwxyz {| }~ ";
/* Convert integer to hex string */
VaR shex = "0123456789 abcdef ";
Function MD5 (I)
{
H = "";
For (j = 0; j <= 3; j ++)
{
H + = shex. charat (I> (J * 8 + 4) & 0x0f) +
Shex. charat (I >>( J * 8) & 0x0f );
}
Return h;
}
/* Add, handling overflows correctly */
Function add (x, y)
{
Return (X & 0x7fffffff) + (Y & 0x7fffff) ^ (X & 0x80000000) ^ (Y & 0x80000000 );
}
/* MD5 rounds functions */
Function r1 (a, B, c, d, X, S, T)
{
Q = add (A, (B & C) | (~ B & D), add (x, t ));
Return add (q <s) | (q> (32-S) & (math. Pow (2, S)-1), B );
}
Function R2 (a, B, c, d, X, S, T)
{
Q = add (A, (B & D) | (C &~ D), add (x, t ));
Return add (q <s) | (q> (32-S) & (math. Pow (2, S)-1), B );
}
Function R3 (a, B, c, d, X, S, T)
{
Q = add (a, B ^ C ^ d), add (x, t ));
Return add (q <s) | (q> (32-S) & (math. Pow (2, S)-1), B );
}
Function R4 (a, B, c, d, X, S, T)
{
Q = add (A, C ^ (B | ~ D), add (x, t ));
Return add (q <s) | (q> (32-S) & (math. Pow (2, S)-1), B );
}
/* Main entry point */
Function cal1_5 (sinp ){
/* Calculate length in machine words, including padding */
WLEN = (sinp. Length + 8)> 6) + 1) <4;
VaR x = new array (WLEN );
/* Convert string to array of words */
J = 4;
For (I = 0; (I * 4) <sinp. length; I ++)
{
X [I] = 0;
For (j = 0; (j <4) & (J + I * 4) <sinp. Length); j ++)
{
X [I] + = (sascii. indexof (sinp. charat (I * 4) + J) + 32) <(J * 8 );
}
}
/* Append padding bits and length */
If (j = 4)
{
X [I ++] = 0x80;
}
Else
{
X [I-1] + = 0x80 <(J * 8 );
}
For (; I <WLEN; I ++) {x [I] = 0 ;}
X [WLEN-2] = sinp. length * 8;
/* Hard-coded initial values */
A = 0x67452301;
B = 0xefcdab89;
C = 0x98badcfe;
D = 0x10325476;
/* Process each 16-word block in turn */
For (I = 0; I <WLEN; I + = 16 ){
Ao =;
BO = B;
CO = C;
Do = D;
A = R1 (a, B, c, d, X [I + 0], 7, 0xd76aa478 );
D = R1 (D, a, B, c, X [I + 1], 12, 0xe8c7b756 );
C = R1 (c, d, A, B, X [I + 2], 17, 0x242070db );
B = R1 (B, c, d, A, X [I + 3], 22, 0xc1bdceee );
A = R1 (a, B, c, d, X [I + 4], 7, 0xf57c0faf );
D = R1 (D, a, B, c, X [I + 5], 12, 0x4787c62a );
C = R1 (c, d, A, B, X [I + 6], 17, 0xa8304613 );
B = R1 (B, c, d, A, X [I + 7], 22, 0xfd469501 );
A = R1 (a, B, c, d, X [I + 8], 7, 0x698098d8 );
D = R1 (D, a, B, c, X [I + 9], 12, 0x8b44f7af );
C = R1 (c, d, A, B, X [I + 10], 17, 0xffff5bb1 );
B = R1 (B, c, d, A, X [I + 11], 22, 0x895cd7be );
A = R1 (a, B, c, d, X [I + 12], 7, 0x6b901122 );
D = R1 (D, a, B, c, X [I + 13], 12, 0xfd987193 );
C = R1 (c, d, A, B, X [I + 14], 17, 0xa679438e );
B = R1 (B, c, d, A, X [I + 15], 22, 0x49b40821 );
A = R2 (a, B, c, d, X [I + 1], 5, 0xf61e2562 );
D = R2 (D, a, B, c, X [I + 6], 9, 0xc040b340 );
C = R2 (c, d, A, B, X [I + 11], 14, 0x265e5a51 );
B = R2 (B, c, d, A, X [I + 0], 20, 0xe9b6c7aa );
A = R2 (a, B, c, d, X [I + 5], 5, 0xd62f105d );
D = R2 (D, a, B, c, X [I + 10], 9, 0x2441453 );
C = R2 (c, d, A, B, X [I + 15], 14, 0xd8a1e681 );
B = R2 (B, c, d, A, X [I + 4], 20, 0xe7d3fbc8 );
A = R2 (a, B, c, d, X [I + 9], 5, 0x21e1cde6 );
D = R2 (D, a, B, c, X [I + 14], 9, 0xc33707d6 );
C = R2 (c, d, A, B, X [I + 3], 14, 0xf4d50d87 );
B = R2 (B, c, d, A, X [I + 8], 20, 0x455a14ed );
A = R2 (a, B, c, d, X [I + 13], 5, 0xa9e3e905 );
D = R2 (D, a, B, c, X [I + 2], 9, 0xfcefa3f8 );
C = R2 (c, d, A, B, X [I + 7], 14, 0x676f02d9 );
B = R2 (B, c, d, A, X [I + 12], 20, 0x8d2a4c8a );
A = R3 (a, B, c, d, X [I + 5], 4, 0xfffa3942 );
D = R3 (D, a, B, c, X [I + 8], 11, 0x8771f681 );
C = R3 (c, d, A, B, X [I + 11], 16, 0x6d9d6122 );
B = R3 (B, c, d, A, X [I + 14], 23, 0xfde5380c );
A = R3 (a, B, c, d, X [I + 1], 4, 0xa4beea44 );
D = R3 (D, a, B, c, X [I + 4], 11, 0x4bdecfa9 );
C = R3 (c, d, A, B, X [I + 7], 16, 0xf6bb4b60 );
B = R3 (B, c, d, A, X [I + 10], 23, 0xbebfbc70 );
A = R3 (a, B, c, d, X [I + 13], 4, 0x289b7ec6 );
D = R3 (D, a, B, c, X [I + 0], 11, 0xeaa1_fa );
C = R3 (c, d, A, B, X [I + 3], 16, 0xd4ef3085 );
B = R3 (B, c, d, A, X [I + 6], 23, 0x4881d05 );
A = R3 (a, B, c, d, X [I + 9], 4, 0xd9d4d039 );
D = R3 (D, a, B, c, X [I + 12], 11, 0xe6db99e5 );
C = R3 (c, d, A, B, X [I + 15], 16, 0x1fa27cf8 );
B = R3 (B, c, d, A, X [I + 2], 23, 0xc4ac5665 );
A = R4 (a, B, c, d, X [I + 0], 6, 0xf4292244 );
D = R4 (D, a, B, c, X [I + 7], 10, 0x432aff97 );
C = R4 (c, d, A, B, X [I + 14], 15, 0xab9423a7 );
B = R4 (B, c, d, A, X [I + 5], 21, 0xfc93a039 );
A = R4 (a, B, c, d, X [I + 12], 6, 0x655b59c3 );
D = R4 (D, a, B, c, X [I + 3], 10, 0x8f0ccc92 );
C = R4 (c, d, A, B, X [I + 10], 15, 0xffeff47d );
B = R4 (B, c, d, A, X [I + 1], 21, 0x85845dd1 );
A = R4 (a, B, c, d, X [I + 8], 6, 0x6fa87e4f );
D = R4 (D, a, B, c, X [I + 15], 10, 0xfe2ce6e0 );
C = R4 (c, d, A, B, X [I + 6], 15, 0xa3014314 );
B = R4 (B, c, d, A, X [I + 13], 21, 0x4e0811a1 );
A = R4 (a, B, c, d, X [I + 4], 6, 0xf7537e82 );
D = R4 (D, a, B, c, X [I + 11], 10, 0xbd3af235 );
C = R4 (c, d, A, B, X [I + 2], 15, 0x2ad7d2bb );
B = R4 (B, c, d, A, X [I + 9], 21, 0xeb86d391 );
A = add (A, AO );
B = add (B, Bo );
C = add (C, CO );
D = add (D, do );
}
Return MD5 (A) + MD5 (B) + MD5 (c) + MD5 (d );
}
Base64 encryption and decryption
VaR base64encodechars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /";
VaR base64decodechars = new array (
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-62,-1, -1,-1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1,-1,-1,-1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1,-1 );
// 64 Encryption
Function base64encode (STR ){
VaR out, I, Len;
VaR C1, C2, C3;
Len = Str. length;
I = 0;
Out = "";
While (I <Len ){
C1 = Str. charcodeat (I ++) & 0xff;
If (I = Len)
{
Out + = base64encodechars. charat (C1> 2 );
Out + = base64encodechars. charat (C1 & 0x3) <4 );
Out + = "= ";
Break;
}
C2 = Str. charcodeat (I ++ );
If (I = Len)
{
Out + = base64encodechars. charat (C1> 2 );
Out + = base64encodechars. charat (C1 & 0x3) <4) | (C2 & 0xf0)> 4 ));
Out + = base64encodechars. charat (C2 & 0xf) <2 );
Out + = "= ";
Break;
}
C3 = Str. charcodeat (I ++ );
Out + = base64encodechars. charat (C1> 2 );
Out + = base64encodechars. charat (C1 & 0x3) <4) | (C2 & 0xf0)> 4 ));
Out + = base64encodechars. charat (C2 & 0xf) <2) | (C3 & 0xc0)> 6 ));
Out + = base64encodechars. charat (C3 & 0x3f );
}
Return out;
}
// 64 decryption
Function base64decode (STR ){
VaR C1, C2, C3, C4;
VaR I, Len, out;
Len = Str. length;
I = 0;
Out = "";
While (I <Len ){
/* C1 */
Do {
C1 = base64decodechars [Str. charcodeat (I ++) & 0xff];
} While (I <Len & C1 =-1 );
If (C1 =-1)
Break;
/* C2 */
Do {
C2 = base64decodechars [Str. charcodeat (I ++) & 0xff];
} While (I <Len & C2 =-1 );
If (C2 =-1)
Break;
Out + = string. fromcharcode (C1 <2) | (C2 & 0x30)> 4 ));
/* C3 */
Do {
C3 = Str. charcodeat (I ++) & 0xff;
If (C3 = 61)
Return out;
C3 = base64decodechars [C3];
} While (I <Len & C3 =-1 );
If (C3 =-1)
Break;
Out + = string. fromcharcode (C2 & 0xf) <4) | (C3 & 0x3c)> 2 ));
/* C4 */
Do {
C4 = Str. charcodeat (I ++) & 0xff;
If (C4 = 61)
Return out;
C4 = base64decodechars [C4];
} While (I <Len & C4 =-1 );
If (C4 =-1)
Break;
Out + = string. fromcharcode (C3 & 0x03) <6) | C4 );
}
Return out;
}
UTF 8-bit and 16-bit conversion is not used
Function utf16to8 (STR ){
VaR out, I, Len, C;
Out = "";
Len = Str. length;
For (I = 0; I <Len; I ++ ){
C = Str. charcodeat (I );
If (C> = 0x0001) & (c <= 0x007f )){
Out + = Str. charat (I );
} Else if (C> 0x07ff ){
Out + = string. fromcharcode (0xe0 | (C> 12) & 0x0f ));
Out + = string. fromcharcode (0x80 | (C> 6) & 0x3f ));
Out + = string. fromcharcode (0x80 | (C> 0) & 0x3f ));
} Else {
Out + = string. fromcharcode (0xc0 | (C> 6) & 0x1f ));
Out + = string. fromcharcode (0x80 | (C> 0) & 0x3f ));
}
}
Return out;
}
Function utf8to16 (STR ){
VaR out, I, Len, C;
VaR char2, char3;
Out = "";
Len = Str. length;
I = 0;
While (I <Len ){
C = Str. charcodeat (I ++ );
Switch (C> 4)
{
Case 0: Case 1: Case 2: Case 3: Case 4: Case 5: Case 6: Case 7:
// 0 xxxxxxx
Out + = Str. charat (I-1 );
Break;
Case 12: Case 13:
// 110x XXXX 10xx xxxx
Char2 = Str. charcodeat (I ++ );
Out + = string. fromcharcode (C & 0x1f) <6) | (char2 & 0x3f ));
Break;
Case 14:
// 1110 XXXX 10xx XXXX 10xx xxxx
Char2 = Str. charcodeat (I ++ );
Char3 = Str. charcodeat (I ++ );
Out + = string. fromcharcode (C & 0x0f) <12) |
(Char2 & 0x3f) <6) |
(Char3 & 0x3f) <0 ));
Break;
}
}
Return out;
}