Source code for string Encryption By js in the project

Source: Internet
Author: User

This is not the same as the http://blog.csdn.net/zl544434558/article/details/21702195 java edition encryption rules. That is to say, the same string is parsed differently.

The following is the source code for string Encryption by js used in the project (written in pure js without any environment or file ):

/** A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Version 2.1 Copyright (C) Paul Johnston 1999-2002. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distributed under the BSD License * See http://pajhome.org.uk/crypt/md5 for more info. * // ** variable able variables. you may need to tweak these to be compati Ble with * the server-side, but the defaults work in most cases. */var hexcase = 0;/* hex output format. 0-lowercase; 1-uppercase */var b64pad = "";/* base-64 pad character. "=" for strict RFC compliance */var chrsz = 8;/* bits per input character. 8-ASCII; 16-Unicode * // ** These are the functions you'll usually want to call * They take string arguments and return either hex or base-6 4 encoded strings */function hex_md5 (s) {return binl2hex (core_md5 (str2binl (s), s. length * chrsz);} function b64_md5 (s) {return binl2b64 (core_md5 (str2binl (s), s. length * chrsz);} function str_md5 (s) {return binl2str (core_md5 (str2binl (s), s. length * chrsz);} function hex_hmac_md5 (key, data) {return binl2hex (core_hmac_md5 (key, data);} function b64_hmac_md5 (key, data) {return binl2b64 (core_hmac_md5 (Key, data);} function str_hmac_md5 (key, data) {return binl2str (core_hmac_md5 (key, data ));} /** Calculate the MD5 of an array of little-endian words, and a bit length */function core_md5 (x, len) {/* append padding */x [len> 5] | = 0x80 <(len) % 32 ); x [(len + 64) >>> 9) <4) + 14] = len; var a = 1732584193; var B =-271733879; var c =-1732584194; var d = 271733878; for (var I = 0; I <x. Length; I + = 16) {var olda = a; var oldb = B; var oldc = c; var oldd = d; a = md5_ff (a, B, c, d, x [I + 0], 7,-680876936); d = md5_ff (d, a, B, c, x [I + 1], 12,-389564586 ); c = md5_ff (c, d, a, B, x [I + 2], 17,606 105819); B = md5_ff (B, c, d,, x [I + 3], 22,-1044525330); a = md5_ff (a, B, c, d, x [I + 4], 7,-176418897 ); d = md5_ff (d, a, B, c, x [I + 5], 12,120 0080426); c = md5_ff (c, d, a, B, x [I + 6], 17,-1473231341); B = md5_ff (B, c, d, a, x [I + 7], 22,-45705983); a = md5_ff (a, B, c, d, x [I + 8], 7, 1770035416); d = md5_ff (d, a, B, c, x [I + 9], 12, -1958414417); c = md5_ff (c, d, a, B, x [I + 10], 17,-42063); B = md5_ff (B, c, d, a, x [I + 11], 22,-1990404162); a = md5_ff (a, B, c, d, x [I + 12], 7, 1804603682 ); d = md5_ff (d, a, B, c, x [I + 13], 12,-40341101); c = md5_ff (c, d, a, B, x [I + 14], 17,-15020. 02290); B = md5_ff (B, c, d, a, x [I + 15], 22,123 6582429); a = md5_gg (a, B, c, d, x [I + 1], 5,-165796510); d = md5_gg (d, a, B, c, x [I + 6], 9,-1069501632 ); c = md5_gg (c, d, a, B, x [I + 11], 14,643 717713); B = md5_gg (B, c, d,, x [I + 0], 20,-373897302); a = md5_gg (a, B, c, d, x [I + 5], 5,-701558691 ); d = md5_gg (d, a, B, c, x [I + 10], 9, 38016083); c = md5_gg (c, d, a, B, x [I + 15], 14,-660478335); B = Md5_gg (B, c, d, a, x [I + 4], 20,-405537848); a = md5_gg (a, B, c, d, x [I + 9], 5, 568446438); d = md5_gg (d, a, B, c, x [I + 14], 9,-1019803690 ); c = md5_gg (c, d, a, B, x [I + 3], 14,-187363961); B = md5_gg (B, c, d,, x [I + 8], 20,116 running 1501); a = md5_gg (a, B, c, d, x [I + 13], 5,-1444681467 ); d = md5_gg (d, a, B, c, x [I + 2], 9,-51403784); c = md5_gg (c, d, a, B, x [I + 7], 14,173 5328473); B = md5_gg (B, c, d, a, x [I + 12], 20,-1926607734); a = md5_hh (a, B, c, d, x [I + 5], 4,-378558); d = md5_hh (d, a, B, c, x [I + 8], 11,-2022574463 ); c = md5_hh (c, d, a, B, x [I + 11], 16,183 9030133); B = md5_hh (B, c, d,, x [I + 14], 23,-35309556); a = md5_hh (a, B, c, d, x [I + 1], 4,-1530992060 ); d = md5_hh (d, a, B, c, x [I + 4], 11,127 2893353); c = md5_hh (c, d, a, B, x [I + 7], 16,-155497632); B = md5_hh (B, c, d, A, x [I + 10], 23,-1094730640); a = md5_hh (a, B, c, d, x [I + 13], 4, 681279174 ); d = md5_hh (d, a, B, c, x [I + 0], 11,-358537222); c = md5_hh (c, d, a, B, x [I + 3], 16,-722521979); B = md5_hh (B, c, d, a, x [I + 6], 23,760 29189 ); a = md5_hh (a, B, c, d, x [I + 9], 4,-640364487); d = md5_hh (d, a, B, c, x [I + 12], 11,-421815835); c = md5_hh (c, d, a, B, x [I + 15], 16,530 742520 ); B = md5_hh (B, c, d, a, x [I + 2], 23,-995338651); a = md5_ii (a, B, c, d, x [I + 0], 6,-198630844); d = md5_ii (d,, b, c, x [I + 7], 10,112 6891415); c = md5_ii (c, d, a, B, x [I + 14], 15,-1416354905 ); B = md5_ii (B, c, d, a, x [I + 5], 21,-57434055); a = md5_ii (a, B, c, d, x [I + 12], 6, 1700485571); d = md5_ii (d, a, B, c, x [I + 3], 10,-1894986606 ); c = md5_ii (c, d, a, B, x [I + 10], 15,-1051523); B = md5_ii (B, c, d,, x [I + 1], 21,-20 54922799); a = md5_ii (a, B, c, d, x [I + 8], 6, 1873313359); d = md5_ii (d, a, B, c, x [I + 15], 10,-30611744); c = md5_ii (c, d, a, B, x [I + 6], 15,-1560198380 ); B = md5_ii (B, c, d, a, x [I + 13], 21,130 9151649); a = md5_ii (a, B, c, d, x [I + 4], 6,-145523070); d = md5_ii (d, a, B, c, x [I + 11], 10,-1120210379 ); c = md5_ii (c, d, a, B, x [I + 2], 15,718 787259); B = md5_ii (B, c, d,, x [I + 9], 21,-343485551. ); A = safe_add (a, olda); B = safe_add (B, oldb); c = safe_add (c, oldc); d = safe_add (d, oldd );} return Array (a, B, c, d);}/** These functions implement the four basic operations the algorithm uses. */function md5_cen (q, a, B, x, s, t) {return safe_add (bit_rol (safe_add (a, q), safe_add (x, t )), s), B);} function md5_ff (a, B, c, d, x, s, t) {return md5_cen (B & c) | ((~ B) & d), a, B, x, s, t);} function md5_gg (a, B, c, d, x, s, t) {return md5_cen (B & d) | (c &(~ D), a, B, x, s, t);} function md5_hh (a, B, c, d, x, s, t) {return md5_cen (B ^ c ^ d, a, B, x, s, t);} function md5_ii (a, B, c, d, x, s, t) {return md5_cen (c ^ (B | (~ D), a, B, x, s, t);}/** Calculate the HMAC-MD5, of a key and some data */function core_hmac_md5 (key, data) {var bkey = str2binl (key); if (bkey. length> 16) bkey = core_md5 (bkey, key. length * chrsz); var ipad = Array (16), opad = Array (16); for (var I = 0; I <16; I ++) {ipad [I] = bkey [I] ^ 0x36363636; opad [I] = bkey [I] ^ 0x5C5C5C5C;} var hash = core_md5 (ipad. concat (str2binl (data), 512 + data. leng Th * chrsz); return core_md5 (opad. concat (hash), 512 + 128);}/** Add integers, wrapping at 2 ^ 32. this uses 16-bit operations internally * to work around the bugs in some JS interpreters. */function safe_add (x, y) {var lsw = (x & 0 xFFFF) + (y & 0 xFFFF); var msw = (x> 16) + (y> 16) + (lsw> 16); return (msw <16) | (lsw & 0 xFFFF );} /** Bitwise rotate a 32-bit number to the left. */function bit_r Ol (num, cnt) {return (num <cnt) | (num >>>( 32-cnt ));} /** Convert a string to an array of little-endian words * If chrsz is ASCII, characters> 255 have their hi-byte silently ignored. */function str2binl (str) {var bin = Array (); var mask = (1 <chrsz)-1; for (var I = 0; I <str. length * chrsz; I + = chrsz) bin [I> 5] | = (str. charCodeAt (I/chrsz) & mask) <(I % 32); return bin;}/** Convert An array of little-endian words to a string */function binl2str (bin) {var str = ""; var mask = (1 <chrsz)-1; for (var I = 0; I <bin. length * 32; I + = chrsz) str + = String. fromCharCode (bin [I> 5] >>>( I % 32) & mask); return str ;} /** Convert an array of little-endian words to a hex string. */function binl2hex (binarray) {var hex_tab = hexcase? "0123456789 ABCDEF": "0123456789 abcdef"; var str = ""; for (var I = 0; I <binarray. length * 4; I ++) {str + = hex_tab.charAt (binarray [I> 2]> (I % 4) * 8 + 4) & 0xF) + hex_tab.charAt (binarray [I> 2]> (I % 4) * 8) & 0xF) ;} return str ;} /** Convert an array of little-endian words to a base-64 string */function binl2b64 (binarray) {var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 789 +/"; var str =" "; for (var I = 0; I <binarray. length * 4; I + = 3) {var triplet = (binarray [I> 2]> 8 * (I % 4) & 0xFF) <16) | (binarray [I + 1> 2]> 8 * (I + 1) % 4) & 0xFF) <8) | (binarray [I + 2> 2]> 8 * (I + 2) % 4) & 0xFF); for (var j = 0; j <4; j ++) {if (I * 8 + j * 6> binarray. length * 32) str + = b64pad; else str + = tab. charAt (triplet> 6 * (3-j) & 0x3F) ;}} return str ;}/* * Perform a simple self-test to see if the VM is working */function md5_vm_test () {return hex_md5 ("abc") = "900150983cd24fb0d6963f7d28e17f72"; // return true, this md5 encryption rule is different from that in java .}


Related Article

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.