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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值