德code。使用CryptoJS一个Base64字符串 [英] Decode a Base64 String using CryptoJS

查看:253
本文介绍了德code。使用CryptoJS一个Base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建目标的一个简单的网页来发送和加密的信息到服务器(这将创建与内容的文件),然后创建链接,谁接受提供的链接,用户将能够看到加密值(因为它提供了文件和密钥的名称)。

的消息是使用CryptoJS AES进行了加密,并且结果是Base64的连接coded到是德$ C $光盘在此之后,仅加密的消息和加密的消息的的Base64被发送到服务器没有别的,与此使用Javascript进行。

在这里我的问题是。我有一个消息,让说的Hello World,我带code。使用Base64编码,它给了我这样的:

  1ffffffff5a8ae57

如果我这个值发送给一个变量,然后只使用该变量,它显示的结果:

  //作品!
VAR测试= CryptoJS.enc.Base64.parse(的Hello World);
警报(CryptoJS.enc.Base64.stringify(试验));

这是正常的。但如果我尝试直接写的文字(或只是做一个toString(),它不工作......这也是正常的,因为测试变量不是一个简单的字符串变量):

  //不行!
VAR测试= CryptoJS.enc.Base64.parse(Hello World的)的toString()。
警报(CryptoJS.enc.Base64.stringify(试验));

但我需要,因为它是基于PHP的$ _GET值,然后再使用Javascript去coded到使用字符串。所以我的问题是,我怎么能做到这一点,以EN code String,然后去codeD的结果作为一个字符串?

这是我的engine.js文件:

  //使用生成的密钥加密消息
函数加密(消息,键){
    返回CryptoJS.AES.encrypt(消息密钥);
}// EN code字符串为Base64
功能连接codeBase64(值){
    返回CryptoJS.enc.Base64.parse(value.toString());
}从Base64编码Enconding //德code字符串
功能去codeBase64(EN codedValue){
    返回CryptoJS.enc.Base64.stringify(EN codedValue);
}//使用生成的密钥解密消息
解密功能(加密密钥){
    返回CryptoJS.AES.decrypt(加密密钥)的ToString(CryptoJS.enc.Utf8);
}//生成随机密钥
功能generateKey(){
    返回CryptoJS.lib.WordArray.random(16)的ToString();
}//生成随机文件名
功能generateFileName(){
    返回CryptoJS.lib.WordArray.random(16)的ToString();
}//转换与加密版的窗体上的文本发送到服务器
函数SendMessage消息(消息,FinalURL){
    如果((message.value).trim()){
        VAR XMLHTTP =新XMLHtt prequest;
        xmlhttp.open(POST,的index.php,真正的);
        xmlhttp.setRequestHeader(内容类型,应用程序/ x-WWW的形式urlen codeD);        //生成密钥和加密消息
        VAR键= generateKey();
        VAR的EncryptedData =加密(message.value,键);
        变种文件名= generateFileName();        xmlhttp.send(文件名=+文件名+&放大器; encryptedMsg =EN + codeBase64(的EncryptedData));        VAR finalURL = document.URL +?邮件ID =+文件名+&放大器;关键=+键;        FinalURL.innerHTML =< P>最终网址:< A HREF =+ finalURL +>中+ finalURL +&下; / A>&下; / P>中;
    }其他{
        警报(没有被加密的文字!);
    }
}


解决方案

我遇到类似的困惑,以供参考,在这里是解决方案。

要转的文本字符串(UTF-8 EN codeD)基64字符串,您需要:

  VAR textString =世界,你好; // UTF8恩codeD字符串
VAR字= CryptoJS.enc.Utf8.parse(textString); // WordArray对象
VAR的base64 = CryptoJS.enc.Base64.stringify(字); //字符串:'SGVsbG8gd29ybGQ ='

要转基础-64连接codeD字符串文本(UTF-8 EN codeD),它是:

  VAR的base64 ='SGVsbG8gd29ybGQ =';
VAR字= CryptoJS.enc.Base64.parse的(Base64);
VAR textString = CryptoJS.enc.Utf8.stringify(字); // '你好,世界'

部分解释

你可以从 CryptoJS文档解析的意思来分析,该连接codeR期待(成WordArray)格式的字符串,而字符串化的变成一个WordArray为一个字符串。

从文档:

  VAR字= CryptoJS.enc.Base64.parse('SG​​VsbG8sIFdvcmxkIQ ==');
VAR的base64 = CryptoJS.enc.Base64.stringify(字); // '你好,世界!'

该WordArray是数据的CryptoJS的格式无关的再presentation。格式化程序(如Base64和UTF8)是和字符串这WordArray格式之间的接口,其中可能含有带任何格式codeD数据。因此,要格式之间进行转换,需要在两端格式化,一个解析和一个字符串化(即编码)。在这种情况下,你需要记住,当我们写的'Hello World,那是文字连接特定格式codeD(我假设UTF-8)。

我发现这吉斯特的帮助。

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).

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.

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));

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));

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 ?

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.

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='

To turn a base-64 encoded string back into text (UTF-8 encoded), it's:

var base64 = 'SGVsbG8gd29ybGQ=';
var words = CryptoJS.enc.Base64.parse(base64);
var textString = CryptoJS.enc.Utf8.stringify(words); // 'Hello world'

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.

From the documentation:

var words  = CryptoJS.enc.Base64.parse('SGVsbG8sIFdvcmxkIQ==');
var base64 = CryptoJS.enc.Base64.stringify(words); // 'Hello, World!'

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).

I found this Gist helpful.

这篇关于德code。使用CryptoJS一个Base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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