Html textarea更改编码的javascript字符串的值

时间:2016-12-31 21:42:05

标签: javascript html cryptojs

我正在使用Encryption CipherCryptoJS工作,以便为想要加密邮件的人们提供帮助。{3}}不幸的是,我遇到了一个问题,即解密任何使用" NoPadding"加密的消息。填充。经过数小时和数小时的程序测试,我终于找到了问题所在。我发现,当我将加密的javascript字符串附加到HTML textarea时,它改变了字符串的值。因此,当我尝试解密textarea中的值时,它没有工作。

    <textarea id="textarea"></textarea>
    <button onclick="encrypting_without_textarea()">encrypting without textarea</button>
    <button onclick="encrypting_with_textarea()">encrypting with textarea</button>

HTML

    function encrypting_without_textarea(){
        var textarea_element = document.getElementById("textarea");

        var encryptionKey = "fkjasdlfkdsanfknlasknflsjkanflnasjkfnjknskjnfnlsnf";
        var key  = CryptoJS.enc.Hex.stringify(encryptionKey);
        var value = "Multi-stability is the tendency of ambiguous perceptual experiences to move unstably back and forth between alternative interpretations. Some objects can be perceived in more than one way. An example from below in the section of figure/ground.";
        var paddingObject = CryptoJS.pad.NoPadding;
        var encrypted = CryptoJS.AES.encrypt(value, key, {mode:CryptoJS.mode.ECB, padding:paddingObject});

        var decrypted = CryptoJS.AES.decrypt(encrypted, key, {mode:CryptoJS.mode.ECB, padding:paddingObject});
        var msg = decrypted.toString(CryptoJS.enc.Utf8);
        alert(msg); 
    } 

上面的一个直接从加密的消息中解密并且它可以工作,下面的一个用来自textarea的值解密但是它返回一个错误。

             function encrypting_with_textarea(){
                var textarea_element = document.getElementById("textarea");

                var encryptionKey = "fkjasdlfkdsanfknlasknflsjkanflnasjkfnjknskjnfnlsnf";
                var key  = CryptoJS.enc.Hex.stringify(encryptionKey);
                var value = "Multi-stability is the tendency of ambiguous perceptual experiences to move unstably back and forth between alternative interpretations. Some objects can be perceived in more than one way. An example from below in the section of figure/ground.";
                var paddingObject = CryptoJS.pad.NoPadding;
                var encrypted = CryptoJS.AES.encrypt(value, key, {mode:CryptoJS.mode.ECB, padding:paddingObject});
                textarea_element.value = encrypted;

                var decrypted = CryptoJS.AES.decrypt(textarea_element.value, key, {mode:CryptoJS.mode.ECB, padding:paddingObject});
                var msg = decrypted.toString(CryptoJS.enc.Utf8);
                alert(msg); 
                //Error: Malformed UTF-8 data
            } 

这是plnkr link。我理解附加到textarea的价值有变化,但是怎么样?并且,我如何更改/编码字符串,以便代码可以在textarea中显示并复制,以便消息可以粘贴/解码而不会被破坏。我对任何解决方案持开放态度,越简单越好?

0 个答案:

没有答案