Jump to content

Unpredictable results with encrypted data saved to a file


Recommended Posts

The code I'm working on appears to properly encrypt and decrypt any username and password entered, but sometimes things go wrong somewhere between saving it to a file and reading the data back from that file.  The example code below lets you enter a "username" and "password" (at this point with no rules imposed). The entered username and password are encrypted and saved to a file. It can then decrypt and display the username and password from the original encrypted variables (i.e., file I/O is bypassed; this is just encrypt then decrypt).  The encrypted username and password are also fetched from the file, decrypted, and displayed. The latter is where things go awry. Lots of usernames and passwords play back fine from the file, but some do not, like the one shown below. Before I continue fleshing this out I gotta understand where I'm screwing up. 

credential_IO_problem.thumb.png.77880b19

 

I assume the problem is either in the BinaryToString/StringToBinary conversion or in the file writing and reading.

#Region - Declarations
; #INCLUDES# =========================================================================================================
#include <Debug.au3>
#include <Crypt.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>

OnAutoItExitRegister ( "_Terminate" )

; #GLOBAL VARIABLES# ========================================================================================================

Global Const $sFilename = @ScriptDir & "\credentials.txt"

#EndRegion - Declarations


#Region - Program
#cs -----------------------------------------------------
    Enter UN and PW, encrypt and write both to a file
#ce -----------------------------------------------------


_Crypt_Startup() ; Start the Crypt library.

While 1
    If FileExists($sFileName) Then FileDelete($sFileName)

    $sUsername = InputBox("","Enter username: ")
        If @error = 1 then Exit
    $sPassword = InputBox("","Enter password: ")
        If @error = 1 then Exit

    ; Encrypt text using a generic key
    $sUNEncrypted = _Crypt_EncryptData($sUsername, 'EncryptionKey', $CALG_RC4)
    $sPWEncrypted = _Crypt_EncryptData($sPassword, 'EncryptionKey', $CALG_RC4)

    ; open and write both credentials to file
    $hFileOpen = FileOpen ($sFilename, $FO_OVERWRITE)
    FileWriteLine ($hFileOpen,BinaryToString($sUNEncrypted))
    FileWriteLine ($hFileOpen,BinaryToString($sPWEncrypted))
    FileClose ($hFileOpen)


#cs -----------------------------------------------------
    Read file, decrypt and display
#ce -----------------------------------------------------

    $sUserResponse = MsgBox(BitOR($MB_TOPMOST,$MB_OKCANCEL),"","Press enter to decrypt the file contents or Cancel to exit.")
    Switch $sUserResponse
        Case $IDOK
            $vFileUN = StringToBinary(FileReadLine($sFilename,1))
            $vFilePW = StringToBinary(FileReadLine($sFilename,2))

            ; Decrypt the encrypted text FETCHED FROM THE FILE
            $sUNDecryptedF = BinaryToString(_Crypt_DecryptData($vFileUN, 'EncryptionKey', $CALG_RC4))
            $sPWDecryptedF = BinaryToString(_Crypt_DecryptData($vFilePW, 'EncryptionKey', $CALG_RC4))

            ; Decrypt the original encrypted variables
            $sUNDecrypted = BinaryToString(_Crypt_DecryptData($sUNEncrypted, 'EncryptionKey', $CALG_RC4))
            $sPWDecrypted = BinaryToString(_Crypt_DecryptData($sPWEncrypted, 'EncryptionKey', $CALG_RC4))

            ; Display the decrypted text pulled from the file
            MsgBox($MB_TOPMOST, "Fetched from File", "Original Username: " & $sUsername & @CRLF & _
                                        "    Decrypted result: " & $sUNDecryptedF & @CRLF & @CRLF & _
                                        "Original Password: " & $sPassword & @CRLF & _
                                        "   Decrypted result: " & $sPWDecryptedF)


            ; Display the decrypted text using original encrypted variables.
            MsgBox($MB_TOPMOST, "Direct from Variables", "Original Username: " & $sUsername & @CRLF & _
                                        "    Decrypted result: " & $sUNDecrypted & @CRLF & @CRLF & _
                                        "Original Password: " & $sPassword & @CRLF & _
                                        "   Decrypted result: " & $sPWDecrypted)
            FileDelete ($sFilename)

        Case $IDCANCEL
            Exit
    EndSwitch

WEnd

Exit

#EndRegion - Program


#Region - Functions

Func _Terminate()
    _Crypt_Shutdown() ; Shutdown the Crypt library.
    Exit 0
EndFunc   ;==>_Terminate

#EndRegion - Functions

 

Link to comment
Share on other sites

I think the problem is here

