Command3r Posted January 28, 2012 Share Posted January 28, 2012 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] Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 28, 2012 Moderators Share Posted January 28, 2012 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
sleepydvdr Posted January 28, 2012 Share Posted January 28, 2012 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> Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 28, 2012 Moderators Share Posted January 28, 2012 (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 January 28, 2012 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Command3r Posted January 28, 2012 Author Share Posted January 28, 2012 @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 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] Link to comment Share on other sites More sharing options...
ter-pierre Posted February 15, 2013 Share Posted February 15, 2013 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 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 15, 2013 Moderators Share Posted February 15, 2013 ter-pierre, You can encrypt and decrypt a multi-line file to which you keep adding lines of data like this: expandcollapse popup#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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ter-pierre Posted February 15, 2013 Share Posted February 15, 2013 Very good M23!"& Binary(0xFEFEFEFE)" make all difference!Many Thanks!I applaud you 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