Sign in to follow this  
Followers 0
ChrisH3

AutoIT Encrypt -> .Net Decrypt, and .Net Encrypt -> AutoIT Decrypt

2 posts in this topic

I had a need for some fairly straighforward encryption between 2 applications. The wrinkle was that one end was done with AutoIT and the other end was coded in C# and .Net. After searching I found no code samples that directly applied, so after spending the usual hours to understand (being new to encryption) the problem I am posting my solution in the hope that it saves someone else a few hours. The code and ideas mainly come from the following sources

I just put the pieces together.

The AutoIT code is trivial see below

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#include <Crypt.au3>
EncData("This is Plain Text")
DecData("0x072EAFADE21090803E31F136EB14D2650D9F7B2620F4984F")
Func EncData($plainText)
Local $EncryptedData
Local $hKey = _Crypt_DeriveKey("MyKey", $CALG_RC2) ; Declare a password string and algorithm to create a cryptographic key.
$EncryptedData = _Crypt_EncryptData($plainText, $hKey, $CALG_USERKEY)
_Crypt_DestroyKey($hKey) ; Destroy the cryptographic key.
;ClipPut($EncryptedData)
MsgBox(0, "", $EncryptedData) ;Results in 0x072EAFADE21090803E31F136EB14D2650D9F7B2620F4984F
EndFunc   ;==>EncData
Func DecData($EncryptData)
Local $stringDecrypt
$stringDecrypt = BinaryToString(_Crypt_DecryptData($EncryptData, "MyKey", $CALG_RC2))
MsgBox(0, "", $stringDecrypt) ;Results in "This is Plain Text"
EndFunc   ;==>DecData

The corresponding C# Class that generates exactly the same results is as follows

using System;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using System.IO;
namespace MyNameSpace
{
    class Crypt
    {
        internal void testEnc()
        {
            byte[] plaintext = Encoding.UTF8.GetBytes("This is Plain Text");
            byte[] password = Encoding.UTF8.GetBytes("MyKey");
            PasswordDeriveBytes cdk = new PasswordDeriveBytes(password, null);
            byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
            byte[] key = cdk.CryptDeriveKey("RC2", "MD5", 0, iv);
            RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
            rc2.Key = key;
            rc2.IV = iv;    //IV MUST be specified with Zeroes, or it will be defaulted to a random value
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, rc2.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(plaintext, 0, plaintext.Length);
            cs.Close();
            string str = BitConverter.ToString(ms.ToArray());
            str = str.Replace("-", "");  //formats output of BitConvertor to AutoIT binary formatted as string
            System.Windows.Forms.MessageBox.Show("0x" + str);
        }
        internal void testDec()
        {
            string encData = "072EAFADE21090803E31F136EB14D2650D9F7B2620F4984F";
            byte[] password = Encoding.UTF8.GetBytes("MyKey");
            PasswordDeriveBytes cdk = new PasswordDeriveBytes(password, null);
            byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
            byte[] key = cdk.CryptDeriveKey("RC2", "MD5", 0, iv);
            RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
            rc2.Key = key;
            rc2.IV = iv;
            //Use Linq to convert the string to a byte array
            byte[] encrypted = Enumerable.Range(0, encData.Length)
                .Where(x => x % 2 == 0)
                .Select(x => Convert.ToByte(encData.Substring(x, 2), 16))
                .ToArray();
            MemoryStream ms = new MemoryStream(encrypted);
            string plaintext = null;
            CryptoStream cs = new CryptoStream(ms, rc2.CreateDecryptor(), CryptoStreamMode.Read);
            StreamReader srDecrypt = new StreamReader(cs);
            plaintext = srDecrypt.ReadToEnd();
            System.Windows.Forms.MessageBox.Show(plaintext);
        }
    }
}

My application only requires .Net encryption and AutoIT decryption. I did both ways to make sure everything worked.

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0