ali*_*ody 9 javascript php base64 aes cryptojs
我正在尝试创建一个简单的网页,其目标是向服务器发送和加密消息(将创建包含该内容的文件),然后创建一个链接,并且接收所提供链接的用户将能够看到加密value(因为它提供了文件名和密钥).
使用CryptoJS AES对消息进行加密,结果是Base64编码后进行解码,只有加密消息的Base64和加密消息才会被发送到服务器,这是使用Javascript完成的.
我的问题是.我有一条消息,让我说使用Base64编码的"Hello World",它给了我这个:
1ffffffff5a8ae57
Run Code Online (Sandbox Code Playgroud)
如果我将此值发送给变量,然后只使用该变量,则显示结果:
// Works !
var test = CryptoJS.enc.Base64.parse("Hello World");
alert(CryptoJS.enc.Base64.stringify(test));
Run Code Online (Sandbox Code Playgroud)
这是正常的.但是如果我尝试直接写文本(或者只是做一个toString(),它就不起作用......这也是正常的,因为'test'变量不是一个简单的String变量):
// Doesn't work !
var test = CryptoJS.enc.Base64.parse("Hello World").toString();
alert(CryptoJS.enc.Base64.stringify(test));
Run Code Online (Sandbox Code Playgroud)
但我需要使用一个String,因为它基于PHP $ _GET值,然后再使用Javascript进行解码.所以我的问题是,我怎么能这样做才能编码一个字符串,然后将结果解码为一个字符串?
这是我的engine.js文件:
// 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 !");
}
}
Run Code Online (Sandbox Code Playgroud)
dec*_*tes 41
我遇到了类似的困惑,作为参考,这是解决方案.
要将文本字符串(UTF-8编码)转换为base-64字符串,您需要:
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='
Run Code Online (Sandbox Code Playgroud)
要将base-64编码的字符串转换回文本(UTF-8编码),它是:
var base64 = 'SGVsbG8gd29ybGQ=';
var words = CryptoJS.enc.Base64.parse(base64);
var textString = CryptoJS.enc.Utf8.stringify(words); // 'Hello world'
Run Code Online (Sandbox Code Playgroud)
从CryptoJS文档中给出的示例中可以看出,parse用于以编码器期望的格式解析字符串(进入WordArray),stringify将WordArray转换为字符串.
从文档:
var words = CryptoJS.enc.Base64.parse('SGVsbG8sIFdvcmxkIQ==');
var base64 = CryptoJS.enc.Base64.stringify(words); // 'Hello, World!'
Run Code Online (Sandbox Code Playgroud)
WordArray是CryptoJS独立于格式的数据表示.Formatters(如Base64和Utf8)是此WordArray格式与字符串之间的接口,字符串可能包含以任何格式编码的数据.因此,要在格式之间进行更改,您需要在任一端使用格式化程序,一个解析和一个字符串化(即编码).在这种情况下,您需要记住,当我们编写"Hello World"时,该文本以特定格式编码(我假设为UTF-8).
我发现这个Gist很有帮助.
| 归档时间: |
|
| 查看次数: |
33297 次 |
| 最近记录: |