autonomouslogic

Storing and reading encrypted data from a text file (not working)

16 posts in this topic

#1 ·  Posted (edited)

hello i have tried to store and read encrypted data of passwords and usernames to a text file my code is as follows for and storing to a text file is

FileOpen("data.txt", 1)
local $encryptedusername = _Crypt_EncryptData($usernames[ubound($usernames)-1], "P14h735536jk3fvvbg", $CALG_AES_128)
local $encryptedpassword = _Crypt_EncryptData($passwords[ubound($passwords)-1], "P14h735536jk3fvvbg", $CALG_AES_128)
FileWriteLine("data.txt", $encryptedusername)
FileWriteLine("data.txt", $encryptedpassword)
FileClose("data.txt")

 

and for reading it is

FileOpen("data.txt", 1)
FileClose("data.txt")
FileOpen("data.txt", 0)
for $i = 1 to _FileCountLines("data.txt") step 2
   _ArrayAdd($usernames, BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i), "P14h735536jk3fvvbg", $CALG_AES_128)))
   _ArrayAdd($passwords,BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i+1), "P14h735536jk3fvvbg", $CALG_AES_128)))
Next
for $i = 0 to UBound($usernames)-1 step 1
   ConsoleWrite($usernames[$i])
   ConsoleWrite($passwords[$i])
Next
FileClose("data.txt")

the problem is that the data comes back as yyyy (with dots over) and Im really stuck as to what to do about this? Thanks if anyone helps

btw I have searched forums for a similar problem but I havent been able to implement their solution into mine..

Edited by autonomouslogic
input error

Share this post


Link to post
Share on other sites



#include <Crypt.au3>
#include <File.au3>

local $usernames[1]
$usernames[0]="NewUserwith a very lenghty name"
local $passwords[1]
$passwords[0]="NewPassword which is even longer"

encrypt()
decrypt()

func encrypt()
    FileOpen("data.txt",  $FO_OVERWRITE + $FO_BINARY)
    local $encryptedusername = _Crypt_EncryptData($usernames[ubound($usernames)-1], "P14h735536jk3fvvbg", $CALG_AES_128)
    local $encryptedpassword = _Crypt_EncryptData($passwords[ubound($passwords)-1], "P14h735536jk3fvvbg", $CALG_AES_128)
    FileWriteLine("data.txt", $encryptedusername)
    FileWriteLine("data.txt", $encryptedpassword)
    FileClose("data.txt")
EndFunc

func decrypt()
    FileOpen("data.txt", $FO_READ + $FO_BINARY)
;~  FileClose("data.txt")
;~  FileOpen("data.txt", 0)
consolewrite(_FileCountLines("data.txt") & " lines in the file" & @CRLF)

    for $i = 1 to (_FileCountLines("data.txt")/2)-1
        _ArrayAdd($usernames, BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i), "P14h735536jk3fvvbg", $CALG_AES_128)))
        _ArrayAdd($passwords,BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i+1), "P14h735536jk3fvvbg", $CALG_AES_128)))
    Next

    for $i = 0 to UBound($usernames)-1 step 1
        ConsoleWrite($i & $usernames[$i] & @crlf)
        ConsoleWrite($i & $passwords[$i] & @crlf)
    Next
    FileClose("data.txt")
endfunc

You are incorrectly calculating the arraysize. Its not the same as your filecountlines

You are messing with binary and text (assuming no CRLF lines are in the binary)

for $i = 1 to (_FileCountLines("data.txt")/2)-1 will come closer

Share this post


Link to post
Share on other sites

@junkew thanks for the response.

