Jump to content

[Solved]Replace line from file(delete and write new)


Recommended Posts

Hey guys,

 

I am working on creating an ecrypted text file that will save encrypted text and read it back as well as replace it when the time comes to update it. Currently my focus is on replacing the text as I know and have been able to duplicate reading it but replacing the encryption is what is killing me. Here is what I have so far, its basic as I like to playing around with it first before i throw it into the programs.

 

#include <Crypt.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>
#include <GuiConstants.au3>
#include <ButtonConstants.au3>



Example()

Func Example()
    $Gui=GUICreate("Encryptogram",200,200)
    $Inputbox=GUICtrlCreateInput("",5,5)
    $Button=GUICtrlCreateButton("Encrypt me",5,35)
    GUISetState(@SW_SHOW)
While 1
    sleep(10)
switch GUIGetMsg()

    Case $Button
    Local Const $sUserKey = "CryptPassword" ; Declare a password string to decrypt/encrypt the data.
    Local $sData = GUICtrlRead($Inputbox)

    Local $bEncrypted = _Crypt_EncryptData($sData, $sUserKey, $CALG_AES_256) ; Encrypt the data using the generic password string.
    MsgBox(0,"",($bEncrypted))
    $sFile = FileWrite("....\Encryption.txt", StringReplace("", FileReadLine("....\Encryption.txt",1), $bEncrypted))
    $oFile = FileOpen("....\Encryption.txt")
    $RFile = FileReadLine("....\Encryption.txt", 1)
    $cFile = FileClose("....\Encryption.txt")

    $bEncrypted = _Crypt_DecryptData($RFile, $sUserKey, $CALG_AES_256) ; Decrypt the data using the generic password string. The return value is a binary string.
    MsgBox($MB_SYSTEMMODAL, "Decrypted data", BinaryToString($bEncrypted)) ; Convert the binary string using BinaryToString to display the initial data we encrypted.
    case $GUI_EVENT_CLOSE
        Exit
EndSwitch
WEnd
EndFunc

I search the forums but I am still new to the way the search is now and some of the results are just too broad, i typically find myself eyeballing all the new posts just to see if any of it could be helpful for me later. :)

 

Thanks,

 

EDIT:::Changed title to match more of what I am trying to do.

 

Edit Final: fixed the word edit and remove proprietary information :)

Edited by tweakster2010
Link to comment
Share on other sites

Well the way I am going about it, i actualyl just went with iniwrite and iniread. It works better for me for the results I want and still keeps the encryption I am after, i just can hide the file so no1 knows its there. :) Trying to meet all admin protocols for useful tools at work can be a pain!

 

#include <Crypt.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>
#include <GuiConstants.au3>
#include <ButtonConstants.au3>



Example()

Func Example()
    $Gui=GUICreate("Encryptogram",200,200)
    $Inputbox=GUICtrlCreateInput("",5,5)
    $Button=GUICtrlCreateButton("FIRE",5,35)
    GUISetState(@SW_SHOW)
While 1
    sleep(10)
switch GUIGetMsg()

    Case $Button
    Local Const $sUserKey = "CryptPassword" ; Declare a password string to decrypt/encrypt the data.
    Local $sData = GUICtrlRead($Inputbox)

    Local $bEncrypted = _Crypt_EncryptData($sData, $sUserKey, $CALG_AES_256) ; Encrypt the data using the generic password string.
    MsgBox(0,"",($bEncrypted))
    $WFile = IniWrite("....\Test.ini", "Password","Admin", $bEncrypted)
    $RFile = IniRead("....\Test.ini","Password","Admin","")

    $bEncrypted = _Crypt_DecryptData($RFile, $sUserKey, $CALG_AES_256) ; Decrypt the data using the generic password string. The return value is a binary string.
    MsgBox($MB_SYSTEMMODAL, "Decrypted data", BinaryToString($bEncrypted)) ; Convert the binary string using BinaryToString to display the initial data we encrypted.
    case $GUI_EVENT_CLOSE
        Exit
EndSwitch
WEnd
EndFunc   ;==>Example

 

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...