Announcement

Collapse
No announcement yet.

CryptoJS

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • CryptoJS

    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, 11:41 AM.
    Mirth Connect v3.2.2
    Java 8
    SQL Server 2005

  • #2
    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.

    Comment


    • #3
      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?
      Mirth Connect v3.2.2
      Java 8
      SQL Server 2005

      Comment


      • #4
        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.

        Comment


        • #5
          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?
          Mirth Connect v3.2.2
          Java 8
          SQL Server 2005

          Comment


          • #6
            From the same exact page:

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

            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

            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Ê�Æí�6ä§söܯ¥+/�
            
                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.

            Comment


            • #7
              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.
              Mirth Connect v3.2.2
              Java 8
              SQL Server 2005

              Comment


              • #8
                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.

                Comment


                • #9
                  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, 11:40 AM.
                  Mirth Connect v3.2.2
                  Java 8
                  SQL Server 2005

                  Comment


                  • #10
                    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.
                    Mirth Connect v3.2.2
                    Java 8
                    SQL Server 2005

                    Comment


                    • #11
                      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 but it's not working can you please explain me how to do it.
                      it's giving error : TypeError: Cannot call method "decrypt" of undefined
                      Last edited by kpatel; 10-14-2014, 06:17 AM.

                      Comment


                      • #12
                        Copy the CryptoJS source code, and paste it into a new code template. The type would be function, and I use global as my context.
                        Mirth Connect v3.2.2
                        Java 8
                        SQL Server 2005

                        Comment


                        • #13
                          Originally posted by kpatel View Post
                          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 but it's not working can you please explain me how to do it.
                          it's giving error : TypeError: Cannot call method "decrypt" of undefined
                          Can you show how you're trying to use it in your code and where (in preprocessor, transformer, etc.)?
                          Mirth Connect v3.2.2
                          Java 8
                          SQL Server 2005

                          Comment


                          • #14
                            i did same but it's not working.

                            for Example :

                            function function_name() {

                            AES SCRIPT PEST HERE

                            }

                            Comment


                            • #15
                              Originally posted by kpatel View Post
                              i did same but it's not working.

                              for Example :

                              function function_name() {

                              AES SCRIPT PEST HERE

                              }
                              You don't need the function declaration. The contents will only be the source of the AES.js file.
                              Mirth Connect v3.2.2
                              Java 8
                              SQL Server 2005

                              Comment

                              Working...
                              X