简体   繁体   English

如果包装在函数中,CryptoJS 不会解密

[英]CryptoJS not decrypting if wrapped in a function

I am experiencing an issue that is blowing my mind.我遇到了一个让我大吃一惊的问题。 I have two equal pieces of code, with the only difference that I wrapped a call (that I will use often in my class) inside a function in one of them, while in the other is explicit.我有两段相同的代码,唯一的区别是我在其中一个函数中包装了一个调用(我将在我的类中经常使用),而另一个是显式的。 Well, the one piece works, while the other does not.好吧,一件作品有效,而另一件则不起作用。

This works fine:这工作正常:

public testSymmetricEncryption(): void {
        const original = 'Understanding at the alpha quadrant that is when conscious klingons yell.';
        const password = 'myPassword';
        const encrypted = CryptoJS.AES.encrypt(original, password).toString();
        const decrypted = CryptoJS.AES.decrypt(encrypted, password).toString(CryptoJS.enc.Utf8);

        console.log('Test symmetric encryption', {original, password, encrypted, decrypted});
    }

While this other does not work (no decrypted value give, ie '' ), and sometimes even an Error: Malformed UTF-8 data exception:虽然这个其他不起作用(没有解密的值给出,即'' ),有时甚至是Error: Malformed UTF-8 data异常:

public testSymmetricEncryption(): void {
        const original = 'Understanding at the alpha quadrant that is when conscious klingons yell.';
        const password = 'myPassword';
        const encrypted = this.encryptSymmetric(original, original);
        const decrypted = this.decryptSymmetric(encrypted, password);

        console.log('Test symmetric encryption', {original, password, encrypted, decrypted});
}

    public encryptSymmetric(value: string, password: string) {
        return CryptoJS.AES.encrypt(value, password).toString();
}

    public decryptSymmetric(encrypted: string, password: string) {
        return CryptoJS.AES.decrypt(encrypted, password).toString(CryptoJS.enc.Utf8);
}

Anybody has an idea why this is happening?有人知道为什么会这样吗?

I think you might have a typo here.我想你这里可能有错别字。 Instead of const encrypted = this.encryptSymmetric(original, original);而不是const encrypted = this.encryptSymmetric(original, original); , I think it should be const encrypted = this.encryptSymmetric(original, password); ,我觉得应该是const encrypted = this.encryptSymmetric(original, password);

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

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