简体   繁体   English

如何让google crypto.js cypher输出一个字符串?

[英]how to make google crypto.js cypher output a string?

I want to have an offline webpage containing javascript that will encrypt a string with a key and result in another string. 我想要一个包含javascript的离线网页,该网页将使用密钥加密字符串并生成另一个字符串。 Then I want to do the reverse. 然后我想反过来。

This is a simple tool that I would run offline so I would prefer to keep the conversation away from client side encryption. 这是一个简单的工具,我将脱机运行,所以我宁愿让对话远离客户端加密。

For now i've chosen google crypto.js as my cypher and for this example I'm using Rabbit 现在我选择google crypto.js作为我的密码,对于这个例子我使用的是Rabbit

here is my jdfiddle 这是我的jdfiddle

http://jsfiddle.net/Pz3ab/ http://jsfiddle.net/Pz3ab/

which uses jquery and the following external resource 它使用jquery和以下外部资源

http://code.google.com/p/crypto-js/#Rabbit http://code.google.com/p/crypto-js/#Rabbit

Where am I going wrong? 我哪里错了?

<div>key<br><input class="key" type="text"></div>
<div>readout<br><input class="readout" type="text"></div>
<div>
    <div class="button encrypt">encrypt</div>
    <div class="button decrypt">decrypt</div>
</div>
<div class="error"><div>

var key,readout;
$('.encrypt').on('click', function(){
    if (init()) {
        console.log('-- encrypt clicked --');
        console.log('key = ', key);
        console.log('readout = ', readout);
        var answer = CryptoJS.Rabbit.encrypt(readout, key);
        answer = answer.toString();
        console.log('answer = ',answer);
        $('.readout').val(answer);
    }
});
$('.decrypt').on('click', function(){
    if (init()) {
        console.log('-- decrypt clicked --');
        console.log('key = ', key);
        console.log('readout = ', readout);
        var answer=CryptoJS.Rabbit.decrypt(readout, key);
        answer = answer.toString();
        console.log('answer = ',answer);
        $('.readout').val(answer);
    }
});
function init(){
    key = '' + $('.key').val();
    readout = '' + $('.readout').val();
    console.log('-- init, error check, get key and readout --');
    console.log('key = ', key);
    console.log('readout = ', readout);
    $('.error').empty();
    var success = true;
    if (key=="") {$('.error').append('key is empty<br>');success=false;}
    if (readout=="") {$('.error').append('readout is empty<br>');success=false;}
    return success;
}

Short answer: answer.toString( CryptoJS.enc.Utf8 ); 简答: answer.toString( CryptoJS.enc.Utf8 );

Long answer: The decrypted otuput you're seeing is the original readout string encoded as hex. 答案很长:你看到的解密输出是编码为十六进制的原始读出字符串。 The reason it's encoded as hex is because the cipher algorithms have no way to know the original character encoding. 它被编码为十六进制的原因是因为密码算法无法知道原始字符编码。 Was it Latin1? 是Latin1吗? Utf8? UTF8? Utf16? UTF-16? Etc. By passing the Utf8 encoder to the toString method, we can tell it to use that character encoding. 等等。通过将Utf8编码器传递给toString方法,我们可以告诉它使用该字符编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM