Jump to content

Help replacing _StringEncrypt


Go to solution Solved by Melba23,

Recommended Posts

Hi everyone.

I have a simple script that takes some data from a server and send it to another server using a webbservice. Just for the purpose of not sending the data in plain text, i encrypt the textstring using the _StringEncrypt function. I know its not that safe, and it dpoesnt have to be 100% safe. I just dont want to send it as plain text for anyone to sniff and see. 

$encryptedstring = _StringEncrypt(1,FileRead($file),$password)

to encrypt it on one and, and then

filewrite(_StringEncrypt(0,$encryptedstring,$password) )
 

The problem is that in the latest version of Autoit, they removed StringEncrypt. So now what? I have tried using _Crypt_EncryptData and then _Crypt_DecryptData, but i cant get it to work and it seems to be overcomplicated. I just need somthig simple. Anyone have any idees?

Thanks.

Link to comment
Share on other sites

Posting the code you've tried to encrypt/decrypt would greatly help us help you, don't you think so?

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)

Link to comment
Share on other sites

Posting the code you've tried to encrypt/decrypt would greatly help us help you, don't you think so?

 

Nah. Posting the code would only complicate things. What im looking for is an alternative to _StringEncrypt witch just encrypts a string with a password, and then you can decrypt it again. Thats exactly what i would like to do.

When i try this, it doesnt work:

#include <crypt.au3>
$password = "P@ssw0rd"
$string = "This is the string to encrypt"
$data = _Crypt_EncryptData($string, $password ,$CALG_RC4)
MsgBox(0,"",$data)
$decrypted = _Crypt_DecryptData($data,$password,$CALG_RC4)
MsgBox(0,"",$decrypted)

What was wrong with the old function that workd perfectly? :P

Link to comment
Share on other sites

  • Moderators

Tjalve,

As the Help file explains, the returned values are binary - so you need to convert them: :)

#include <Crypt.au3>
#include <MsgBoxConstants.au3>

$sPassword = "P@ssw0rd"

$sString = "This is the string to encrypt"

$bData = _Crypt_EncryptData($sString, $sPassword, $CALG_RC4)
MsgBox($MB_SYSTEMMODAL, "Encrypted", BinaryToString($bData))

$bDecrypted = _Crypt_DecryptData($bData, $sPassword, $CALG_RC4)
MsgBox($MB_SYSTEMMODAL, "Decrypted", BinaryToString($bDecrypted))
As to why the old function has been removed - the new ones are more secure and (once you read the Help file) just as easy to use. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Tjalve,

As the Help file explains, the returned values are binary - so you need to convert them: :)

#include <Crypt.au3>
#include <MsgBoxConstants.au3>

$sPassword = "P@ssw0rd"

$sString = "This is the string to encrypt"

$bData = _Crypt_EncryptData($sString, $sPassword, $CALG_RC4)
MsgBox($MB_SYSTEMMODAL, "Encrypted", BinaryToString($bData))

$bDecrypted = _Crypt_DecryptData($bData, $sPassword, $CALG_RC4)
MsgBox($MB_SYSTEMMODAL, "Decrypted", BinaryToString($bDecrypted))
As to why the old function has been removed - the new ones are more secure and (once you read the Help file) just as easy to use. ;)

M23

 

I guess that i just have to RTFM again then :P

Just one more question. Is the new version compatible with the old one? if i encrypted a string (using the new function), wrote it into a file, and then open the file on another computer an try to decrypt it using the old function. Would it work?(im guessing not, but it would save me ALOT of time if it did).

Link to comment
Share on other sites

  • Moderators

Tjalve,

