Jump to content

String encryption problem


Recommended Posts

I am trying to encrypt a comma delimited string. When I try to decrypt it, it isn't giving the correct string. Its something like "yyyy". Does anyone know a solution.

#include <Crypt.au3>
$string = ',,hello,world,,'
MsgBox(0,'', _Crypt_EncryptData($string, "123", $CALG_AES_256))
MsgBox(0,'', BinaryToString(_Crypt_DecryptData($string, "123",$CALG_AES_256)))

UPDATE:

I figured out why I was getting "yyyy". It was because it was trying to BinaryToString(-1) which is returned by _Crypt_DecryptData(). I am still not sure why it is unable to decrypt the string correctly.

Edited by grasshopper3
Link to comment
Share on other sites

#include <Crypt.au3> 
$string = ',,hello,world,,' 
$crypt = _Crypt_EncryptData($string, "123", $CALG_AES_256) 
MsgBox(0,'', $crypt) 
MsgBox(0,'', BinaryToString(_Crypt_DecryptData($crypt,"123",$CALG_AES_256)))

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

UPDATE:

I figured out why I was getting "yyyy". It was because it was trying to BinaryToString(-1) which is returned by _Crypt_DecryptData(). I am still not sure why it is unable to decrypt the string correctly.

Look closer at the example from JohnOne. Your problem is that you didn't save the encrypted value anywhere. The function _Crypt_EncryptData($string, "123", $CALG_AES_256) returns the encrypted data, it does not modify $string. So the problem is that you are trying to decrypt the unencrypted data with _Crypt_DecryptData($string, "123",$CALG_AES_256).

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

