Cet article décrit le cryptage Base64, le cryptage MD5 et le cryptage SHA1 mis en œuvre par JS. Partagez-le pour votre référence, comme suit:
1. Encryption de base64
Présentez le fichier Base64.js dans la page, et la méthode d'appel est:
<! Doctype html> <html> <éadf> <meta charset = "utf-8"> <tight> Base64 Encryption </ title> <script type = "text / javascript" src = "base64.js"> </ script> <script type = "text / javascript"> var b = new base64 (); var str = b.encode ("admin: admin"); alerte ("Base64 Encode:" + Str); // Decrypt str = b.decode (str); alerte ("Base64 Decode:" + str); </cript> </ada> <body> </body> </html>2. Encryption MD5
Reportez-vous au fichier MD5.js dans la page et la méthode d'appel est
<! Doctype html> <html> <éadf> <meta charset = "utf-8"> <tight> md5 Encryption </ title> <script type = "text / ecmascript" src = "md5.js"> </ script> <script type = "text / javascript"> var hash = hex_md5 ("123dafd"); alerte (hachage) </cript> </ired> <body> </body> </html>3. Encryption SHA1
On dit que c'est le cryptage le plus sûr
La page présente Sha1.js et la méthode d'appel est
<! Doctype html> <html> <éadf> <meta charset = "utf-8"> <tight> sha1 Encryption </ title> <script type = "text / ecmascript" src = "sha1.js"> </ script> <script type = "text / javascrip alerte (sha) </cript> </ad> <body> </gody> </html>
Voici le code source de JS
base64.js:
/ **** Base64 Encode / Decode ** @author haitao.tu * @date 2010-04-26 * @email [email protected]**/fonction base64 () {// propriété privée _KeyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 + / ="; // Méthode publique pour coder this.encode = function (input) {var output = ""; Var Chr1, Chr2, Chr3, ENC1, ENC2, ENC3, ENC4; var i = 0; input = _utf8_encode (input); while (i <input.length) {ch1 = input.charcodeat (i ++); chr2 = input.charcodeat (i ++); chr3 = input.charcodeat (i ++); ENC1 = CHR1 >> 2; Enc2 = ((Chr1 et 3) << 4) | (Chr2 >> 4); Enc3 = ((chr2 et 15) << 2) | (ch3 >> 6); ENC4 = CHR3 & 63; if (isnan (chr2)) {enc3 = enc4 = 64; } else if (isnan (ch3)) {enc4 = 64; } output = output + _Keystr.Charat (ENC1) + _KeyStr.Charat (ENC2) + _Keystr.Charat (ENC3) + _KeyStr.Charat (ENC4); } return output; } // Méthode publique pour décoder this.decode = function (input) {var output = ""; var chr1, chr2, chr3; var Enc1, Enc2, Enc3, ENC4; var i = 0; input = input.replace (/ [^ a-za-z0-9 / + /// =] / g, ""); while (i <input.length) {enc1 = _KeyStr.Indexof (input.charat (i ++)); enc2 = _KeyStr.Indexof (input.charat (i ++)); ENC3 = _KEYSTR.Indexof (Input.Charat (i ++)); Enc4 = _KeyStr.Indexof (Input.Charat (i ++)); Chr1 = (ENC1 << 2) | (ENC2 >> 4); chr2 = ((enc2 et 15) << 4) | (ENC3 >> 2); Chr3 = ((Enc3 et 3) << 6) | ENC4; output = output + String.FromCharcode (Chr1); if (Enc3! = 64) {Output = Output + String.FromCharcode (Chr2); } if (Enc4! = 64) {output = output + string.fromCharcode (chr3); }} output = _UTF8_DECODE (sortie); sortie de retour; } // Méthode privée pour UTF-8 codage _UTF8_Encode = function (string) {string = string.replace (// r / n / g, "/ n"); var utftext = ""; pour (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; } // Méthode privée pour le décodage UTF-8 _UTF8_DECODE = fonction (utftext) {var string = ""; var i = 0; var c = c1 = c2 = 0; while (i <utftext.length) {c = utfText.CharCodeat (i); if (c <128) {string + = string.fromCharcode (c); i ++; } else if ((c> 191) && (c <224)) {c2 = utftext.CharCodeat (i + 1); String + = String.FromCharcode (((C & 31) << 6) | (C2 & 63)); i + = 2; } else {c2 = utftext.CharCodeat (i + 1); c3 = utfText.CharCodeat (i + 2); String + = String.FromCharcode (((C & 15) << 12) | ((C2 & 63) << 6) | (C3 & 63)); i + = 3; }} return String; }}
MD5.JS:
/ * * Une implémentation JavaScript de l'algorithme RSA Data Security, Inc. MD5 * Digest Algorithm, tel que défini dans RFC 1321. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. * Les autres contributeurs: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distribué sous la licence BSD * See http://pajhome.org.uk/crypt/md5 pour plus d'informations. * // * * Variables configurables. Vous devrez peut-être les modifier pour être compatibles avec * le côté serveur, mais les défauts fonctionnent dans la plupart des cas. * / var hexcase = 0; / * Format de sortie hexadécimal. 0 - minuscules; 1 - Uppercase * / var b64pad = ""; / * Caractère de pad de base-64. "=" pour une conformité RFC stricte * / var chrsz = 8; / * bits par caractère d'entrée. 8 - ASCII; 16 - Unicode * // * * Ce sont les fonctions que vous voudrez généralement appeler * Ils prennent des arguments de chaîne et renvoient des chaînes codées hex binl2b64 (core_md5 (str2binl (s), s.length * chrsz));} fonction str_md5 (s) {return binl2str (core_md5 (str2binl (s), s.length * chrsz));} function hex_hmac_md5 (key, data) {return binl2hx (core_hmac_md5, data) {return binl2hx (core_hmac_mac_md5, data) {return binl2hx (Core_hmac_mac_md5, data) {return binl2hx (Core_hmac_mac_ données)); } fonction b64_hmac_md5 (key, data) {return binl2b64 (core_hmac_md5 (key, data)); } fonction str_hmac_md5 (key, data)); } fonction str_hmac_md5 (key, data) {return binl2str (core_hmac_md5 (key, data)); } / * * Effectuez un simple test d'auto-test pour voir si la machine virtuelle fonctionne * / fonction md5_vm_test () {return hex_md5 ("ABC") == "900150983cd24fb0d6963f7d28e17f72";} / * * calculer le MD5 d'un tableau de petits mots, et un bits de la longueur * core_md5 (x, len) {/ * padding d'append * / 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; pour (var i = 0; i <x.length; i + = 16) {var old = a; var oldb = b; var oldc = c; var old = 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, 606105819); b = md5_ff (b, c, d, a, 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, 1200080426); 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, -1502002290); b = md5_ff (b, c, d, a, x [i + 15], 22, 1236535329); 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 + 1], 14, 643717713); b = md5_gg (b, c, d, a, x [i + 0], 20, -373897302); a = md5_gg (a, b, c, d, x [i + 5], 5, -701558691); d = md5_gg (d, a, b, c, d, x [i + 5], 5, -701558691); d = md5_gg (d, a, b, c, 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, a, x [i + 8], 20, 1163531501); 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, 1735328473); 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_gg (b, c, d, a, 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, 1839030562); b = md5_hh (b, c, d, a, 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, 1272893353); 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, 76029189); a = md5_hh (a, b, c, d, x [i + 6], 23, 76029189); a = md5_hh (a, b, c, d, x [i + 6], 23, 76029189); a = md5_hh (a, b, c, d, 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, 76029189); a = md5_hh (a, b, c, d, x [i + 6], 23, 76029189); a = md5_hh (a, b, c, d, x [i + 6], 23, 76029189); a = md5_hh (a, b, c, d, x [i + 6], 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, 530742520); 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, a, b, c, x [i + 7], 10, 1126891415); 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, a, x [i + 1], 21, -2054922799); 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, 1309151649); 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, 718787259); b = md5_ii (b, c, d, a, 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);} / * * Ces fonctions implémentent les quatre opérations de base que l'algorithme utilise. * / fonction md5_cmn (q, a, b, x, s, t) {return safe_add (bit_rol (safe_add (safe_add (safe_add (a, q), safe_add (x, t)), s), b);} fonction md5_ff (a, b, c, x, s, t) {return md5_cmn (b, d, x, s, t) {return md5_cmn ((b) & d), a, b, x, s, t);}function md5_gg(a, b, c, d, x, s, t){ return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);}function md5_hh(a, b, c, d, x, s, t){ return md5_cmn(b ^ c ^ d, a, b, x, s, t);} fonction md5_ii (a, b, c, d, x, s, t) {return md5_cmn (c ^ (b | (~ d)), a, b, x, s, t);} / * * calculer le hmac-md5, d'une clé et de quelques données * / fonction core_hmac_md5 (clé, data) {VaR bkey = str2binl (key); 0x5c5c5c5c;} var hash = core_md5 (ipad.concat (str2binl (data)), 512 + data.length * chrsz); Les interprètes. return (num << cnt) | Chrsz; i + = chrsz) Bin.Length * 32; i + = chrsz) str + = string.fromCharcode ((bin [i >> 5] >>> (i% 32) & mask); "0123456789ABCDEF": "0123456789ABCDEF"; hex_tab.charat ((binArray [i >> 2] >> ((i% 4) * 8)) & 0xf); "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 +";; << 16) | ((binArray [i + 1 >> 2] >> 8 * (i + 1)% 4)) & 0xff) << 8) | ((binarray [i + 2 >> b64pad; else str + = tab.charat ((triplet >> 6 * (3-j)) & 0x3f);sha1.js:
/ * * Une implémentation JavaScript de l'algorithme de hachage sécurisé, SHA-1, tel que défini * dans FIPS Pub 180-1 * Version 2.1-Beta Copyright Paul Johnston 2000 - 2002. * D'autres contributeurs: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distribué sous la licence BSD * Voir http://pajhome.org.uk/Md) pour plus de détails. * // * * Variables configurables. Vous devrez peut-être les modifier pour être compatibles avec * le côté serveur, mais les défauts fonctionnent dans la plupart des cas. * / var hexcase = 0; / * Format de sortie hexadécimal. 0 - minuscules; 1 - Uppercase * / var b64pad = ""; / * Caractère de pad de base-64. "=" pour une conformité RFC stricte * / var chrsz = 8; / * bits par caractère d'entrée. 8 - ASCII; 16 - Unicode * // * * Ce sont les fonctions que vous voudrez généralement appeler * Ils prennent des arguments de chaîne et renvoient des chaînes codées hex binb2b64 (core_sha1 (str2binb (s), s.length * chrsz));} fonction str_sha1 (s) {return binb2str (core_sha1 (str2binb (s), s.length * chrsz));} function hex_hmac_sha1 (key, data) {return binb2hex (corshmac_ha1, data) {return binb2hex (corshmac_ données));} fonction b64_hmac_sha1 (key, data) {return binb2b64 (core_hmac_sha1 (key, data));} function str_hmac_sha1 (key, data));} fonction str_hmac_sha1 (key, data) {return binb2str (core_hmmac_sha1 (key, data); Auto-test simple pour voir si la machine virtuelle fonctionne * / fonction sha1_vm_test () {return hex_sha1 ("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";} / * calcul le sha-1 de anec Core_sha1 (x, len) {/ * padding d'append * / x [len >> 5] | = 0x80 << (24 - len% 32); x [((len + 64 >> 9) << 4) + 15] = len; var w = tableau (80); var a = 1732584193; var b = -271733879; var c = -1732584194; var d = 271733878; var e = -1009589776; pour (var i = 0; i <x.length; i + = 16) {var old = a; var oldb = b; var oldc = c; var old = d; var old = e; pour (var j = 0; j <80; j ++) {if (j <16) w [j] = x [i + j]; else w [j] = rol (w [j - 3] ^ w [j - 8] ^ w [j - 14] ^ w [j - 16], 1); var t = safe_add (safe_add (rol (a, 5), sha1_ft (j, b, c, d)), safe_add (safe_add (e, w [j]), sha1_kt (j))); e = d; d = c; c = rol (b, 30); b = a; a = t; } a = safe_add (a, olda); b = safe_add (b, oldb); c = safe_add (c, oldc); d = safe_add (d, old); e = safe_add (e, old); } Return Array (A, B, C, D, E);} / * * Effectuez la fonction de combinaison de triplet appropriée pour le courant * itération * / fonction sha1_ft (t, b, c, d) {if (t <20) return (b & c) | ((~ b) & d); if (t <40) retour b ^ c ^ d; if (t <60) retour (b & c) | (B&D) | (C&D); retour b ^ c ^ d;} / * * Déterminez la constante supplémentaire appropriée pour l'itération actuelle * / fonction sha1_kt (t) {return (t <20)? 1518500249: (t <40)? 1859775393: (t <60)? -1894007588: -899497514;} / * * Calculez le HMAC-Sha1 d'une clé et certaines données * / fonction core_hmac_sha1 (clé, données) {var bkey = str2binb (key); if (bkey.length> 16) bkey = core_sha1 (bkey, key.length * chrsz); var iPad = array (16), opad = array (16); pour (var i = 0; i <16; i ++) {iPad [i] = bkey [i] ^ 0x36363636; opad [i] = bkey [i] ^ 0x5c5c5c5c; } var hash = core_sha1 (ipad.concat (str2binb (data)), 512 + data.length * chrsz); return core_sha1 (opad.concat (hash), 512 + 160);} / * * ajouter des entiers, enveloppant 2 ^ 32. Cela utilise des opérations 16 bits en interne * pour contourner les bogues dans certains interprètes JS. * / fonction safe_add (x, y) {var lsw = (x & 0xffff) + (y & 0xffff); var msw = (x >> 16) + (y >> 16) + (lsw >> 16); retour (MSW << 16) | (lsw & 0xffff);} / * * Faites pivoter un numéro de 32 bits vers la gauche. * / fonction rol (num, cnt) {return (num << cnt) | (num >>> (32 - cnt));} / * * Convertir une chaîne 8 bits ou 16 bits en un tableau de mots Big-endian * En fonction 8 bits, les caractères> 255 ont leur hi-byte en silence ignoré. * / fonction str2binb (str) {var bin = array (); var mask = (1 << chrsz) - 1; pour (var i = 0; i <str.length * chrsz; i + = chrsz) bin [i >> 5] | = (str.charcodeat (i / chrsz) & mask) << (24 - i% 32); return bin;} / * * Convertir un tableau de mots big-endian en une chaîne * / fonction binb2str (bin) {var str = ""; var mask = (1 << chrsz) - 1; pour (var i = 0; i <bin.length * 32; i + = chrsz) str + = string.fromCharcode ((bin [i >> 5] >>> (24 - i% 32)) & mask); return Str;} / * * Convertir un tableau de mots Big-endian en une chaîne hexagonale. * / fonction binb2hex (binarray) {var hex_tab = hexcase? "0123456789AbcDef": "0123456789ABCDEF"; var str = ""; pour (var i = 0; i <binarray.length * 4; i ++) {str + = hex_tab.charat ((binarray [i >> 2] >> ((3 - i% 4) * 8 + 4)) & 0xf) + hex_tab.charat ((binarray [i >> 2] >> ((3 - i% 4) * 8) & 0xf); } return str;} / * * convertir un tableau de mots big-endian en une chaîne de base-64 * / fonction binb2b64 (binArray) {var tab = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrsvwxyz0123456789 + /";; var str = ""; pour (var i = 0; i <binary.length * 4; i + = 3) {var triplet = (((binarray [i >> 2] >> 8 * (3 - i% 4)) & 0xff) << 16) | (((binarray [i + 1 >> 2] >> 8 * (3 - (i + 1)% 4)) & 0xff) << 8) | ((binarray [i + 2 >> 2] >> 8 * (3 - (i + 1)% 4)) & 0xff) << 8) | ((binarray [i + 2 >> 2] >> 8 * (3 - (i + 2)% 4)) & 0xff); pour (var j = 0; j <4; j ++) {if (i * 8 + j * 6> binary.length * 32) str + = b64pad; else str + = tab.charat ((triplet >> 6 * (3 - j)) & 0x3f); }} return str;}Les amis qui sont intéressés par le cryptage JavaScript peuvent également se référer aux outils en ligne de ce site :
Outil de cryptage en ligne MD5
Outil d'échappement de chiffrement / de décryptage
Outil de cryptage SHA1 en ligne
Outils de cryptage / décryptage Thunder, Express, Tornado URL
Détection en ligne de sécurité de mot de passe
Générateur de mots de passe à haute résistance
Pour plus d'informations sur JavaScript, veuillez consulter les sujets de ce site: "Résumé des compétences de cryptage et de décryptage JavaScript", "Résumé des compétences JavaScript Switching Special Effects", "Résumé des effets spéciaux et des compétences" JavaScript ", Summer des erreurs de données et des compétences de débogage javascrip Compétences en algorithme "," Résumé de l'algorithme de traverse JavaScript et des compétences "et" Résumé de l'utilisation de l'opération mathématique JavaScript "
J'espère que cet article sera utile à la programmation JavaScript de tous.