Sign in to follow this  
Followers 0
Command3r

FileWrite MD5 problem

8 posts in this topic

Hello, the script explaining it self :)

#include <Crypt.au3>
$file = @ScriptDir & "\test.txt"
$pw = InputBox("pw", "pw", "", "", "10", "130")
$Cr = _Crypt_EncryptData("Command3r", $pw, $CALG_RC4)
MsgBox(0, "", $Cr) ; here works

;~ FileOpen($file, 1)
;~ FileWrite($file, $Cr) ; here not working and writing another characters
;~ FileClose($file)

what should i do??


[font="arial, helvetica, sans-serif;"]Advice for you[/font][font="arial, helvetica, sans-serif;"]: [/font][u]Search[/u] before posting.

 

[font="arial, helvetica, sans-serif;"] *********** Problem solved? if yes [/font][color=rgb(0,0,0);font-family:arial, helvetica, sans-serif;] *********[/color]

[font="arial, helvetica, sans-serif;"]******* press "Mark Solved" button. *******[/font]

Share this post


Link to post
Share on other sites



Command3r,

Firstly, it is not an "MD5" problem - MD5 produces an irreversible hash from the data. You are using RC4 encryption, which does produce a reversable encryption. ;)

As to the "problem" itself, the encryption produces binary data so you need to use binary mode to write to, and read from, the file. A normal text editor will show weird characters becasue it tries to display the binary data as ASCII/Unicode - but the data is still there as this script shows:

#include <Crypt.au3>
$sFileName = @ScriptDir & "\test.txt"

; Get password and encrypt the data
$sPassWord = InputBox("pw", "pw", "", "", "10", "130")
$bEncryptedData_ToFile = _Crypt_EncryptData("Command3r", $sPassWord, $CALG_RC4) ; Put parameters in the correct order

; Write the data to file - in binary mode
$hFile = FileOpen($sFileName, 2 + 16)
FileWrite($hFile, $bEncryptedData_ToFile)
FileClose($hFile)

; Read from file - in binary mode
$hFile = FileOpen($sFileName, 16)
$bEncryptedData_FromFile = FileRead($hFile)
FileClose($hFile)

; Decrypt password
$sCleartext = BinaryToString(_Crypt_DecryptData($bEncryptedData_FromFile, $sPassWord, $CALG_RC4))

; And show it works
MsgBox(0, "", "Command3r" & @CRLF & $bEncryptedData_ToFile & @CRLF & $bEncryptedData_FromFile & @CRLF & $sCleartext)

One final point - look at the difference between my FileOpen/FileWrite lines and those you had. If you open a file with FileOpen you MUST use the returned handle with FileWrite or you get into all sorts of problems. ;)

All clear? Please ask if not. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

You could use _StringEncrypt, which I think is much easier to use. Also, you may want to look at InputBox again. You can use "*" on the 4th parameter to blank out the password.


#include <ByteMe.au3>

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

sleepydvdr,

_StringEncrypt is deprecated since the Crypt.au3 UDF was added. It may well vanish at some time in the future, so I would strongly advise using the _Crypt_* functions for all encryption/hash tasks. ;)

Good catch on the InputBox parameter. :)

M23

Edited by Melba23
Typo

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

@M23

Okay thanks M23 for the notice, ;) the script is working but i'll study it a little.. :) because the file which wrote makes me dispersion. :)

@sleepydvdr

_StringEncrypt is much easier but much slower too :D and about blank-ing the password i'll do it next time ;) thanks.


[font="arial, helvetica, sans-serif;"]Advice for you[/font][font="arial, helvetica, sans-serif;"]: [/font][u]Search[/u] before posting.

 

[font="arial, helvetica, sans-serif;"] *********** Problem solved? if yes [/font][color=rgb(0,0,0);font-family:arial, helvetica, sans-serif;] *********[/color]

[font="arial, helvetica, sans-serif;"]******* press "Mark Solved" button. *******[/font]

Share this post


Link to post
Share on other sites

Hi Melba23!

what if a multi-line file?

I try this and is not working...

$hFile = FileOpen($sFileName, 2 + 16)

$x=0

while $x<3

$bEncryptedData_ToFile = _Crypt_EncryptData(_NowCalc(), $sPassWord, $CALG_RC4) ; Put parameters in the correct order

FileWrite($hFile, $bEncryptedData_ToFile)

$x=$x+1

Sleep(2000)

WEnd

FileClose($hFile)

I write a tool that generate a txt log.

My issue is to crypt this log.

I will appreciate if you or anybody can help.

TKS

Share this post


Link to post
Share on other sites

ter-pierre,

You can encrypt and decrypt a multi-line file to which you keep adding lines of data like this:

#include <Crypt.au3>

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <Array.au3>
Global $aData[11][2]
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++

$sFileName = @ScriptDir & "\test.txt"

; Get password
$sPassWord = InputBox("pw", "pw", "", "", "10", "130")

; Open file in binary mode
$hFile = FileOpen($sFileName, 2 + 16)

; Simulation of your log file
For $i = 1 To 10
    ; Generate data to encrypt
    $sText = "Line " & $i & " at time: " & @MSEC
    ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ; Add to array so we can check if we decrypt correctly
    $aData[$i][0] = $sText
    ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ; Encrypt the data
    $bEncryptedData_ToFile = _Crypt_EncryptData($sText, $sPassWord, $CALG_RC4) ; Put parameters in the correct order
    ; Write the data to file - in binary mode - adding our delimiter
    FileWrite($hFile, $bEncryptedData_ToFile & Binary(0xFEFEFEFE))
Next

; Close the file
FileClose($hFile)

; Read from file - in binary mode
$hFile = FileOpen($sFileName, 16)
$bEncryptedData_FromFile = FileRead($hFile)
FileClose($hFile)

; Now split the file into the lines using our delimiter
$sChars = StringTrimLeft(String($bEncryptedData_FromFile), 2)
$aLines = StringSplit($sChars, "FEFEFEFE", 1)
For $i = 1 To 10
    $sCleartext = BinaryToString(_Crypt_DecryptData("0x" & $aLines[$i], $sPassWord, $CALG_RC4))
    ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ; Add to array
    $aData[$i][1] = $sCleartext
    ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Next

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++
; And display the results
_ArrayDisplay($aData)
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++

Everything between the +++++++ lines is just there to display the lines at the end to show that they were correctly decrypted - it is not needed for the encryption itself. ;)

All clear? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Very good M23!

"& Binary(0xFEFEFEFE)" make all difference!

Many Thanks!

I applaud you

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
Sign in to follow this  
Followers 0