Cet article décrit le cryptage et le décryptage de la base64 mis en œuvre par JS. Partagez-le pour votre référence, comme suit:
Code complet:
<Html> <A-Head> <Title> Base64 </ Title> <Script Language = JavaScript> var base64Encodechars = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 + /" -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 ,re -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, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -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, -1, -1, 2, 26, 29, 30 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); <html> <adread> <itle> base64 </tdicat> <script = javascript> var base64encocech = "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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ,re -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -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, -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); fonction Base64encode (str) {var out, i, leren; 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 + = "=="; casser; } 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 + = "="; casser; } 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;} 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) casser; / * c2 * / do {c2 = base64decodechars [str.charcodeat (i ++) & 0xff]; } while (i <len && c2 == -1); if (c2 == -1) casser; out + = string.fromCharcode ((c1 << 2) | ((C2 & 0x30) >> 4)); / * c3 * / do {c3 = str.charcodeat (i ++) & 0xff; if (c3 == 61) renvoie; C3 = base64decodechars [C3]; } while (i <len && c3 == -1); if (c3 == -1) casser; out + = string.fromCharcode (((c2 & 0xf) << 4) | ((C3 & 0x3c) >> 2)); / * c4 * / do {c4 = str.charcodeat (i ++) & 0xff; if (c4 == 61) retourner; C4 = base64decodechars [C4]; } while (i <len && c4 == -1); if (c4 == -1) casser; out + = string.fromCharcode (((C3 & 0x03) << 6) | C4); } return out;} fonction utf16to8 (str) {var out, i, len, c; out = ""; Len = str.length; pour (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;} fonction utf8to16 (str) {var out, i, len, c; var char2, char3; out = ""; Len = str.length; i = 0; while (i <len) {c = str.charcodeat (i ++); commutateur (c >> 4) {cas 0: cas 1: cas 2: cas 3: cas 4: cas 5: cas 6: cas 7: // 0xxxxx out + = str.charat (i-1); casser; Cas 12: cas 13: // 110x xxxx 10xx xxxx char2 = str.charcodeat (i ++); out + = string.fromCharcode (((c & 0x1f) << 6) | (char2 & 0x3f)); casser; cas 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)); casser; }} return out;} function DOIT () {var f = document.f f.output.value = base64encode (utf16to8 (f.source.value)) f.decode.value = utf8to16 (base64decode (f.output.value))} </cript> </read> Name = "f"> code d'origine <br> <textarea name = "source" Rows = 4 Cols = 60 wrap = "Soft"> </ TextArea> <br> <br> Base64 Encode <br> <TextArea Name = "Output" Rows = 4 Cols = 60 Wrap = "Soft"> </ TextArea> <br> <br> Base64 Decode <br> <Textara Namea = "Decode" Rows = 4 ColoS = 64 Wrap = "soft"> </ textarea> <br> <br> <entrée type = Button Value = "Convert" onclick = "DOIT ()"> </form> </odyfing>Pour le cryptage et le décryptage de la base64, vous pouvez également vous référer aux outils en ligne de ce site :
Outil de codage et de décodage de base64
Pour un contenu plus crypté et décrypté, veuillez vous référer à:
Outil de cryptage en ligne MD5
Outil de chiffrement hexadécimal URL
Outils de cryptage / décryptage Thunder, Express, Tornado URL
Outil de cryptage SHA1 en ligne
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.