Jump to content

Simple de/encryption with Autoit


Recommended Posts

s

#include <Crypt.au3>
;_Crypt_Startup() ; To optimize performance start the crypt library.


;EG1------------------------------------------------------------------------------------
Global $sData2Encrypt = 'ĐÀO VĂN TRONG', $iPasswd_1 = 'Việt Nam', $iAlgID_1 = 'RC4', $iPasswd_2 = 'Muôn Năm', $iAlgID_2 = '3DES'
ConsoleWrite('+ EG1 - Data input: ' & $sData2Encrypt & @CRLF)
Global $bDataEncrypted = _Encrypt_Data($sData2Encrypt)
ConsoleWrite('+ EG1 - Data Encrypted (E:' & @error & '): ' & $bDataEncrypted & @CRLF)
Global $sDataDecrypted = _Decrypt_Data($bDataEncrypted)
ConsoleWrite('+ EG1 - Data Decrypted (E:' & @error & '): ' & $sDataDecrypted & @CRLF & @CRLF)

;EG2------------------------------------------------------------------------------------
Global $sData2Encrypt = 'ĐÀO VĂN TRONG love AUTOIT', $iPasswd_1 = 'Việt Nam', $iAlgID_1 = 'RC4', $iPasswd_2 = 'Muôn Năm', $iAlgID_2 = '3DES'
ConsoleWrite('+ EG2 - Data input: ' & $sData2Encrypt & @CRLF)
ConsoleWrite('- EG2 - Passwd 1: ' & $iPasswd_1 & ' | Algorithm 1: ' & $iAlgID_1 & @CRLF)
ConsoleWrite('- EG2 - Passwd 2: ' & $iPasswd_2 & ' | Algorithm 2: ' & $iAlgID_2 & @CRLF)
Global $bDataEncrypted = _Encrypt_Data($sData2Encrypt, $iPasswd_1, $iAlgID_1, $iPasswd_2, $iAlgID_2)
ConsoleWrite('- EG2 - Data Encrypted (E:' & @error & '): ' & $bDataEncrypted & @CRLF)
Global $sDataDecrypted = _Decrypt_Data($bDataEncrypted, $iPasswd_1, $iAlgID_1, $iPasswd_2, $iAlgID_2)
ConsoleWrite('- EG2 - Data Decrypted (E:' & @error & '): ' & $sDataDecrypted & @CRLF)




Func _Encrypt_Data($sData2Encrypt, $iPasswd_1 = 'TRONG.LIVE', $iAlgID_1 = '3DES', $iPasswd_2 = 'TRONG.LIVE', $iAlgID_2 = 'RC4')
    Local $bDataEncrypted = StringToBinary($sData2Encrypt, 4)
    Local $ePasswd_1 = StringToBinary($iPasswd_1, 4), $ePasswd_2 = StringToBinary($iPasswd_2, 4)
    Local $rDataEncrypted = _Crypt_EncryptData($bDataEncrypted, $ePasswd_1, _GetAlgorithm($iAlgID_1))
    If @error Then Return SetError(1, 0, "")
    $bDataEncrypted = _Crypt_EncryptData($rDataEncrypted, $ePasswd_2, _GetAlgorithm($iAlgID_2))
    If @error Then Return SetError(2, 0, "")
    $rDataEncrypted = String($bDataEncrypted)
    $bDataEncrypted = StringLeft($rDataEncrypted, 2) == '0x' ? StringTrimLeft($rDataEncrypted, 2) : $rDataEncrypted
    Return $bDataEncrypted
EndFunc   ;==>_Encrypt_Data

Func _Decrypt_Data($bDataEncrypted, $iPasswd_1 = 'TRONG.LIVE', $iAlgID_1 = '3DES', $iPasswd_2 = 'TRONG.LIVE', $iAlgID_2 = 'RC4')
    Local $rDataEncrypted = Binary(StringLeft($bDataEncrypted, 2) == '0x' ? $bDataEncrypted : '0x' & $bDataEncrypted)
    Local $ePasswd_1 = StringToBinary($iPasswd_1, 4), $ePasswd_2 = StringToBinary($iPasswd_2, 4)
    $bDataEncrypted = _Crypt_DecryptData($rDataEncrypted, $ePasswd_2, _GetAlgorithm($iAlgID_2))
    If @error Then Return SetError(1, 0, "")
    $rDataEncrypted = _Crypt_DecryptData($bDataEncrypted, $ePasswd_1, _GetAlgorithm($iAlgID_1))
    If @error Then Return SetError(2, 0, "")
    $bDataEncrypted = BinaryToString($rDataEncrypted, 4)
    Return $bDataEncrypted
EndFunc   ;==>_Decrypt_Data