ever since I implemented this into my script it has been causing trouble. I will encrypt a string and it is written into the .log like... ¾£{Њ…Eqo#“BÀ<kªå,jlË•?0(kåæ6. This causes an error when trying to decrypt.

I have put msgboxes on the filewriteline() to preview what it is writing and it doesn't look like the above, it looks correct 38A9CC7FE2E499EDDE3DFFC24A.

Does any one have an idea of what is going on?

Link to comment
Share on other sites

The binary is being automatically converted to an ASCII string of hex code for your viewing. If you want to save the values, it must be in a binary file, i.e. opened with:

#include <Crypt.au3>

Global $sString, $bCrypted, $bDecrypted, $sDecrypted
Global $sFile = @ScriptDir & "\Test1.bin", $hFile, $bData

$sString = ',,hello,world,,'
ConsoleWrite("Original $sString = " & $sString & " ; Type = " & VarGetType($sString) & @LF)

$bCrypted = _Crypt_EncryptData($sString, "123", $CALG_AES_256)
ConsoleWrite("Encrypted $bCrypted = " & $bCrypted & " ; Type = " & VarGetType($bCrypted) & @LF)

$hFile = FileOpen($sFile, 16 + 2) ; Binary + overwrite
FileWrite($hFile, $bCrypted)
FileClose($hFile)

$hFile = FileOpen($sFile, 16) ; Binary read
$bData = FileRead($hFile)
FileClose($hFile)
ConsoleWrite("File data $bData = " & $bData & " ; Type = " & VarGetType($bData) & @LF)

$bDecrypted = _Crypt_DecryptData($bData, "123", $CALG_AES_256)
ConsoleWrite("Decrypted $bDecrypted = " & $bDecrypted & " ; Type = " & VarGetType($bDecrypted) & @LF)
$sDecrypted = BinaryToString($bDecrypted)
ConsoleWrite("String $sDecrypted = " & $sDecrypted & " ; Type = " & VarGetType($sDecrypted) & @LF)

If you want to mix it in with other textual data, then you'll have to do your own conversion to "0xFEDCBA9876543210" format.

:idea:

Edit: Forgot write mode for FileOpen()

Edit2: Expanded demo script

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

The binary is being automatically converted to an ASCII string of hex code for your viewing. If you want to save the values, it must be in a binary file, i.e. opened with:

$hBinFile = FileOpen($sFileName, 16) ; 16 = binary
FileWrite($hFile, $binCrptData)
FileClose($hFile)

If you want to mix it in with other textual data, then you'll have to do your own conversion to "0xFEDCBA9876543210" format.

:idea:

I didn't say earlier, but this doesn't occur every time; only every other. Would this still be the solution if it works sometimes?

Update: I tried your solution and it got worst

$Data= _Crypt_EncryptData($Data, "123", $CALG_AES_256)
$File = FileOpen(@ScriptDir & "\settings.ini",18)
FileWriteLine($File,$Data)
FileClose($File)

Update: fixed it with

FileWriteLine($File,StringToBinary($SetStrings))
Edited by grasshopper3
Link to comment
Share on other sites

I didn't say earlier, but this doesn't occur every time; only every other. Would this still be the solution if it works sometimes?

Update: I tried your solution and it got worst

$Data= _Crypt_EncryptData($Data, "123", $CALG_AES_256)
$File = FileOpen(@ScriptDir & "\settings.ini",18)
FileWriteLine($File,$Data)
FileClose($File)

Update: fixed it with

FileWriteLine($File,StringToBinary($SetStrings))
Yeah, I expanded the example in my post to include that.

Note that _Crypt_EncryptData() is treating the input $Data as a binary too. To strictly type your variables correctly, you would do StringToBinary() on the input before encrypting also. AutoIt's built-in recasting hides that from you a little.

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 6 years later...

Hello everyone, I'm having problems, I updated my autoit to the version: 3.3.14.2 and my script did not want to work anymore, I'll post it so that you can help me.

 

#include <String.au3>

Local $numero_serial=""
Local $serial_guardado=""
Local $Senha=""


$Senha = IniRead("C:\ID.ini","activation","key","0")

$numero_serial = IniRead("C:\ID.ini","activation","number","0")

$serial_guardado = IniRead("C:\ID.ini","activation","activation","0")

$numero_serial = _StringEncrypt (1, $numero_serial, $Senha,1)


If StringCompare($numero_serial, $serial_guardado) <> 0 Then
     ShellExecute("C:\ID.exe")
     Exit
EndIf

 

 

 

Link to comment
Share on other sites

As per the helpfile

Script Breaking Changes in Recent Versions
v3.3.10.0
AutoIt:

UDFs:

_StringEncrypt() documentation has been removed. The function is still present in String.au3, but may be removed at a later date. Scripts should be updated to use the Crypt functions instead.
Look at _Crypt_EncryptData() for an example of an alternative _StringEncrypt.

 

Link to comment
Share on other sites

@Subz

Before it was a mistake, now it does not work, what can it be?
I have already modified several things and nothing.

#include <String.au3>
#include <Crypt.au3>

Local $numero_serial=""
Local $serial_guardado=""
Local $Senha=""


$Senha = IniRead("C:\ID.ini","activation","key","0")

$numero_serial = IniRead("C:\ID.ini","activation","number","0")

$serial_guardado = IniRead("C:\ID.ini","activation","activation","0")

$numero_serial =  _Crypt_EncryptData (1, $numero_serial, $Senha,1)


If StringCompare($numero_serial, $serial_guardado) <> 0 Then
     ShellExecute("C:\ID.exe")
     Exit
EndIf

 

Edited by JSBGAMES
Link to comment
Share on other sites

I managed to solve !!!!, the only detail would be to make that the encrypted number example 0x4af07c668ec0 was generated without the 0x only 4af07c668ec0 what would have to modify to work the way I want?

#include <Crypt.au3>
#include <String.au3>
#include <MsgBoxConstants.au3>

Local $numero_serial =""
Local $serial_guardado =""
Local $Senha =""

$Senha = IniRead("C:\ID.ini","activation","key","0")

$numero_serial = IniRead("C:\ID.ini","activation","number","0")

$serial_guardado = IniRead("C:\ID.ini","activation","activation","0")

$numero_serial = _Crypt_EncryptData ($numero_serial, $Senha,$CALG_rc4)

MsgBox(0,'', $numero_serial)
MsgBox(0,'', BinaryToString(_Crypt_DecryptData($numero_serial,$Senha,$CALG_rc4)))

If StringCompare($NUMERO_SERIAL, $SERIAL_GUARDADO) <> 0 Then
  SplashTextOn("", "errado", 160, 50, -1, -1, 1, "", 18)
  Sleep(2000)
Else
  SplashTextOn("", "correto", 160, 50, -1, -1, 1, "", 18)
  Sleep(2000)

EndIf

 

Edited by JSBGAMES
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...