I am afraid that the 2 encryption methods are incompatible - sorry. :(

M23

Edited by Melba23
Typo

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Well Im still having problems. The problem is that i cannot get the string as binary data...

My webbservice takes a string of characters and save them in a textfile on the server. befor the encryted string was hex data and therefor was no problem to send as a string to the webbservice, save in the file and then decrypt it on the other end. However now when the data is binary, i get a file thats empy on the other end. 

I tried to convert the binary data to a string and then send it. But it seems that you cannot convert the encrypted data to string befor its decrypted.

Any one have any sugestions? I need tyhe encrypted data to be a string. Not binary...

I also tried to convert the encrypted binary to hex, send it (works) and then convert it back to binary, and then decrypt it. Didnt work...

Edited by Tjalve
Link to comment
Share on other sites

  • Moderators

Tjalve,

The paired functions BinaryToString & StringToBinary should be able to deal with that:

#include <Crypt.au3>
#include <MsgBoxConstants.au3>

$sFile = "File.txt"

$sPassword = "P@ssw0rd"

$sString = "This is the string to encrypt"

; Encrypt and convert to string
$sData = BinaryToString(_Crypt_EncryptData($sString, $sPassword, $CALG_RC4))
MsgBox($MB_SYSTEMMODAL, "Encrypted", $sData)

; Write the string to a file
FileWrite($sFile, $sData)

Sleep(1000)

; Read the string from file
$sData = FileRead($sFile)
FileDelete($sFile)

; Decrypt the string converted to binary
$bDecrypted = _Crypt_DecryptData(StringToBinary($sData), $sPassword, $CALG_RC4)
; And then reconvert to a string to display
MsgBox($MB_SYSTEMMODAL, "Decrypted", BinaryToString($bDecrypted))
Works for me. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Yes, that works for me aswell. The probem seems to be that my webbservice cannot handle the characters that meks up the encrypted string.

I actallu think that im going to copy the old function into my script and just keep it as it was. If that even works...

Link to comment
Share on other sites

  • Moderators
  • Solution

Tjalve,

So save the file directly as a hex string and let AutoIt do most of the work for you:

#include <Crypt.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>

$sFile = "File.txt"

$sPassword = "P@ssw0rd"

$sString = "This is the string to encrypt"

; Encrypt
$bData = _Crypt_EncryptData($sString, $sPassword, $CALG_RC4)
MsgBox($MB_SYSTEMMODAL, "Encrypted", $bData)

; Write the returned binary (in the form of a hex string) to a file

FileWrite($sFile, String($bData))

; Read the hex string from file
$sData = FileRead($sFile)
FileDelete($sFile)

; Decrypt the string
$bDecrypted = _Crypt_DecryptData($sData, $sPassword, $CALG_RC4)
; And then reconvert to a string to display
MsgBox($MB_SYSTEMMODAL, "Decrypted", BinaryToString($bDecrypted))
Any better? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Tjalve,

So save the file directly as a hex string and let AutoIt do most of the work for you:

#include <Crypt.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>

$sFile = "File.txt"

$sPassword = "P@ssw0rd"

$sString = "This is the string to encrypt"

; Encrypt
$bData = _Crypt_EncryptData($sString, $sPassword, $CALG_RC4)
MsgBox($MB_SYSTEMMODAL, "Encrypted", $bData)

; Write the returned binary (in the form of a hex string) to a file

FileWrite($sFile, String($bData))

; Read the hex string from file
$sData = FileRead($sFile)
FileDelete($sFile)

; Decrypt the string
$bDecrypted = _Crypt_DecryptData($sData, $sPassword, $CALG_RC4)
; And then reconvert to a string to display
MsgBox($MB_SYSTEMMODAL, "Decrypted", BinaryToString($bDecrypted))
Any better? :)

M23

 

Wow, that works great!!

But im bit confused on how this works. When you use string() on the binary data, you get a hex? 

And then when you decrypt it, you dont have to convert it back to binary first??

Link to comment
Share on other sites

  • Moderators

Tjalve,

You will have to get a Dev or someone like jchd or Mat to give you chapter and verse of what is going on, but to me it looks like AutoIt trying to get the data into what it thinks is a suitable format. I always have to play around a bit when using Binary to make sure I get the result I need - I use a lot of ConsoleWrite & MsgBox lines to display what is actually in the variable. Try it and see. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

I ruefully admit I also sometimes need to display things to ensure I get the proper type.

Joker!

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)

Link to comment
Share on other sites

  • 1 month later...

Tjalve et al.,

I used the old _StringEncrypt() function quite excessively and I am quite willing to convert to the much better and more flexible new one based on <Crypt.au3>.

However I have also the problem of having old compiled programs on remote machines (not accessible to me) using the old encryption to access encrypted data locally as well as accessing encrypted data over the Internet. Introducing an update with the new encryption will break the system.

I probably have to write a module which converts (replaces) old to (with) new if a user performs an update. In order to be able to still use the old function (to decrypt old data) I have extracted the _StringEncrypt() function section out of the old <String.au3> file and included in my code so that I can still use it. I can then use the new to write the data back using the new encryption.

Cheers

Mungo

Edited by Mungo
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...