JS Base64加密解密

目录

1. 核心代码

2. 完整代码演示

核心代码

function Base64() {
    
    

    // private property
    const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

    // utf-8加密
    const utf8Encode = function(str) {
    
    
    let string = str;
    string = string.replace(/\r\n/g, '\n');
    let utfText = '';
    for (let n = 0; n < string.length; n++) {
    
    
        const 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;
    };

    // utf-8解密
    const utf8Decode = function(utfText) {
    
    
    let string = '';
    let i = 0;
    let c = 0;
    let c2 = 0;
    let c3 = 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;
    };


    // base64加密
    this.encode = function(str) {
    
    
    let input = str;
    let output = '';
    let chr1;
    let chr2;
    let chr3;
    let enc1;
    let enc2;
    let enc3;
    let enc4;
    let i = 0;
    input = utf8Encode(input);
    while (i < input.length) {
    
    
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 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);
    }
    return output;
    };

    // base64解密
    this.decode = function(str) {
    
    
    let input = str;
    let output = '';
    let chr1;
    let chr2;
    let chr3;
    let enc1;
    let enc2;
    let enc3;
    let enc4;
    let 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 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output += String.fromCharCode(chr1);
        if (enc3 !== 64) {
    
    
        output += String.fromCharCode(chr2);
        }
        if (enc4 !== 64) {
    
    
        output += String.fromCharCode(chr3);
        }
    }
    output = utf8Decode(output);
    return output;
    };

    }


完整代码演示

<!DOCTYPE html>
<html>
 
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="">
    
</head>
 <style>
     .Int {
    
    
        width: 100%;
        height: 50px;
        margin-bottom: 30px;
     }
     .box {
    
    
         width: 600px;
         height: 300px;
         border: 1px solid pink;
         margin: 30px auto;
     }
 </style>
<body>
    
    <input id="Int" class="Int" />
    <button onclick="handleEncode()">点击查看Base64加密</button onclick="">
    <button onclick="handleDecode()">点击查看Base64解密</button>
    <div id="box" class="box"></div>
 
 
</body>

<script>
    function Base64() {
    
    

    // private property
    const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

    // private method for UTF-8 encoding
    const utf8Encode = function(str) {
    
    
    let string = str;
    string = string.replace(/\r\n/g, '\n');
    let utfText = '';
    for (let n = 0; n < string.length; n++) {
    
    
        const 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;
    };

    // private method for UTF-8 decoding
    const utf8Decode = function(utfText) {
    
    
    let string = '';
    let i = 0;
    let c = 0;
    let c2 = 0;
    let c3 = 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;
    };


    // base64加密
    this.encode = function(str) {
    
    
    let input = str;
    let output = '';
    let chr1;
    let chr2;
    let chr3;
    let enc1;
    let enc2;
    let enc3;
    let enc4;
    let i = 0;
    input = utf8Encode(input);
    while (i < input.length) {
    
    
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 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);
    }
    return output;
    };

    // base64解密
    this.decode = function(str) {
    
    
    let input = str;
    let output = '';
    let chr1;
    let chr2;
    let chr3;
    let enc1;
    let enc2;
    let enc3;
    let enc4;
    let 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 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output += String.fromCharCode(chr1);
        if (enc3 !== 64) {
    
    
        output += String.fromCharCode(chr2);
        }
        if (enc4 !== 64) {
    
    
        output += String.fromCharCode(chr3);
        }
    }
    output = utf8Decode(output);
    return output;
    };

    }

    // 实例化对象
    const base64 = new Base64()

    const input = document.getElementById("Int")
    const box = document.getElementById("box")

    // 加密
    function handleEncode() {
    
    
        const val = input.value
        console.log(val, 'val加密')
        const result = base64.encode(val)
        console.log(result, 'result加密')
        box.innerHTML = result
    }

    // 解密
    function handleDecode() {
    
    
        const val = input.value
        console.log(val, 'val解密')
        const result = base64.decode(val)
        console.log(result, 'result解密')
        box.innerHTML = result
    }
</script>
 
</html>

猜你喜欢

转载自blog.csdn.net/weixin_44953227/article/details/103959768