简体   繁体   English

加密(cryptojs) - 解密(.NET)

[英]Encrypt (cryptojs) - Decrypt (.NET)

Passwords are going to be encrypted in .NET using AES and stored in a database. 密码将使用AES在.NET中加密并存储在数据库中。 In another application, using javascript (on top of a Rhino engine) and the cryptojs library, I'll need to decrypt the password and then use it. 在另一个应用程序中,使用javascript(在Rhino引擎之上)和cryptojs库,我需要解密密码然后使用它。

The solution doesn't need to be complex, all I'm seeking is a simple implementation of how I can get these two guys to work together. 解决方案不需要复杂,我所寻求的只是一个简单的实现,我可以让这两个人一起工作。

Followed this guide: http://www.aspsnippets.com/Articles/AES-Encryption-Decryption-Cryptography-Tutorial-with-example-in-ASPNet-using-C-and-VBNet.aspx 遵循本指南: http//www.aspsnippets.com/Articles/AES-Encryption-Decryption-Cryptography-Tutorial-with-example-in-ASPNet-using-C-and-VBNet.aspx

Successfully encrypted a simple string ("SFTPPass") with a key of ("Key") in .NET, but once I get to decrypting in javascript not sure how to do it. 在.NET中用一键(“Key”)成功加密了一个简单的字符串(“SFTPPass”),但是一旦我在javascript中解密就不知道怎么做了。

This is what I have in javascript: 这是我在javascript中的内容:

var encryptedPass = 'StrmZO1Vjd8noHYzXs8hiQQBQDJZA5Bpg3t4BcmrViU=';

var iv = CryptoJS.enc.Base64.parse('1Ph1oX+N+q/kzpdyaIKBpA==');
var key = CryptoJS.enc.Base64.parse('k/W+Xeenh3kSLneZ/DYXVpvshGbsFVdyfOFdFTJb1yE=');

var SFTPPass = CryptoJS.AES.decrypt(encryptedPass, 'Key', key, {iv: iv});

However when I write my output to a file it's empty. 但是,当我将输出写入文件时,它是空的。

Any suggestions or alternative solutions are greatly welcomed! 任何建议或替代解决方案都非常欢迎!

EDIT: With the recommendation by alancnet, I'm now getting output but the string doesn't match the original which is "1234". 编辑:根据alancnet的推荐,我现在得到输出,但字符串与原来的“1234”不匹配。 So I followed the guide in the link above down to the wire using the same Key Phrase and input string. 所以我按照上面链接中的指南使用相同的关键短语和输入字符串向下连线。 Captured the hex of both the key & iv using BitConverter.toString in .NET. 使用.NET中的BitConverter.toString捕获key和iv的十六进制。

key = "752DA9F0D612540EEF1DE07A144781261A3D604DE339174ADAC5D5D6A695ABFF"
iv = "9714413594225F1D14FA02101C0D584D"

What my javascript looks like now: 我的javascript现在是什么样的:

var encryptedPass = 'Ao5ZnFYo344iWqv/Jr9euw==';

var iv = CryptoJS.enc.Hex.parse('9714413594225F1D14FA02101C0D584D');
var key = CryptoJS.enc.Hex.parse('752DA9F0D612540EEF1DE07A144781261A3D604DE339174ADAC5D5D6A695ABFF');

var decryptedString = CryptoJS.AES.decrypt(encryptedPass, key, {iv: iv});

Your code: 你的代码:

var SFTPPass = CryptoJS.AES.decrypt(encryptedPass, 'Key', key, {iv: iv});

'Key' is an unexpected parameter, and should be removed. 'Key'是一个意外的参数,应该删除。

I ported your code to Node: 我将您的代码移植到Node:

var CryptoJS = new require("cryptojs").Crypto;

var encryptedPass = 'StrmZO1Vjd8noHYzXs8hiQQBQDJZA5Bpg3t4BcmrViU=';

var iv = CryptoJS.util.base64ToBytes('1Ph1oX+N+q/kzpdyaIKBpA==');
var key = CryptoJS.util.base64ToBytes('k/W+Xeenh3kSLneZ/DYXVpvshGbsFVdyfOFdFTJb1yE=');

var SFTPPass = CryptoJS.util.bytesToBase64(
        CryptoJS.AES.decrypt(
            encryptedPass, 
            key, 
            {
                iv: iv, 
                asBytes: true
            }
        )
    );

console.log(SFTPPass);

and it ouput: UABfRxZLApVrt/t8JtoHMhCxfYUPWDwMLuBmWe50tDw= 它输出: UABfRxZLApVrt/t8JtoHMhCxfYUPWDwMLuBmWe50tDw=

Good luck :) 祝好运 :)

I figured it out, it was the format of how I was outputting the decrypted string. 我想通了,这是我输出解密字符串的格式。

Played around with multiple versions... 玩过多个版本......

 FileWrite(decryptedString);
 FileWrite(decryptedString.toString());

Ultimately what got it working was: 最终使它运作的是:

 FileWrite(decryptedString.toString(CryptoJS.enc.Utf8));

Mind you the lower case of "t" and "f", tried doing "UTF8" and it gave me completely different output as well. 请注意“t”和“f”的小写,尝试做“UTF8”,它也给了我完全不同的输出。

Thanks again to alancnet for pointing out the main mistake I was doing in trying to use an invalid parameter, i probably wouldn't have noticed that till later in the day. 再次感谢alancnet指出我在尝试使用无效参数时所犯的主要错误,我可能不会注意到直到当天晚些时候。

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

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