What im trying to do is it update a combo box with the stored user names when the program is started. I have added your code into what I have done and I find it doesnt work although it hasnt been much it doesn't make sense to me why its not working? I've been trying to make it work for ages but I just cant see what Im doing wrong?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <Array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <File.au3>
#Include <Timers.au3>
#include <Crypt.au3>
#include <GuiEdit.au3>
$Form1 = GUICreate("Pie", 170, 235, 190, 200)
local $usernames[5]
local $passwords[5]
global $selectuser = GUICtrlCreateCombo($usernames, 21, 95, 130, 20)
$addusername = GUICtrlCreateButton("Add", 21, 170, 130, 30)
$usernamefield = GUICtrlCreateInput("", 21, 120, 130, 20)
$passwordfield = GUICtrlCreateInput("", 21, 145, 130, 20, 0x0020)

func encrypt()
    FileOpen("data.txt",  $FO_OVERWRITE + $FO_BINARY)
    local $encryptedusername = _Crypt_EncryptData($usernames[ubound($usernames)-1], "P14h735536jk3fvvbg", $CALG_AES_128)
    local $encryptedpassword = _Crypt_EncryptData($passwords[ubound($passwords)-1], "P14h735536jk3fvvbg", $CALG_AES_128)
    FileWriteLine("data.txt", $encryptedusername)
    FileWriteLine("data.txt", $encryptedpassword)
    FileClose("data.txt")
EndFunc
5
func decrypt()
    FileOpen("data.txt", $FO_READ + $FO_BINARY)
;~  FileClose("data.txt")
;~  FileOpen("data.txt", 0)
consolewrite(_FileCountLines("data.txt") & " lines in the file" & @CRLF)

    for $i = 1 to (_FileCountLines("data.txt")/2)-1
        local $rawuserdata = BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i)
        local $rawpassworddata = BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i+1)
        _ArrayAdd($usernames, $rawuserdata, "P14h735536jk3fvvbg", $CALG_AES_128)))
        _ArrayAdd($passwords, $rawpassworddata, "P14h735536jk3fvvbg", $CALG_AES_128)))
     Next
     ConsoleWrite($usernames[0])
     FileClose("data.txt")
    updatecomboandinfo()
endfunc

func updatecomboandinfo()
   if not GUICtrlRead($usernamefield) = "" Then
      _ArrayAdd($usernames, GUICtrlRead($usernamefield))
      for $i = 0 to UBound($usernames)-1 step 1
         GUICtrlSetData($selectuser, $usernames[$i])
      Next
      _ArrayAdd($passwords, GUICtrlRead($passwordfield))
   EndIf
EndFunc

decrypt()
GUISetState(@SW_SHOW)
While True
   $nMsg = GUIGetMsg()
   Switch $nMsg
      Case $GUI_EVENT_CLOSE
         Exit
      Case $addusername
         updatecomboandinfo()
         encrypt()
         GUICtrlSetData($usernamefield, "")
         GUICtrlSetData($passwordfield, "")
      EndSwitch
WEnd

 

Share this post


Link to post
Share on other sites

I've changed the decrypt function to this and it doesnt still work im really baffled.

func decrypt()
    FileOpen("data.txt", $FO_READ + $FO_BINARY)
;~  FileClose("data.txt")
;~  FileOpen("data.txt", 0)
consolewrite(_FileCountLines("data.txt") & " lines in the file" & @CRLF)
    for $i = 1 to (_FileCountLines("data.txt")/2)-1
        _ArrayAdd($usernames, BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i), "P14h735536jk3fvvbg", $CALG_AES_128)))
        _ArrayAdd($passwords,BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i+1), "P14h735536jk3fvvbg", $CALG_AES_128)))
    Next
     FileClose("data.txt")
    for $i = 0 to $i = ubound($usernames)-1 step 1
      GUICtrlSetData($selectuser, $usernames[$i] & @CRLF)
   next
endfunc

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You could always encrypt the whole file rather than individual lines.

When you write the lines to the text file they all come out in one line, so counting the lines won't work. You can't decrypt the data of individual lines, because the way you write them means there are no "lines" to decrypt.

