Este artigo descreve a criptografia Base64, a criptografia MD5 e a criptografia SHA1 implementada por JS. Compartilhe -o para sua referência, como segue:
1. Criptografia base64
Apresente o arquivo base64.js na página, e o método de chamada é:
<! Doctype html> <html> <head> <meta charset = "utf-8"> <title> base64 criptografia </ititle> <script type = "text/javascript" src = "base64.js"> </script> <script type = "text/javascript"> var str = B.Encode ("Admin: Admin"); ALERT ("Base64 Encode:" + str); // descriptografar str = b.decode (str); Alert ("Base64 Decody:" + str); </script> </ad Head> <body> </body> </html>2. Criptografia MD5
Consulte o arquivo md5.js na página, e o método de chamada é
<! Doctype html> <html> <head> <meta charset = "utf-8"> <title> MD5 CRIPTIÇÃO </title> <script type = "text/ecmascript" src = "md5.js"> </script> <script = "text/javasscript"> varhhh = hhex " alerta (hash) </sCript> </head> <body> </body> </html>
3. Criptografia sha1
Dizem que esta é a criptografia mais segura
A página apresenta sha1.js e o método de chamada é
<! Doctype html> <html> <head> <meta charset = "utf-8"> <title> sha1 criptografia </title> <script type = "text/ecmascript" src = "sha1.js"> </script> <script = "text/javascript"> vars Alerta (sha) </sCript> </ad Head> <body> </body> </html>
Aqui está o código -fonte para JS
base64.js:
/**** base64 codificação /decodificação ** @author haitao.tu*@date 2010-04-26*@email [email protected]**/function base64 () {// Propriedade privada _Keystr = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; // Método público para codificar this.Encode = function (input) {var output = ""; var Chr1, Chr2, Chr3, ENC1, ENC2, ENC3, ENC4; var i = 0; input = _utf8_encode (entrada); while (i <input.length) {chr1 = input.charcodeat (i ++); Chr2 = input.charcodeat (i ++); Chr3 = input.charcodeat (i ++); ENC1 = Chr1 >> 2; ENC2 = ((Chr1 e 3) << 4) | (Chr2 >> 4); Enc3 = ((Chr2 e 15) << 2) | (Chr3 >> 6); ENC4 = Chr3 e 63; if (isnan (Chr2)) {Enc3 = Enc4 = 64; } else if (isnan (Chr3)) {Enc4 = 64; } output = output + _Keystr.Charat (ENC1) + _Keystr.Charat (ENC2) + _Keystr.Charat (ENC3) + _Keystr.Charat (ENC4); } retornar saída; } // Método público para decodificar 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 e 15) << 4) | (Enc3 >> 2); Chr3 = ((Enc3 e 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 (saída); saída de retorno; } // Método privado para utf-8 codificação _utf8_encode = function (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) e 63) | 128); utftext += string.fromCharcode ((c & 63) | 128); }} retornar utftext; } // Método privado para UTF-8 Decodificação _utf8_decode = function (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 e 63)); i += 3; }} retornar string; }}
Md5.js:
/ * * Uma implementação JavaScript do algoritmo Digest RSA Data Security, Inc. MD5 * Digest, conforme definido na RFC 1321. http://pajhome.org.uk/crypt/md5 para mais informações. * // * * variáveis configuráveis. Pode ser necessário ajustá-los para serem compatíveis com * o lado do servidor, mas os padrões funcionam na maioria dos casos. */var hexcase = 0; /* Formato de saída hexadecimal. 0 - minúscula; 1 - Uppercase */var B64pad = ""; /* Caractere de Base-64 PAD. "=" Para conformidade rfc estrita */var Chrsz = 8; /* bits por caractere de entrada. 8 - ASCII; 16 - Unicode * // * * Essas são as funções que você normalmente deseja chamar *, eles recebem argumentos de string e retornam hexadecimal ou strings codificadas Base -64 */função hex_md5 (s) {retorna binl2hex (core_md5 (str2binl (s), s.Length * Chrsz);} funct. binl2b64 (core_md5 (str2binl (s), s.length * Chrsz));} função str_md5 (s) {return binl2str (core_md5 (str2binl (s), s.length * chrsz);} função dados)); } função b64_hmac_md5 (chave, dados) {return binl2b64 (core_hmac_md5 (chave, dados)); } função str_hmac_md5 (chave, dados)); } função str_hmac_md5 (chave, dados) {return binl2str (core_hmac_md5 (chave, dados)); }/* * Perform a simple self-test to see if the VM is working */function md5_vm_test(){ return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";}/* * Calculate the MD5 of an array of little-endian words, and a bit length */function Core_md5 (x, len) { / * Anexe o preenchimento * / 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 antigo = a; var OldB = B; var Oldc = C; var antigo = 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, antiga); b = safe_add (b, OldB); c = safe_add (c, antigo); d = safe_add (D, Oldd); } Retorno Array (A, B, C, D);}/ * * Essas funções implementam as quatro operações básicas que o algoritmo usa. */function md5_cmn (q, a, b, x, s, t) {return safe_add (bit_rol (safe_add (spear_add (spear_add (a, q), spear_add (x, t), s), b);} função md5_ff (a, b, c, x, x, x, s), t); s); Função md5_gg (b, x, s, t);}} md5_gg (a, b, c, d, x, s, t) {return md5_cmn ((b & d) | (c & (~ d)), a, b, x, s, t);} função md5_hh (a, b, c, d, x, s, t);} md5_hh (a, b, c, d, x, x, t, t);} md5_hh (a, b, c, d, x, x, t); s; função b, x, s, t);} função md5_ii (a, b, c, d, x, s, t) {return md5_cmn (c ^ (b | (~ d)), a, b, x, s, t);}/ * * * calcule o hmac-md5, de uma chave e alguns dados */function_hm; str2binl (chave); 0x5c5c5c5c; Interpretadores. Retorno (NUM << CNT) | Chrsz; * 32; "0123456789ABCDEF"; (i%4) * 8)) e 0xf); "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 +/"; << 16) | B64PAD;sha1.js:
/ * * Uma implementação JavaScript do algoritmo de hash seguro, SHA-1, conforme definido * no pub 180-1 * versão 2.1-beta Copyright Paul Johnston 2000-2002. * Outros colaboradores: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distribuído sob a licença BSD * consulte HTPTP: detalhes. * // * * variáveis configuráveis. Pode ser necessário ajustá-los para serem compatíveis com * o lado do servidor, mas os padrões funcionam na maioria dos casos. */var hexcase = 0; /* Formato de saída hexadecimal. 0 - minúscula; 1 - Uppercase */var B64pad = ""; /* Caractere de Base-64 PAD. "=" Para conformidade rfc estrita */var Chrsz = 8; /* bits por caractere de entrada. 8 - ASCII; 16 - Unicode * // * * Essas são as funções que você normalmente deseja chamar *, eles recebem argumentos de string e retornam hexadecimal ou strings codificadas base -64 */função hex_sha1 (s) {retorna binb2hex (core_sha1 (str2binb (s), s.lng *) binb2b64 (core_sha1 (str2binb (s), s.length * chrsz));} função str_sha1 (s) {return binb2str (core_sha1 (str2binb (s), s.length * chrsz);} função dados));} função b64_hmac_sha1 (chave, dados) {return binb2b64 (core_hmac_sha1 (chave, dados));} função str_hmac_sha1 (key, dados));} function; Auto-teste simples para ver se a VM está funcionando */função sha1_vm_test () {return hex_sha1 ("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d"/}/ * calcule o sha-1-1 do shay-1 do shay-1 do shay-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA-1 do SHA1. core_sha1 (x, len) { / * Anexe o preenchimento * / x [len >> 5] | = 0x80 << (24 - Len % 32); x [((len + 64 >> 9) << 4) + 15] = len; var w = matriz (80); var a = 1732584193; var B = -271733879; var c = -1732584194; var d = 271733878; var e = -1009589776; for (var i = 0; i <x.length; i += 16) {var antigo = a; var OldB = B; var Oldc = C; var antigo = d; var antigo = e; for (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 (spear_add (e, w [j]), sha1_kt (j))); e = d; d = c; c = rol (b, 30); b = a; a = t; } a = spear_add (a, antiga); b = safe_add (b, OldB); c = safe_add (c, antigo); d = safe_add (d, antigo); e = safe_add (e, antigo); } Retorno Array (A, B, C, D, E);}/ * * Faça a função de combinação de trigêmeos apropriada para a atual iteração */função SHA1_FT (T, B, C, D) {if (t <20) Return (B & C) | ((~ b) & d); if (t <40) retornar b ^ c ^ d; se (t <60) retornar (B&C) | (B&D) | (cd); Retornar B ^ c ^ d;}/ * * Determine a constante adicional apropriada para a iteração atual */função sha1_kt (t) {return (t <20)? 1518500249: (t <40)? 1859775393: (t <60)? -1894007588: -899497514;}/ * * Calcule o hmac -sha1 de uma chave e alguns dados */function core_hmac_sha1 (chave, dados) {var bkey = str2binb (key); if (bkey.length> 16) bkey = core_sha1 (bkey, key.length * chrsz); var ipad = matriz (16), opad = matriz (16); for (var i = 0; i <16; i ++) {ipad [i] = bkey [i] ^ 0x36363636; opad [i] = bkey [i] ^ 0x5c5c5c5c; } var hash = core_sha1 (ipad.concat (str2binb (dados)), 512 + data.length * chrsz); Return Core_Sha1 (opad.Concat (hash), 512 + 160);}/ * * Adicionar números inteiros, envolvendo 2^32. Isso usa operações de 16 bits internamente * para trabalhar em torno de bugs em alguns intérpretes do JS. */function spear_add (x, y) {var lsw = (x & 0xffff) + (y & 0xffff); var msw = (x >> 16) + (y >> 16) + (LSW >> 16); Retornar (RSU << 16) | (LSW & 0XFFFF);}/ * * Gire bittude um número de 32 bits para a esquerda. */função rol (num, cnt) {return (num << cnt) | (num >>> (32-cnt));}/ * * Converta uma corda de 8 bits ou 16 bits em uma matriz de palavras grandes endianas * Na função de 8 bits, caracteres> 255 têm seu hi-byte silenciosamente ignorado. */function str2binb (str) {var bin = array (); var máscara = (1 << Chrsz) - 1; for (var i = 0; i <str.Length * Chrsz; i += chrsz) bin [i >> 5] | = (str.charcodeat (i / chrsz) e máscara) << (24 - i % 32); retornar bin;}/ * * Converta uma matriz de palavras grandes endianas em uma string */função binb2str (bin) {var str = ""; var máscara = (1 << Chrsz) - 1; for (var i = 0; i <bin.length * 32; i += chrsz) str += string.fromCharcode ((bin [i >> 5] >>> (24 - i % 32)) e máscara); Retorne str;}/ * * Converta uma matriz de palavras-grandes em uma sequência hexadecimal. */function binb2hex (binarray) {var hex_tab = hexcase? "0123456789ABCDEF": "0123456789ABCDEF"; var str = ""; para (var i = 0; i <binarray.length * 4; i ++) {str += hex_tab.charat ((binarray [i >> 2] >> ((3 - i % 4) * 8 +4) e 0xf) +hex_tab.charat ((binarray [i>> 0>>>> >> } return str;}/* * Convert an array of big-endian words to a base-64 string */function binb2b64(binarray) { var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var str = ""; for (var i = 0; i <binário.length * 4; i += 3) {var triplet = (((binary [i >> 2] >> 8 * (3 - i % 4) e 0xff) << 16) | (((BINARRAY [i + 1 >> 2] >> 8 * (3 - (i + 1) % 4)) e 0xff) << 8) | ((BINARRAY [i + 2 >> 2] >> 8 * (3 - (i + 1) % 4)) e 0xff) << 8) | ((BINARRAY [i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) e 0xFF); for (var j = 0; j <4; j ++) {if (i * 8 +j * 6> binário.length * 32) str += b64pad; else str += tab.charat ((trigêmeo >> 6 * (3 - j)) & 0x3f); }} retornar str;}Amigos interessados em criptografia JavaScript também podem se referir às ferramentas on -line deste site :
Ferramenta de criptografia online do MD5
Ferramenta de criptografia/descriptografia de escape
Ferramenta de criptografia sha1 online
Tonder, Express, Tornado URL Criptografia/Decripção Ferramentas
Segurança de senha detecção online
Gerador de senha de alta resistência
Para obter mais informações sobre JavaScript, consulte os tópicos deste site: "Resumo da criptografia JavaScript e habilidades de descriptografia", "Resumo dos efeitos e habilidades especiais de troca de JavaScript", "Resumo de Javascript Sumts, Sestrict Errringscript Skills", Summars e Javscript Effects and Skills "," Summary Of Javcript Skills " Habilidades de algoritmo "," Resumo do algoritmo e habilidades de travessia de JavaScript "e" Resumo do Uso da Operação Matemática JavaScript "
Espero que este artigo seja útil para a programação JavaScript de todos.