Jump to content

Problem with Encryption/Decryption


Recommended Posts

Hello Pals,

This is my code:

#include <Crypt.au3>

Global $s_IniFile = "test.ini"
;~ $s_RemoteUserPasswordHash = IniRead($s_IniFile, "General", "RemoteUserPasswordHash", "")
;~ Local $b_Encrypted = Binary("$s_RemoteUserPasswordHash")
Local $b_Encrypted = Binary("0x4AFFF9D9ECC556D90AF00046")

If $b_Encrypted <> "" Then
    _Crypt_Startup()
    $h_Key = _Crypt_DeriveKey("Arschloch-Karte17", $CALG_RC4)
    _Crypt_DestroyKey($h_Key)
    _Crypt_Shutdown()
    $s_RemotePassword = BinaryToString(_Crypt_DecryptData($b_Encrypted, $h_Key, $CALG_RC4))
Else
    ConsoleWrite(@CRLF & "No hash found in the INI-File!")
EndIf
MsgBox("", "Password", $s_RemotePassword)

If only (!!!) this line:

;~ $s_RemoteUserPasswordHash = IniRead($s_IniFile, "General", "RemoteUserPasswordHash", "")

...is uncommented (nothing else!!!), the hash is not correctly decrypted to the correct value of "ValikIsAGirl", no matter if the inifile/key/value exist - and even although $s_RemoteUserPasswordHash isn't used further.

Why?!?!?!?

Me = totally clueless. Is this a Friday's problem trying to tell me to call it a day and go for the weekend?

Regards,

Chris

Link to comment
Share on other sites

Link to comment
Share on other sites

Treading on something can only be described as glass injected with AIDs. With some hot coals, used needles and feathers thrown in.

Is Valik eventually not a girl? I don't know him/her personally, and "Valik" is neither male nor female. He/She always has Avatar pictures with girls...so? The passphrase was used completely unintentional and without any spirit of mischief. I didn't mean to tread on him/her. Now that that's cleared, do you have some advice regarding the problem?

Edited by cherdeg
Link to comment
Share on other sites

Here's a shot in the dark.

You have these two lines:

_Crypt_DestroyKey($h_Key)
_Crypt_Shutdown()

Before the

[url="../autoit3/docs/functions/BinaryToString.htm"]BinaryToString[/url](_Crypt_DecryptData($b_Encrypted, $h_Key, $CALG_RC4))

Which means that the key is no longer valid. Change the order and see if it solves the problem.

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

I will admit quickly, I know nothing of the functions you're using from the Crypt library as I haven't used any of them, but it would seem you're destroying the key too soon, and you're also shutting down the Crypt library before calling your last function. Maybe try the following?

#include <Crypt.au3>

Global $s_IniFile = "test.ini"
;~ $s_RemoteUserPasswordHash = IniRead($s_IniFile, "General", "RemoteUserPasswordHash", "")
;~ Local $b_Encrypted = Binary("$s_RemoteUserPasswordHash")
Local $b_Encrypted = Binary("0x4AFFF9D9ECC556D90AF00046")

If $b_Encrypted <> "" Then
    _Crypt_Startup()
    $h_Key = _Crypt_DeriveKey("Arschloch-Karte17", $CALG_RC4)
    $s_RemotePassword = BinaryToString(_Crypt_DecryptData($b_Encrypted, $h_Key, $CALG_RC4))
    ; The below lines seemed to come to early because you were trying to use _Crypt_DecryptData() after
    ; shutting down the library. So this was a simple change. I hope it works out for you.
    _Crypt_DestroyKey($h_Key)
    _Crypt_Shutdown()
Else
    ConsoleWrite(@CRLF & "No hash found in the INI-File!")
EndIf
MsgBox("", "Password", $s_RemotePassword)

@monoceres

Good job, you beat me to it!

Thanks a ton,

Jarvis

Edited by JSThePatriot

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

You do not have to use use CryptDeriveKey, just use the key-string directly in Decrypt, startup and shutdown is not required, too. Everything is done by _Crypt_DecryptData.

#include <Crypt.au3>

$sString = "a Teststring"

$b_Encrypted = _Crypt_EncryptData(StringToBinary($sString), "Arschloch-Karte17", $CALG_RC4)
MsgBox(0, '', $b_Encrypted)

$s_RemotePassword = BinaryToString(_Crypt_DecryptData($b_Encrypted, "Arschloch-Karte17", $CALG_RC4))
MsgBox(0, '', $s_RemotePassword)
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Hey Guys,

thank you all - everything you say may be correct. And indeed, _Crypt_DeriveKey, start and stop of encryption isn't needed. Now it works...but it's still a riddle for me that it didn't before...

Encryption:

#include <Crypt.au3>

Local $s_IniFile = "test.ini"

$s_Password = InputBox("Password Encrypter", "Please enter the password to encrypt: ")
$s_Hash = _Crypt_EncryptData($s_Password, "Arschloch-Karte17", $CALG_RC4)
ClipPut($s_Hash)

IniWrite($s_IniFile, "General", "RemoteUserPasswordHash", $s_Hash)
MsgBox("", "Password Encrypter",    "Resulting hash: " & $s_Hash & @CRLF & @CRLF & _
                                    "The INI-File was updated with the generated hash." & @CRLF & _
                                    "Also the hash was copied to the clipboard.")

Decryption:

#include <Crypt.au3>

Local $s_RemoteUserPasswordHash = IniRead("test.ini", "General", "RemoteUserPasswordHash", "")
Local $b_Encrypted = Binary($s_RemoteUserPasswordHash)

If $b_Encrypted <> "" Then
    $s_RemotePassword = BinaryToString(_Crypt_DecryptData($b_Encrypted, "Arschloch-Karte17", $CALG_RC4))
Else
    ConsoleWrite(@CRLF & "No hash found in the INI-File!")
EndIf

ClipPut($s_RemotePassword)
MsgBox("", "Password Decrypter",    "Resulting password: " & $s_RemotePassword & @CRLF & @CRLF & _
                                    "The password was copied to the clipboard.")

Thank you all and...have a great weekend!!!

Regards,

Chris

Edited by cherdeg
Link to comment
Share on other sites

The last time I checked, I had a penis. Maybe I need to go check again? At any rate, I don't always use female avatars. The previous avatar was Fruit Fucker, which is definitely male (as far as robots go). I've previously used an image of Homer Simpson's brain (male), Homer Simpson in drag (confused male) and BloodRayne (female).

Edit: Also, the gender field is set on my account... so yeah.

Edited by Valik
Link to comment
Share on other sites

  • Developers

Then only left for me is desperate hope that you aren't pissed...

He doesn't drink so that will not happen .... or did you mean something else? :(

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Is Valik eventually not a girl? I don't know him/her personally, and "Valik" is neither male nor female.

It is pretty easy to determine that his name is Jason, which in my experience is usually male.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

It is pretty easy to determine that his name is Jason, which in my experience is usually male.

If you care, you care. If you don't, you don't. I was just randomly picking a phrase. Edited by cherdeg
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...