Jump to content

Problem with _Crypt_EncryptData


 Share

Recommended Posts

I'm not sure if this is the right place to post this.  But I found a bug in: _Crypt_EncryptData

I noticed that in an older version of Autoit the _Crypt_EncryptData and _Crypt_DecryptData functions were working just fine.  But to my surprise in the latest autoit public version 3.3.14.2 my script was failing.

I figured out what the issue is.  If there is @CRLF at the top of your data when you perform the encryption process... it causes the encrypted data to come out incorrectly.

 

Non Working Script:

#include <Crypt.au3>
Global $Decrypted = @ScriptDir & '\file_Decrypted.txt', $Encrypted = @ScriptDir & '\file_Encrypted.txt'

$ReadDec = FileRead($Decrypted)

$DoIt = _Crypt_EncryptData($ReadDec, "012345678901234", $CALG_AES_256)
If $DoIt = True Then
    $ReadEnc = FileRead($Encrypted)
    $Read = BinaryToString(_Crypt_DecryptData($ReadEnc, "012345678901234", $CALG_AES_256))
    If StringInStr($Read, "[FOLDERS]") Then
        MsgBox(0,'Encryption', "Encryption Successful!"&@CRLF&@CRLF&$Encrypted)
    Else
        MsgBox(4,'Encryption', "Encryption Failed!")
    EndIf
EndIf

 

Working Script:

#include <Crypt.au3>
Global $Decrypted = @ScriptDir & '\file_Decrypted.txt', $Encrypted = @ScriptDir & '\file_Encrypted.txt'

$ReadDec = FileRead($Decrypted)
If StringLeft($ReadDec, 2) = @CRLF Then $ReadDec = StringTrimLeft($ReadDec, 2) ;;Removes @CRLF from the top of the data, if it Exists.
$DoIt = _Crypt_EncryptData($ReadDec, "012345678901234", $CALG_AES_256)
If $DoIt = True Then
    $ReadEnc = FileRead($Encrypted)
    $Read = BinaryToString(_Crypt_DecryptData($ReadEnc, "012345678901234", $CALG_AES_256))
    If StringInStr($Read, "[FOLDERS]") Then
        MsgBox(0,'Encryption', "Encryption Successful!"&@CRLF&@CRLF&$Encrypted)
    Else
        MsgBox(4,'Encryption', "Encryption Failed!")
    EndIf
EndIf

 

This is a workaround I have made for now just in case anyone has had this problem.

Edited by Melba23
Amended title (no bug)
Link to comment
Share on other sites

There is no bug, there's just a problem with the logic of your script.

_Crypt_EncryptData does not return true or false, just a binary string of the encrypted data (if successful)

This will encrypt and decrypt the data without any issues, even with a leading @CRLF

#include <Crypt.au3>
Global $sFileDecrypted = @ScriptDir & '\file_Decrypted.txt', $sFileEncrypted = @ScriptDir & '\file_Encrypted.txt'

If (Not FileExists($sFileDecrypted)) Then FileWrite($sFileDecrypted, @CRLF & "Some data")

Global $sString = FileRead($sFileDecrypted)

Global $xEncrypted = _Crypt_EncryptData($sString, "012345678901234", $CALG_AES_256)

ConsoleWrite("Encrypted: '" & $xEncrypted & "'" & @LF)

FileWrite($sFileEncrypted, $xEncrypted)

Global $xEncryptedRead = FileRead($sFileEncrypted)

Global $xDecrypted = BinaryToString(_Crypt_DecryptData($xEncryptedRead, "012345678901234", $CALG_AES_256))

ConsoleWrite("Decrypted: '" & $xDecrypted & "'" & @LF)

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...