timmy2 Posted July 22, 2015 Share Posted July 22, 2015 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. I assume the problem is either in the BinaryToString/StringToBinary conversion or in the file writing and reading.expandcollapse popup#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 More sharing options...
Valuater Posted July 22, 2015 Share Posted July 22, 2015 (edited) I think the problem is hereRemove 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 July 22, 2015 by Valuater Link to comment Share on other sites More sharing options...
timmy2 Posted July 23, 2015 Author Share Posted July 23, 2015 I think the problem is hereRemove 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 More sharing options...
timmy2 Posted July 23, 2015 Author Share Posted July 23, 2015 (edited) 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++:... whereas IniWrite creates this: 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!expandcollapse popup#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 July 23, 2015 by timmy2 add link Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now