web stats
CryptoJS - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 02-26-2014, 07:42 AM
skekasaurus skekasaurus is offline
OBX.3 Kenobi
 
Join Date: Dec 2013
Posts: 145
skekasaurus is on a distinguished road
Default 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!

Last edited by skekasaurus; 02-27-2014 at 12:41 PM.
Reply With Quote
  #2  
Old 02-26-2014, 08:00 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,124
narupley is on a distinguished road
Default

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.
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #3  
Old 02-26-2014, 08:56 AM
skekasaurus skekasaurus is offline
OBX.3 Kenobi
 
Join Date: Dec 2013
Posts: 145
skekasaurus is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 02-26-2014, 09:01 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,124
narupley is on a distinguished road
Default

Quote:
Originally Posted by skekasaurus View Post
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.
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #5  
Old 02-26-2014, 10:18 AM
skekasaurus skekasaurus is offline
OBX.3 Kenobi
 
Join Date: Dec 2013
Posts: 145
skekasaurus is on a distinguished road
Default

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?
Reply With Quote
  #6  
Old 02-26-2014, 10:23 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,124
narupley is on a distinguished road
Default

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>
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #7  
Old 02-26-2014, 10:54 AM
skekasaurus skekasaurus is offline
OBX.3 Kenobi
 
Join Date: Dec 2013
Posts: 145
skekasaurus is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 02-26-2014, 11:12 AM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,124
narupley is on a distinguished road
Default

Quote:
Originally Posted by skekasaurus View Post
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.
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #9  
Old 02-27-2014, 12:34 PM
skekasaurus skekasaurus is offline
OBX.3 Kenobi
 
Join Date: Dec 2013
Posts: 145
skekasaurus is on a distinguished road
Default

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!

Last edited by skekasaurus; 02-27-2014 at 12:40 PM.
Reply With Quote
  #10  
Old 07-31-2014, 08:54 AM
skekasaurus skekasaurus is offline
OBX.3 Kenobi
 
Join Date: Dec 2013
Posts: 145
skekasaurus is on a distinguished road
Default

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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 01:27 PM.


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