By the way, you're using FileClose wrong, you need to use the file handle returned from the FileOpen to use FileClose, you can't use the file name.

Edited by KingBob

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

@kingbob would this mean that I would have to make a text file with the passwords and usernames and then encrypt and then delete the text file? But surely that would be very insecure is there a better way of doing it?

Share this post


Link to post
Share on other sites

Why not just read/write the encrypted data (actually: binary) in binary mode? This requires Fopen() with binary mode then FileRead/FileWrite using handle. There is no more the notion of "line" in a binary file, so don't try because it will fail.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@kingbob would this mean that I would have to make a text file with the passwords and usernames and then encrypt and then delete the text file? But surely that would be very insecure is there a better way of doing it?

Your whole idea is insecure, so it doesn't make a lot of difference. Why would you have to encrypt it and then delete it? That makes absolutely no sense.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

@ king bob

I looked up the function _Crypt_EncryptFile ( $sSourceFile, $sDestinationFile, $vCryptKey, $iAlgID )

and it says there needs to be a $sSourceFile so I assumed that I would have to make a text file with the data in already and that it would make a new seperate file that is encrypted? Im sorry for the inexpertise I only started autoit a couple of days ago and I have never tried storing data other than in the form of text.

@jchd with the FileRead() with Fopen() function how would I know where to split the binary into the passwords and usernames to go into an array?

(sorry for the highlight, i dont know how to change it)

Edited by Melba23
Highlight removed

Share this post


Link to post
Share on other sites

My question was towards, what are you encrypting the data for if not to put it into a file to store it? If you're not storing the encrypted data, then why are you encrypting it?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Besides what KingBob just pointed out, you would have found _Crypt_EncryptData / _Crypt_DecryptData in the help file if you had looked at the Crypt functions there.

Then you don't have to split the binary (that would never work). Encrypt your data block (e.g. string), giving you binary data, write that to a binary file. Read it at some other time, decrypt it and you can recover your block of data verbatim. Then you can pick whatever piece of information you need inside this plain data.

Now be aware that if you store the passphrase inside the script, the whole operation is essentially pointless just because whatever is put in clear or somehow obfuscated inside a script is ... well ... almost like you post it on FarceBook. Let's say that a script (compiled or not) is unsafe from prying eyes, readable by sister kid and let's don't discuss this point further here.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Im really bad at explaining things (and somewhat understanding) so thank you for your patience and your replies, basically I want/tried the input taken from the username input field and the password input field to be stored cryptographically in a text file (or in some other way (I have only ever dealt with text files so it's seems the most easiest option)) . I want it stored so that when the program is closed and started up again I want the usernames that are in the text file to be in the combo box.

 

func decrypt()
    FileOpen("data.txt", $FO_READ + $FO_BINARY)
;~  FileClose("data.txt")
;~  FileOpen("data.txt", 0)
consolewrite(_FileCountLines("data.txt") & " lines in the file" & @CRLF)
    for $i = 1 to (_FileCountLines("data.txt")/2)-1
        _ArrayAdd($usernames, BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i), "P14h735536jk3fvvbg", $CALG_AES_128)))
        _ArrayAdd($passwords,BinaryToString(_Crypt_DecryptData(FileReadLine("data.txt", $i+1), "P14h735536jk3fvvbg", $CALG_AES_128)))
    Next
     FileClose("data.txt")
    for $i = 0 to $i = ubound($usernames)-1 step 1 # should add the usernames to the combobox
      GUICtrlSetData($selectuser, $usernames[$i] & @CRLF)
   next
endfunc

This is why I have the function decrypt used before the gui starts

decrypt()
GUISetState(@SW_SHOW)
While True
   $nMsg = GUIGetMsg()
   Switch $nMsg
      Case $GUI_EVENT_CLOSE
         Exit
      Case $addusername
         updatecomboandinfo()
         encrypt()
         GUICtrlSetData($usernamefield, "")
         GUICtrlSetData($passwordfield, "")
      EndSwitch
