使用 CryptoJS 解码 Base64 字符串
我正在尝试创建一个简单的网页,目的是向服务器发送和加密的消息(这将创建一个包含该内容的文件),然后创建一个链接,接收所提供链接的用户将能够查看加密值(因为它提供了文件名和密钥).
I am trying to create a simple webpage with the goal to send and encrypted message to the server (which will create a file with that content), then a link is created and the user who receive the link provided will be able to see the encrypted value (since it provides the name of the file and the key).
消息使用 CryptoJS AES 加密,结果是 Base64 编码后解码,只有加密消息的 Base64 和加密消息发送到服务器没有别的,这是使用 Javascript 完成的.
The message is encrypted using CryptoJS AES, and the result is Base64 encoded to be decoded after that, only the Base64 of the encrypted message and the encrypted message is sent to the server nothing else, and this is done using Javascript.
我的问题是.我有一条消息,假设我使用 Base64 编码的Hello World",它给了我这个:
My question here is. I have a message, let say "Hello World" which I encode using Base64, and it gives me this :
1ffffffff5a8ae57
如果我将此值发送给一个变量,然后只使用该变量,它会显示一个结果:
If I send this value to a variable, and then just use that variable, it show a result :
// Works !
var test = CryptoJS.enc.Base64.parse("Hello World");
alert(CryptoJS.enc.Base64.stringify(test));
这是正常的.但是如果我尝试直接写文本(或者只是做一个toString(),它不起作用......这也是正常的,因为'test'变量不是一个简单的字符串变量):
Which is normal. But If I try to write directly the text (or just do a toString(), it doesn't work... which is also normal because the 'test' variable isn't a simple String variable) :
// Doesn't work !
var test = CryptoJS.enc.Base64.parse("Hello World").toString();
alert(CryptoJS.enc.Base64.stringify(test));
但我需要使用字符串,因为它基于 PHP $_GET 值,然后再次使用 Javascript 对其进行解码.所以我的问题是,我怎样才能做到这一点才能编码一个字符串,然后将结果解码为一个字符串?
But I need to use a String since it is based on a PHP $_GET Value which is then decoded using Javascript again. So my question is, how can I do this in order to encode a String and then decoded the result as a String ?
这是我的 engine.js 文件:
This is my engine.js file :
// Encrypt the message using a generated key
function encrypt(message, key) {
return CryptoJS.AES.encrypt(message, key);
}
// Encode String to Base64
function encodeBase64(value) {
return CryptoJS.enc.Base64.parse(value.toString());
}
// Decode String from Base64 Enconding
function decodeBase64(encodedValue) {
return CryptoJS.enc.Base64.stringify(encodedValue);
}
// Decrypt the message using the generated key
function decrypt(encrypted, key) {
return CryptoJS.AES.decrypt(encrypted, key).toString(CryptoJS.enc.Utf8);
}
// Generate the random key
function generateKey() {
return CryptoJS.lib.WordArray.random(16).toString();
}
// Generate the random fileName
function generateFileName() {
return CryptoJS.lib.WordArray.random(16).toString();
}
// Convert the text on the form with the encrypted version to be sent into the server
function SendMessage(message, FinalURL) {
if ((message.value).trim()) {
var xmlhttp = new XMLHttpRequest;
xmlhttp.open("POST", "index.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Generate the Key and Encrypt the Message
var key = generateKey();
var encryptedData = encrypt(message.value, key);
var fileName = generateFileName();
xmlhttp.send("fileName=" + fileName + "&encryptedMsg=" + encodeBase64(encryptedData));
var finalURL = document.URL + "?MessageID=" + fileName + "&Key=" + key;
FinalURL.innerHTML = "<p>Final URL: <a href=" + finalURL + ">" + finalURL + "</a></p>";
} else {
alert("There is no text to be encrypted !");
}
}
推荐答案
我遇到了类似的困惑,作为参考,这里是解决方案.
I ran into a similar confusion, and for reference, here is the solution.
要将文本字符串(UTF-8 编码) 转换为 base-64 字符串,您需要:
To turn a text string (UTF-8 encoded) into a base-64 string, you need:
var textString = 'Hello world'; // Utf8-encoded string
var words = CryptoJS.enc.Utf8.parse(textString); // WordArray object
var base64 = CryptoJS.enc.Base64.stringify(words); // string: 'SGVsbG8gd29ybGQ='
要将 base-64 编码字符串转回 文本(UTF-8 编码),它是:
var base64 = 'SGVsbG8gd29ybGQ=';
var words = CryptoJS.enc.Base64.parse(base64);
var textString = CryptoJS.enc.Utf8.stringify(words); // 'Hello world'
一些解释
您可以从 CryptoJS 文档中给出的示例中看到,parse 旨在以编码器期望的格式解析字符串(转换为 WordArray),而 stringify 将 WordArray 转换为字符串.
Some explanation
As you can see from the examples given in the CryptoJS documentation, parse is meant to parse a string in the format that the encoder is expecting (into a WordArray), and stringify turns a WordArray into a string.
来自文档:
var words = CryptoJS.enc.Base64.parse('SGVsbG8sIFdvcmxkIQ==');
var base64 = CryptoJS.enc.Base64.stringify(words); // 'Hello, World!'
WordArray 是 CryptoJS 的与格式无关的数据表示.格式化程序(如 Base64 和 Utf8)是这种 WordArray 格式和字符串之间的接口,字符串可能包含以任何格式编码的数据.因此,要在格式之间进行更改,您需要在任一端使用格式化程序,一个解析和一个字符串化(即编码).在这种情况下,您需要记住,当我们编写Hello World"时,它是以特定格式编码的文本(我假设为 UTF-8).
The WordArray is CryptoJS's format-independent representation of data. Formatters (like Base64 and Utf8) are interfaces between this WordArray format, and strings, which may contain data encoded in any format. So to change between formats, you need a formatter at either end, one parsing and one stringifying (i.e. encoding). In this case, you need to remember that when we write 'Hello World', that's text encoded in a particular format (I'm assuming UTF-8).
我发现这个要点很有帮助.
相关文章