JavaScript 实现的base64加密、md5加密、sha1加密及AES加密

1. Base64加密

1). js-base64
2). 安装
npm install --save js-base64
3). 使用
<!-- npm install --save js-base64 -->
<script type="text/javascript" src="base64.min.js"></script>
<script type="text/javascript">
    // 加密
    var str = Base64.encode("mazaiting");
    console.log("base64 encode: " + str);

    // 解密
    str = Base64.decode(str);
    console.log("base64 decode: " + str);
</script>

2. MD5加密

1). js-md5
2). 安装
npm install js-md5
3). 使用
<!-- npm install js-md5 -->
<script type="text/javascript" src="md5.min.js"></script>
<script type="text/javascript">
    // 加密
    var str = md5("mazaiting");
    console.log("md5 encode: " + str);
</script>

3. sha1加密

1). js-sha1
2). 安装
npm install js-sha1
3). 使用
<!-- npm install js-sha1 -->
<script type="text/javascript" src="sha1.min.js"></script>
<script type="text/javascript">
    // 加密
    var str = sha1("mazaiting");
    console.log("sha1 encode: " + str);
</script>

4. 自定义加解密

<script type="text/javascript">
    // 加密
    var str = compileStr("mazaiting");
    console.log("escape encode: " + str);

    // 解密
    str = uncompileStr(str);
    console.log("escape decode: " + str);

    // 对字符串进行加密
    function encode(code){     
        var c=String.fromCharCode(code.charCodeAt(0) + code.length);  
        for(var i = 1; i < code.length; i++) {        
            c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1));  
        }     
    return escape(c);  
    }  

    // 字符串进行解密
    function uncompileStr(code){        
        code = unescape(code);        
        var c = String.fromCharCode(code.charCodeAt(0)-code.length);        
        for(var i = 1; i < code.length; i++) {        
            c += String.fromCharCode(code.charCodeAt(i)-c.charCodeAt(i - 1));        
        }        
        return c;
    }  
</script>

5. AES加密

1). aes-js
2). 安装
npm install aes-js
3). 使用

CBC – Cipher-Block Chaining (recommended)

// An example 128-bit key
var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
 
// The initialization vector (must be 16 bytes)
var iv = [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,35, 36 ];
 
// 加密
// Convert text to bytes (text must be a multiple of 16 bytes)
var text = 'TextMustBe16Byte';
var textBytes = aesjs.utils.utf8.toBytes(text);
 
var aesCbc = new aesjs.ModeOfOperation.cbc(key, iv);
var encryptedBytes = aesCbc.encrypt(textBytes);
 
// To print or store the binary data, you may convert it to hex
var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
console.log(encryptedHex);
// "104fb073f9a131f2cab49184bb864ca2"
 
// 解密
// When ready to decrypt the hex string, convert it back to bytes
var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);
 
// The cipher-block chaining mode of operation maintains internal
// state, so to decrypt a new instance must be instantiated.
var aesCbc = new aesjs.ModeOfOperation.cbc(key, iv);
var decryptedBytes = aesCbc.decrypt(encryptedBytes);
 
// Convert our bytes back into text
var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
console.log(decryptedText);
// "TextMustBe16Byte"
    原文作者:_凌浩雨
    原文地址: https://www.jianshu.com/p/4c4f430cbce2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