JavaScript AES 加解密(高级加密标准) [英] JavaScript AES encryption and decryption (Advanced Encryption Standard)

查看:33
本文介绍了JavaScript AES 加解密(高级加密标准)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 JavaScript 中使用 AES(高级加密标准)实现加密和解密.

How to implement encryption and decryption using AES (Advanced Encryption Standard) in JavaScript.

为什么是 AES(高级加密标准)?

Why AES (Advanced Encryption Standard) ?

安全性: 与其他提交的密码相比,竞争算法将根据它们抵抗攻击的能力来判断,尽管安全强度被认为是竞争中最重要的因素.

Security: Competing algorithms were to be judged on their ability to resist attack, as compared to other submitted ciphers, though security strength was to be considered the most important factor in the competition.

成本:打算在全球、非排他性和免版税的基础上发布,候选算法将在计算和内存效率方面进行评估.

Cost: Intended to be released under a global, nonexclusive and royalty-free basis, the candidate algorithms were to be evaluated on computational and memory efficiency.

推荐答案

AES 是一种非常简单而强大的加密和解密方法.请参阅我下面的示例,它很容易在您准备好的代码中使用.

AES is very Simple and powerful encryption and decryption method. Please see my below example that will very easy to use in your ready code.

只需要调用encryptMessagedecryptMessage 函数即可.我已经在下面提供了运行示例.

Just need to call encryptMessage and decryptMessage fnuction. I already provided running example below.

如何调用这些方法:

code.encryptMessage('Welcome to AES !','your_password');
code.decryptMessage('U2FsdGVkX1/S5oc9WgsNyZb8TJHsuL7+p4yArjEpOCYgDTUdkVxkmr+E+NdJmro9','your_password')

let code = (function(){
    return{
      encryptMessage: function(messageToencrypt = '', secretkey = ''){
        var encryptedMessage = CryptoJS.AES.encrypt(messageToencrypt, secretkey);
        return encryptedMessage.toString();
      },
      decryptMessage: function(encryptedMessage = '', secretkey = ''){
        var decryptedBytes = CryptoJS.AES.decrypt(encryptedMessage, secretkey);
        var decryptedMessage = decryptedBytes.toString(CryptoJS.enc.Utf8);

        return decryptedMessage;
      }
    }
})();

console.log(code.encryptMessage('Welcome to AES !','your_password'));
console.log(code.decryptMessage('U2FsdGVkX1/S5oc9WgsNyZb8TJHsuL7+p4yArjEpOCYgDTUdkVxkmr+E+NdJmro9','your_password'))

<!DOCTYPE html>
<html>
<head>
	<title>E2EE</title>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
</head>
<body>

</body>
</html>

您也可以参考我的 github 代码库以获取更多参考.

You can also refer my github code repository for more references.

https://github.com/shedagemayur/JavaScriptCode/tree/master/AES

这篇关于JavaScript AES 加解密(高级加密标准)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