Func _GetAlgorithm($iAlgID = '')
    Local $sAlgID
    Switch $iAlgID
        Case 'AES-128', $CALG_AES_128
            $sAlgID = $CALG_AES_128
        Case 'AES-192', $CALG_AES_192
            $sAlgID = $CALG_AES_192
        Case 'AES-256', $CALG_AES_256
            $sAlgID = $CALG_AES_256
        Case 'DES', $CALG_DES
            $sAlgID = $CALG_DES
        Case '3DES', $CALG_3DES
            $sAlgID = $CALG_3DES
        Case 'RC4', $CALG_RC4
            $sAlgID = $CALG_RC4
        Case Else
            $sAlgID = $CALG_AES_256
    EndSwitch
    Return $sAlgID
EndFunc   ;==>_GetAlgorithm

 

Regards,
 

Link to post
Share on other sites

Version 2 with compress:

#include <Crypt.au3>
;_Crypt_Startup() ; To optimize performance start the crypt library.

;#-EG-1----------------------------------------------------------------------------------------------
Global $sData2Encrypt = 'ÐÀO VĂN TRONG', $iPasswd_1 = 'Việt Nam', $iAlgID_1 = 'AES-256', $iPasswd_2 = 'Muôn Năm', $iAlgID_2 = 'AES-128'
ConsoleWrite('+ EG1 - Data input: ' & $sData2Encrypt & @CRLF)
Global $bDataEncrypted = _Data_Encrypt($sData2Encrypt)
ConsoleWrite('+ EG1 - Data Encrypted (E:' & @error & '): ' & $bDataEncrypted & @CRLF)
Global $sDataDecrypted = _Data_Decrypt($bDataEncrypted)
ConsoleWrite('+ EG1 - Data Decrypted (E:' & @error & '): ' & $sDataDecrypted & @CRLF & @CRLF)

;#-EG-2----------------------------------------------------------------------------------------------
Global $sData2Encrypt = 'ÐÀO VĂN TRONG', $iPasswd_1 = 'Việt Nam', $iAlgID_1 = '3DS', $iPasswd_2 = 'Muôn Năm', $iAlgID_2 = 'RC4'
ConsoleWrite('+ EG2 - Data input: ' & $sData2Encrypt & @CRLF)
ConsoleWrite('- EG2 - Passwd 1: ' & $iPasswd_1 & ' | Algorithm 1: ' & $iAlgID_1 & @CRLF)
ConsoleWrite('- EG2 - Passwd 2: ' & $iPasswd_2 & ' | Algorithm 2: ' & $iAlgID_2 & @CRLF)
Global $bDataEncrypted = _Data_Encrypt($sData2Encrypt, $iPasswd_1, $iAlgID_1, $iPasswd_2, $iAlgID_2)
ConsoleWrite('- EG2 - Data Encrypted (E:' & @error & '): ' & $bDataEncrypted & @CRLF)
Global $sDataDecrypted = _Data_Decrypt($bDataEncrypted, $iPasswd_1, $iAlgID_1, $iPasswd_2, $iAlgID_2)
ConsoleWrite('- EG2 - Data Decrypted (E:' & @error & '): ' & $sDataDecrypted & @CRLF)

Func _Data_Encrypt($sData2Encrypt, $iPasswd_1 = 'TRONG.LIVE', $iAlgID_1 = 'AES-256', $iPasswd_2 = 'TRONG.LIVE', $iAlgID_2 = 'AES-128')
    Local $rDataEncrypted = StringToBinary($sData2Encrypt, 4)
    Local $bDataEncrypted = _Data_Compress($rDataEncrypted, 258)
    If @error Then Return SetError(1, 0, "")
    Local $ePasswd_1 = StringToBinary($iPasswd_1, 4), $ePasswd_2 = StringToBinary($iPasswd_2, 4)
    $rDataEncrypted = _Crypt_EncryptData($bDataEncrypted, $ePasswd_1, _Data_GetAlgorithm($iAlgID_1))
    If @error Then Return SetError(2, 0, "")
    $bDataEncrypted = _Crypt_EncryptData($rDataEncrypted, $ePasswd_2, _Data_GetAlgorithm($iAlgID_2))
    If @error Then Return SetError(3, 0, "")
    $rDataEncrypted = String($bDataEncrypted)
    $bDataEncrypted = StringLeft($rDataEncrypted, 2) == '0x' ? StringTrimLeft($rDataEncrypted, 2) : $rDataEncrypted
    Return $bDataEncrypted
