Mirth Community

Mirth Community (http://www.mirthcorp.com/community/forums/index.php)
-   Support (http://www.mirthcorp.com/community/forums/forumdisplay.php?f=6)
-   -   CryptoJS (http://www.mirthcorp.com/community/forums/showthread.php?t=10124)

skekasaurus 02-26-2014 07:42 AM

Decrypt an AES encrypted string
 
I'm in the need of decrypting a string that was encrypted using AES. I was thinking of using the CryptoJS library (https://code.google.com/p/crypto-js).

Currently struggling with how I can import this into Mirth to be able to use it. The only way I've seen to import external libraries is by

Code:

importPackage(Packages.org.apache.http.client);
tried using the URL between the parentheses but got an error that it doesn't accept http strings. So my question is how can I import this libary? Will I need to make something similar in a java jar and add that in my lib/custom folder?

Any other recommendations of how I can decrypt a string that used AES for encryption are welcomed!

narupley 02-26-2014 08:00 AM

You can include it with an External Script step, or if you want it to be included with all channels you can do it with a function code template.

skekasaurus 02-26-2014 08:56 AM

I'll definitely need it available to multiple channels, so a code template will be the way to go. But I'm unsure how exactly to include it. If I download the "aes.js" from http://code.google.com/p/crypto-js/s...rollups/?r=636, do I copy&paste the contents into function field of the code template?

narupley 02-26-2014 09:01 AM

Quote:

Originally Posted by skekasaurus (Post 37807)
I'll definitely need it available to multiple channels, so a code template will be the way to go. But I'm unsure how exactly to include it. If I download the "aes.js" from http://code.google.com/p/crypto-js/s...rollups/?r=636, do I copy&paste the contents into function field of the code template?

Yep, that should work.

skekasaurus 02-26-2014 10:18 AM

Okay so i got the javascript library as a code template. I know this is probably outside the scope of how far you can assist with 3rd party stuff, but I'm doing a test run of using the code template.

Following this guide: http://code.google.com/p/crypto-js/#AES

I tried encrypting a simple string, map it to a variable and have the filewriter throw it into a text file.

Initially, I did this:

Code:

var x = new CryptoJS;

var encrypted = CryptoJS.AES.encrypt('Message', 'Secret Passphrase');
var decrypted = CryptoJS.AES.decrypt(encrypted, 'Secret Passphrase');

connectorMap.put('test', decrypted);

But I was getting an exception saying that I couldn't do "var x = new CryptoJS;" because it's an object no a function. So i removed that line and the channel successfully wrote to a file, however it outputted the type rather than my string. This is what I got in the file "[object Object]"

Any idea what I might be doing wrong?

narupley 02-26-2014 10:23 AM

From the same exact page:

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

Quote:

The Cipher Output

The plaintext you get back after decryption is a WordArray object. See Hashers' Output for more detail.
http://code.google.com/p/crypto-js/#The_Hasher_Output

Quote:

The Hasher Output

The hash you get back isn't a string yet. It's a WordArray object. When you use a WordArray object in a string context, it's automatically converted to a hex string.

HTML Code:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>
<script>
    var hash = CryptoJS.SHA256("Message");

    alert(typeof hash); // object

    alert(hash); // 2f77668a9dfbf8d5848b9eeb4a7145ca94c6ed9236e4a773f6dcafa5132b2f91
</script>

You can convert a WordArray object to other formats by explicitly calling the toString method and passing an encoder.

HTML Code:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
<script>
    var hash = CryptoJS.SHA256("Message");

    alert(hash.toString(CryptoJS.enc.Base64)); // L3dmip37+NWEi57rSnFFypTG7ZI25Kdz9tyvpRMrL5E=

    alert(hash.toString(CryptoJS.enc.Latin1)); // /wf�����JqE��6sܯ+/�

    alert(hash.toString(CryptoJS.enc.Hex)); // 2f77668a9dfbf8d5848b9eeb4a7145ca94c6ed9236e4a773f6dcafa5132b2f91
</script>



skekasaurus 02-26-2014 10:54 AM

Don't know how I missed that, but that was the little detail that was throwing everything off.


Thank you Nick, as always you were a HUGE help.

narupley 02-26-2014 11:12 AM

Quote:

Originally Posted by skekasaurus (Post 37811)
Don't know how I missed that, but that was the little detail that was throwing everything off.


Thank you Nick, as always you were a HUGE help.

No prob!

Feel free, if you have time, to post your solution after you get everything working, as I know that encrypting/decrypting in JavaScript transformers is something asked occasionally on these forums.

skekasaurus 02-27-2014 12:34 PM

Okay so I'm back to wrap this up and provide the solution I used for working with encrypting values.

For doing encryption/decryption in Mirth, I used the 3rd party library CryptoJS. It's a pretty good little library that is fairly straight forward to work with.

I don't want to simply show how to encrypt/decrypt a string using only CryptoJS from within Mirth. That was shown above, and a guide can be found here

What I do want to provide, is how I was able to take an encrypted string from another application (e.g. asp.net website) and decrypt it so that its original value can be used inside of Mirth.

----

For the .NET encryption, I took advantage of the existing AES class to encrypt my string. Another good guide for this is here

Once my string was encrypted, I would feed it to Mirth inside a message, extract the encrypted string, do my decryption with CryptoJS and map the value to a connectormap variable. Also, I should mention that you will need to know what the IV/Key are or be able to calculate them so that you can decrypt the string.

Code:

var encryptedString = msg.ZOA['ZOA.1']['ZOA.1.1'].toString(); //extract encrypted string

var iv = CryptoJS.enc.Hex.parse('BA1094C5BCF9E404302CB9D35A842A59');
var key = CryptoJS.enc.Hex.parse('D91A8C897194F22AB09D19760ABC5E0C466089061D33DC72949ED986D5AFF92E');

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

//map
connectorMap.put('decryptedString', decryptedString.toString(CryptoJS.enc.Utf8));

And Voila!

skekasaurus 07-31-2014 08:54 AM

Recently ran into a problem using CryptoJS and wanted to post here in case I can save anyone some time who experiences it or is new to using CryptoJS.

Also, I'm now using Mirth Connect v3.0.3 and not 100% sure if it's related to the upgrade but I believe it is.

Back when using v2.2.3 I was able to map an encrypted string and pull it back out and directly pass it to the decrypt function. This worked flawlessly with no issues. This is how I used to do it:
Code:

var encryptedString = $('password');
CryptoJS.AES.decrypt(encryptedString, key);


Now in v3.0 when I pass the mapped encrypted string to the decrypt function, I get an empty value. This boggled me for a while but then I decided to have a look at the datatypes, since I was able to decrypt a string if I typed it into the function manually. Then I noticed that when the encrypted string is pulled, the datatype is of object. A simple cast to string and problem solved.

Code:

var encryptedString = String($('password'));
CryptoJS.AES.decrypt(encryptedString, key);

Easy fix, but took me a while to find out what was causing the problem to begin with.


All times are GMT -8. The time now is 06:34 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation