Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt (cryptojs) - Decrypt (.NET)

Passwords are going to be encrypted in .NET using AES and stored in a database. 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.

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

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.

This is what I have in 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". 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.

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

What my javascript looks like now:

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});
like image 698
jnasty Avatar asked Aug 23 '26 15:08

jnasty


1 Answers

Your code:

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

'Key' is an unexpected parameter, and should be removed.

I ported your code to 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=

Good luck :)

like image 145
wizulus Avatar answered Aug 26 '26 04:08

wizulus