EndFunc   ;==>_Data_Encrypt by Dao Van Trong - TRONG.LIVE
Func _Data_Decrypt($bDataEncrypted, $iPasswd_1 = 'TRONG.LIVE', $iAlgID_1 = 'AES-256', $iPasswd_2 = 'TRONG.LIVE', $iAlgID_2 = 'AES-128')
    Local $rDataEncrypted = StringLeft($bDataEncrypted, 2) == '0x' ? $bDataEncrypted : '0x' & $bDataEncrypted
    Local $ePasswd_1 = StringToBinary($iPasswd_1, 4), $ePasswd_2 = StringToBinary($iPasswd_2, 4)
    $bDataEncrypted = _Crypt_DecryptData($rDataEncrypted, $ePasswd_2, _Data_GetAlgorithm($iAlgID_2))
    If @error Then Return SetError(3, 0, "")
    $rDataEncrypted = _Crypt_DecryptData($bDataEncrypted, $ePasswd_1, _Data_GetAlgorithm($iAlgID_1))
    If @error Then Return SetError(2, 0, "")
    $bDataEncrypted = _Data_Decompress($rDataEncrypted)
    If @error Then Return SetError(1, 0, "")
    $rDataEncrypted = BinaryToString($bDataEncrypted, 4)
    Return $rDataEncrypted
EndFunc   ;==>_Data_Decrypt by Dao Van Trong - TRONG.LIVE
Func _Data_Decompress($bBinary)
    $bBinary = Binary($bBinary)
    Local $tInput = DllStructCreate("byte[" & BinaryLen($bBinary) & "]")
    DllStructSetData($tInput, 1, $bBinary)
    Local $tBuffer = DllStructCreate("byte[" & 16 * DllStructGetSize($tInput) & "]")
    Local $a_Call = DllCall("ntdll.dll", "int", "RtlDecompressBuffer", "ushort", 2, "ptr", DllStructGetPtr($tBuffer), "dword", DllStructGetSize($tBuffer), "ptr", DllStructGetPtr($tInput), "dword", DllStructGetSize($tInput), "dword*", 0)
    If @error Or $a_Call[0] Then
        Return SetError(1, 0, "")
    EndIf
    Local $tOutput = DllStructCreate("byte[" & $a_Call[6] & "]", DllStructGetPtr($tBuffer))
    Return SetError(0, 0, DllStructGetData($tOutput, 1))
EndFunc   ;==>_Data_Decompress by trancexx
Func _Data_Compress($vInput, $iCompressionFormatAndEngine = 258)
    If Not ($iCompressionFormatAndEngine = 258) Then
        $iCompressionFormatAndEngine = 2
    EndIf
    Local $bBinary = Binary($vInput)
    Local $tInput = DllStructCreate("byte[" & BinaryLen($bBinary) & "]")
    DllStructSetData($tInput, 1, $bBinary)
    Local $a_Call = DllCall("ntdll.dll", "int", "RtlGetCompressionWorkSpaceSize", "ushort", $iCompressionFormatAndEngine, "dword*", 0, "dword*", 0)
    If @error Or $a_Call[0] Then
        Return SetError(1, 0, "")
    EndIf
    Local $tWorkSpace = DllStructCreate("byte[" & $a_Call[2] & "]")
    Local $tBuffer = DllStructCreate("byte[" & 16 * DllStructGetSize($tInput) & "]")
    Local $a_Call = DllCall("ntdll.dll", "int", "RtlCompressBuffer", "ushort", $iCompressionFormatAndEngine, "ptr", DllStructGetPtr($tInput), "dword", DllStructGetSize($tInput), "ptr", DllStructGetPtr($tBuffer), "dword", DllStructGetSize($tBuffer), "dword", 4096, "dword*", 0, "ptr", DllStructGetPtr($tWorkSpace))
    If @error Or $a_Call[0] Then
        Return SetError(2, 0, "")
    EndIf
    Local $tOutput = DllStructCreate("byte[" & $a_Call[7] & "]", DllStructGetPtr($tBuffer))
    Return SetError(0, 0, DllStructGetData($tOutput, 1))
EndFunc   ;==>_Data_Compress  by trancexx
Func _Data_GetAlgorithm($iAlgID = '')
    Local $sAlgID
    Switch $iAlgID
        Case 'AES-128', $CALG_AES_128
            $sAlgID = $CALG_AES_128
        Case 'AES-192', $CALG_AES_192
            $sAlgID = $CALG_AES_192
        Case 'AES-256', $CALG_AES_256
            $sAlgID = $CALG_AES_256
        Case 'DES', $CALG_DES
            $sAlgID = $CALG_DES
        Case '3DES', $CALG_3DES
            $sAlgID = $CALG_3DES
        Case 'RC4', $CALG_RC4
            $sAlgID = $CALG_RC4
        Case Else
            $sAlgID = $CALG_AES_256
    EndSwitch
    Return $sAlgID
EndFunc   ;==>_Data_GetAlgorithm

 

Regards,
 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...