Remove the BinarytoString(), it is not needed here

  FileWriteLine ($hFileOpen,BinaryToString($sUNEncrypted))
    FileWriteLine ($hFileOpen,BinaryToString($sPWEncrypted))

 

or here right?

 

$vFileUN = StringToBinary(FileReadLine($sFilename,1))
            $vFilePW = StringToBinary(FileReadLine($sFilename,2))
Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

I think the problem is here

Remove the BinarytoString(), it is not needed here

  FileWriteLine ($hFileOpen,BinaryToString($sUNEncrypted))
    FileWriteLine ($hFileOpen,BinaryToString($sPWEncrypted))

 

or here right?

 

$vFileUN = StringToBinary(FileReadLine($sFilename,1))
            $vFilePW = StringToBinary(FileReadLine($sFilename,2))

Thank you for replying, Valuater, but those changes did not solve the problem.   I got the idea from various forum posts here that those conversions were needed in this application but their presence or absence appears to have no effect in this instance, except that their absence causes more usernames and/or passwords to be read back empty or otherwise messed up. I suspect the problem lies in saving or reading the file.

Link to comment
Share on other sites

Thank you to @Valuater and @Guinness for helping me from the past.  An invisible hand guided me to this Snippets post where after much study of your exquisite Escher-like code I was prompted to try IniWrite/IniRead instead of FileWriteLine, etc. I should have questioned why the file created by FileWriteLine looked like this in Notepad++:

filewrite_result.thumb.png.4e69177825c0f

... whereas IniWrite creates this:

IniWrite_result.thumb.png.dbe7b58c0592a8

 

I have yet to make the resulting code below fail, so now I can proceed with removing the debugging stuff and move on.  Thank you both!

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

Global Const $sFilename = @ScriptDir & "\credentials.dat"

#cs ----------------------------------------------------------------
    Enter UN and PW, encrypt and write both to a file using IniWrite
#ce ----------------------------------------------------------------

While 1
    If FileExists($sFilename) Then FileDelete($sFilename)

    $sUsername = InputBox("","Enter username: ")
        If @error = 1 then Exit
    $sPassword = InputBox("","Enter password: ")
        If @error = 1 then Exit

    ; Encrypt text using a generic key
    $sUNEncrypted = _Crypt_EncryptData($sUsername, 'EncryptionKey', $CALG_RC4)
    $sPWEncrypted = _Crypt_EncryptData($sPassword, 'EncryptionKey', $CALG_RC4)

    ; open and write both credentials to ini file
    $hFileWrite1 = IniWrite ($sFilename,"PasswordKey","Username",$sUNEncrypted)
    $hFileWrite2 = IniWrite ($sFilename,"PasswordKey","Password",$sPWEncrypted)

#cs -----------------------------------------------------
    Read file, decrypt and display
#ce -----------------------------------------------------

    $sUserResponse = MsgBox(BitOR($MB_TOPMOST,$MB_OKCANCEL),"","Press enter to decrypt the file contents or Cancel to exit.")
    Switch $sUserResponse
        Case $IDOK

            ; Decrypt the encrypted text FETCHED FROM THE FILE
            $sUNDecryptedF = BinaryToString(_Crypt_DecryptData(IniRead($sFilename, 'PasswordKey', 'Username', ''), 'EncryptionKey', $CALG_RC4))
            $sPWDecryptedF = BinaryToString(_Crypt_DecryptData(IniRead($sFilename, 'PasswordKey', 'Password', ''), 'EncryptionKey', $CALG_RC4))

            ; Decrypt the original encrypted variables
            $sUNDecrypted = BinaryToString(_Crypt_DecryptData($sUNEncrypted, 'EncryptionKey', $CALG_RC4))
            $sPWDecrypted = BinaryToString(_Crypt_DecryptData($sPWEncrypted, 'EncryptionKey', $CALG_RC4))

            ; Display the decrypted text pulled from the file
            MsgBox($MB_TOPMOST, "Fetched from File", "Original Username: " & $sUsername & @CRLF & _
                                        "    Decrypted result: " & $sUNDecryptedF & @CRLF & @CRLF & _
                                        "Original Password: " & $sPassword & @CRLF & _
                                        "   Decrypted result: " & $sPWDecryptedF)


            ; Display the decrypted text using original encrypted variables.
            MsgBox($MB_TOPMOST, "Direct from Variables", "Original Username: " & $sUsername & @CRLF & _
                                        "    Decrypted result: " & $sUNDecrypted & @CRLF & @CRLF & _
                                        "Original Password: " & $sPassword & @CRLF & _
                                        "   Decrypted result: " & $sPWDecrypted)
            ;FileDelete ($sFilename)

        Case $IDCANCEL
            Exit
    EndSwitch

WEnd

 

 

Edited by timmy2
add link
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

×
×
  • Create New...