WEnd

Atm I just want this part to work, I know the passwords and that can be easily be discovered but I want to work on that after I go this part done.

This function encrypt() is what I thought is storing the data?

func encrypt()
    FileOpen("data.txt",  $FO_OVERWRITE + $FO_BINARY)
    local $encryptedusername = _Crypt_EncryptData($usernames[ubound($usernames)-1], "P14h735536jk3fvvbg", $CALG_AES_128)
    local $encryptedpassword = _Crypt_EncryptData($passwords[ubound($passwords)-1], "P14h735536jk3fvvbg", $CALG_AES_128)
    FileWriteLine("data.txt", $encryptedusername)
    FileWriteLine("data.txt", $encryptedpassword)
    FileClose("data.txt")
EndFunc

 

I hope everyone understands what Im trying to achieve, Im struggling to see how I can use the _Crypt_EncryptFile function or _Crypt_DecryptFile, would I do something like _Crypt_EncryptFile(FileWriteLine("data.txt", $password[ubound($usernames)-1]fgklgjlg;hk Idk. Would it be easier using an .ini file to go about this? the problem is I haven't got a clue how .ini file works.

Share this post


Link to post
Share on other sites

You would write to the file, the data you want in it, then encrypt it. When you need to read from the file you decrypt it and then read it back. You can't use FileWriteLine with the data already encrypted and be able to read it back with FileReadLine, there is just no way for the function to be able to do that.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

You had many issues in your script

I fixed and modified and mainly as you are mixing strings and binary you have to deal properly with those

  • you cannot use filewriteline when dealing with binary
  • if you want to read by line you have to stringify your binarydata
  • I added some example in the beginning on whats happening with binary and string convert functions
  • use filehandle instead of opening everytime data.txt

advice: first build it uncrypted (i left the commented lines in there)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <Array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <File.au3>
#Include <Timers.au3>
#include <Crypt.au3>
#include <GuiEdit.au3>

$Form1 = GUICreate("Pie", 170, 235, 190, 200)
local $usernames[0]
local $passwords[0]

$myText="Hello World"
consolewrite($myText & " string() := " & string($myText) & @CRLF)
consolewrite($myText & " binaryTostring() := " & binaryTostring($myText) & @CRLF)
consolewrite($myText & " stringtobinary() := " & stringtoBinary($myText) & @CRLF)
consolewrite($myText & " binary() := " & binary($myText) & @CRLF)
consolewrite($myText & " binarytostring(binary()) := " & binarytostring(binary($myText)) & @CRLF)

$myText="0x48656C6C6F20576F726C64"
consolewrite($myText & " string() := " & string($myText) & @CRLF)
consolewrite($myText & " binaryTostring() := " & binaryTostring($myText) & @CRLF)
consolewrite($myText & " stringtobinary() := " & stringtoBinary($myText) & @CRLF)
consolewrite($myText & " binary() := " & binary($myText) & @CRLF)
consolewrite($myText & " binarytostring(binary()) := " & binarytostring(binary($myText)) & @CRLF)


global $selectuser = GUICtrlCreateCombo("", 21, 95, 130, 20)
$addusername = GUICtrlCreateButton("Add", 21, 170, 130, 30)
$usernamefield = GUICtrlCreateInput("", 21, 120, 130, 20)
$passwordfield = GUICtrlCreateInput("", 21, 145, 130, 20, 0x0020)

func encrypt()
    Local $hFileOpen = FileOpen("data.txt",  $FO_APPEND)
;~     Local $hFileOpen = FileOpen("data.txt",  $FO_OVERWRITE + $FO_BINARY)

    if guictrlread($usernamefield) <> "" then
;~      local $encryptedusername = string(guictrlread($usernamefield))
;~      local $encryptedpassword = string(guictrlread($passwordfield))
        local $encryptedusername = string(_Crypt_EncryptData(guictrlread($usernamefield), "P14h735536jk3fvvbg", $CALG_AES_128))
        local $encryptedpassword = string(_Crypt_EncryptData(guictrlread($passwordfield ), "P14h735536jk3fvvbg", $CALG_AES_128))

        FileWrite($hFileOpen, $encryptedusername & @CRLF)
        FileWrite($hFileOpen, $encryptedpassword & @CRLF)
    EndIf

    FileClose($hFileOpen)
EndFunc

func decrypt()
    Local $hFileOpen = FileOpen("data.txt", $FO_READ)

    consolewrite(_FileCountLines("data.txt") & " lines in the file" & @CRLF)

    for $i = 1 to _FileCountLines("data.txt")/2
        local $encryptedusername = FileReadLine($hFileOpen)
        local $encryptedpassword = FileReadLine($hFileOpen)

        consolewrite($encryptedusername  & @CRLF)
        consolewrite($encryptedpassword  & @CRLF)

;~         _ArrayAdd($usernames, FileReadLine($hFileOpen))
;~         _ArrayAdd($passwords, FileReadLine($hFileOpen))

        _ArrayAdd($usernames, binarytostring(_Crypt_DecryptData(binary($encryptedusername), "P14h735536jk3fvvbg", $CALG_AES_128)))
        _ArrayAdd($passwords, binarytostring(_Crypt_DecryptData(binary($encryptedpassword), "P14h735536jk3fvvbg", $CALG_AES_128)))
    Next

    FileClose($hFileOpen)

    consolewrite(ubound($usernames) & " entries in array")
    _arraydisplay($usernames)

    if ubound($userNames)> 0 then
        for $i = 0 to ubound($usernames)-1
            GUICtrlSetData($selectuser, $usernames[$i] & @CRLF)
        next
    EndIf
endfunc

func updatecomboandinfo()
   if not GUICtrlRead($usernamefield) = "" Then
      _ArrayAdd($usernames, GUICtrlRead($usernamefield))
      _ArrayAdd($passwords, GUICtrlRead($passwordfield))

      GUICtrlSetData($selectuser, $usernames[UBound($usernames)-1])

   EndIf
EndFunc

decrypt()

GUISetState(@SW_SHOW)
While True
   $nMsg = GUIGetMsg()
   Switch $nMsg
      Case $GUI_EVENT_CLOSE
         Exit
      Case $addusername
         updatecomboandinfo()
         encrypt()
         GUICtrlSetData($usernamefield, "")
         GUICtrlSetData($passwordfield, "")
      EndSwitch
WEnd

 

Edited by junkew
1 person likes this

Share this post


Link to post
Share on other sites

You had many issues in your script

I fixed and modified and mainly as you are mixing strings and binary you have to deal properly with those

  • you cannot use filewriteline when dealing with binary
  • if you want to read by line you have to stringify your binarydata
  • I added some example in the beginning on whats happening with binary and string convert functions
  • use filehandle instead of opening everytime data.txt

advice: first build it uncrypted (i left the commented lines in there)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
#include <Array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <File.au3>
#Include <Timers.au3>
#include <Crypt.au3>
#include <GuiEdit.au3>

$Form1 = GUICreate("Pie", 170, 235, 190, 200)
local $usernames[0]
local $passwords[0]

$myText="Hello World"
consolewrite($myText & " string() := " & string($myText) & @CRLF)
consolewrite($myText & " binaryTostring() := " & binaryTostring($myText) & @CRLF)
consolewrite($myText & " stringtobinary() := " & stringtoBinary($myText) & @CRLF)
consolewrite($myText & " binary() := " & binary($myText) & @CRLF)
consolewrite($myText & " binarytostring(binary()) := " & binarytostring(binary($myText)) & @CRLF)

$myText="0x48656C6C6F20576F726C64"
consolewrite($myText & " string() := " & string($myText) & @CRLF)
consolewrite($myText & " binaryTostring() := " & binaryTostring($myText) & @CRLF)
consolewrite($myText & " stringtobinary() := " & stringtoBinary($myText) & @CRLF)
consolewrite($myText & " binary() := " & binary($myText) & @CRLF)
consolewrite($myText & " binarytostring(binary()) := " & binarytostring(binary($myText)) & @CRLF)


global $selectuser = GUICtrlCreateCombo("", 21, 95, 130, 20)
$addusername = GUICtrlCreateButton("Add", 21, 170, 130, 30)
$usernamefield = GUICtrlCreateInput("", 21, 120, 130, 20)
$passwordfield = GUICtrlCreateInput("", 21, 145, 130, 20, 0x0020)

func encrypt()
    Local $hFileOpen = FileOpen("data.txt",  $FO_APPEND)
;~     Local $hFileOpen = FileOpen("data.txt",  $FO_OVERWRITE + $FO_BINARY)

    if guictrlread($usernamefield) <> "" then
;~      local $encryptedusername = string(guictrlread($usernamefield))
;~      local $encryptedpassword = string(guictrlread($passwordfield))
        local $encryptedusername = string(_Crypt_EncryptData(guictrlread($usernamefield), "P14h735536jk3fvvbg", $CALG_AES_128))
        local $encryptedpassword = string(_Crypt_EncryptData(guictrlread($passwordfield ), "P14h735536jk3fvvbg", $CALG_AES_128))

        FileWrite($hFileOpen, $encryptedusername & @CRLF)
        FileWrite($hFileOpen, $encryptedpassword & @CRLF)
    EndIf

    FileClose($hFileOpen)
EndFunc

func decrypt()
    Local $hFileOpen = FileOpen("data.txt", $FO_READ)

    consolewrite(_FileCountLines("data.txt") & " lines in the file" & @CRLF)

    for $i = 1 to _FileCountLines("data.txt")/2
        local $encryptedusername = FileReadLine($hFileOpen)
        local $encryptedpassword = FileReadLine($hFileOpen)

        consolewrite($encryptedusername  & @CRLF)
        consolewrite($encryptedpassword  & @CRLF)

;~         _ArrayAdd($usernames, FileReadLine($hFileOpen))
;~         _ArrayAdd($passwords, FileReadLine($hFileOpen))

        _ArrayAdd($usernames, binarytostring(_Crypt_DecryptData(binary($encryptedusername), "P14h735536jk3fvvbg", $CALG_AES_128)))
        _ArrayAdd($passwords, binarytostring(_Crypt_DecryptData(binary($encryptedpassword), "P14h735536jk3fvvbg", $CALG_AES_128)))
    Next

    FileClose($hFileOpen)

    consolewrite(ubound($usernames) & " entries in array")
    _arraydisplay($usernames)

    if ubound($userNames)> 0 then
        for $i = 0 to ubound($usernames)-1
            GUICtrlSetData($selectuser, $usernames[$i] & @CRLF)
        next
    EndIf
endfunc

func updatecomboandinfo()
   if not GUICtrlRead($usernamefield) = "" Then
      _ArrayAdd($usernames, GUICtrlRead($usernamefield))
      _ArrayAdd($passwords, GUICtrlRead($passwordfield))

      GUICtrlSetData($selectuser, $usernames[UBound($usernames)-1])

   EndIf
EndFunc

decrypt()

GUISetState(@SW_SHOW)
While True
   $nMsg = GUIGetMsg()
   Switch $nMsg
      Case $GUI_EVENT_CLOSE
         Exit
      Case $addusername
         updatecomboandinfo()
         encrypt()
         GUICtrlSetData($usernamefield, "")
         GUICtrlSetData($passwordfield, "")
      EndSwitch
WEnd

 

Thank you soooooo much!! That has cleared up everything! :)

Share this post


Link to post
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