1. 程式人生 > >JavaScript Base64加解密

JavaScript Base64加解密

style 應用 fun decode turn a-z abcde dex log

Base64加密算法是網絡上最常見的用於傳輸8Bit字節代碼的編碼方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的詳細規範。Base64編碼可用於在HTTP環境下傳遞較長的標識信息。例如,在Java Persistence系統Hibernate中,就采用了Base64來將一個較長的唯一標識符(一般為128-bit的UUID)編碼為一個字符串,用作HTTP表單和HTTP GET URL中的參數。在其他應用程序中,也常常需要把二進制數據編碼為適合放在URL(包括隱藏表單域)中的形式。此時,采用Base64編碼具有不可讀性,即所編碼的數據不會被人用肉眼所直接看到。

下面是Base64加密、解密算法的簡單實現(不支持中文):

function base64Encrypt(baseString) {
    var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var length = baseString.length;
    var loopNo = parseInt(length / 3);
    var charOne, charTwo, charThree;
    var out = "";
    for (var i = 0, j = 0; j < loopNo; j++) {
        charOne 
= baseString.charCodeAt(i++); charTwo = baseString.charCodeAt(i++); charThree = baseString.charCodeAt(i++); out += base64Chars.charAt(charOne >> 2); out += base64Chars.charAt(((charOne & 0x3) << 4) | ((charTwo & 0xF0) >> 4)); out += base64Chars.charAt(((charTwo & 0xF) << 2) | ((charThree & 0xC0) >> 6)); out
+= base64Chars.charAt(charThree & 0x3F); } if (i === length) { return out; } charOne = baseString.charCodeAt(i++) & 0xff; out += base64Chars.charAt(charOne >> 2); if (i === length) { out += base64Chars.charAt((charOne & 0x3) << 4); out += "=="; } else { charTwo = baseString.charCodeAt(i++); out += base64Chars.charAt(((charOne & 0x3) << 4) | ((charTwo & 0xF0) >> 4)); out += base64Chars.charAt((charTwo & 0xF) << 2); out += "="; } return out; } function base64Encode(baseSting) { var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var length = baseSting.length; var chara1, chara2, chara3; var out = "", i = 0; while (i < length) { chara1 = baseSting.charCodeAt(i++) & 0xff; out += base64Chars.charAt(chara1 >> 2); if (i == length) { out += base64Chars.charAt((chara1 & 0x3) << 4); out += "=="; break; } chara2 = baseSting.charCodeAt(i++); if (i == length) { out += base64Chars.charAt(((chara1 & 0x3) << 4) | ((chara2 & 0xF0) >> 4)); out += base64Chars.charAt((chara2 & 0xF) << 2); out += "="; break; } chara3 = baseSting.charCodeAt(i++); out += base64Chars.charAt(((chara1 & 0x3) << 4) | ((chara2 & 0xF0) >> 4)); out += base64Chars.charAt(((chara2 & 0xF) << 2) | ((chara3 & 0xC0) >> 6)); out += base64Chars.charAt(chara3 & 0x3F); } return out; } function base64Decode(baseString) { var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var length = baseString.length; var charI, charII, charIII, charIV; var out = "", i = 0; if (0 < length && 0 === length % 4) { if (/^[A-Za-z0-9\+/]{2,}={0,2}$/.test(baseString)) { while (i < length - 4) { charI = base64Chars.indexOf(baseString.charAt(i++)); charII = base64Chars.indexOf(baseString.charAt(i++)); charIII = base64Chars.indexOf(baseString.charAt(i++)); charIV = base64Chars.indexOf(baseString.charAt(i++)); out += String.fromCharCode((charI << 2) | ((charII & 0x30) >> 4)); out += String.fromCharCode(((charII & 0xF) << 4) | ((charIII & 0x3C) >> 2)); out += String.fromCharCode(((charIII & 0x3) << 6) | charIV); } charI = base64Chars.indexOf(baseString.charAt(i++)); charII = base64Chars.indexOf(baseString.charAt(i++)); out += String.fromCharCode((charI << 2) | ((charII & 0x30) >> 4)); switch (baseString.indexOf("=")) { case length - 1: charIII = base64Chars.indexOf(baseString.charAt(i++)); out += String.fromCharCode(((charII & 0xF) << 4) | ((charIII & 0x3C) >> 2)); break; case -1: charIII = base64Chars.indexOf(baseString.charAt(i++)); charIV = base64Chars.indexOf(baseString.charAt(i++)); out += String.fromCharCode(((charII & 0xF) << 4) | ((charIII & 0x3C) >> 2)); out += String.fromCharCode(((charIII & 0x3) << 6) | charIV); } } else { throw new Error(baseString + " does not base64 encoded string."); } } else { throw new Error(baseString + " does not base64 encoded string."); } return out; }

JavaScript Base64加解密