Iraj Posted January 31, 2022 Posted January 31, 2022 Need help in writing file with StringEncrypt output. The file is getting write with special characters. Below is the code: #include <Crypt.au3> #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <Date.au3> Example() Func Example() Encrypt text using a generic password. Local $dEncrypted = StringEncrypt(True, 'SRV19', 'H@ppy') FileOpen ("C:\temp\info.txt", 2) FileWrite("C:\temp\info.txt",$dEncrypted) ;~ ; Display the encrypted text. ;~ MsgBox($MB_SYSTEMMODAL, 'Encrypted', $dEncrypted) ;~ ; Decrypt the encrypted text using the generic password. ;~ Local $sDecrypted = StringEncrypt(False, $dEncrypted, 'securepassword') ;~ ; Display the decrypted text. ;~ MsgBox($MB_SYSTEMMODAL, 'Decrypted', $sDecrypted) EndFunc ;==>Example Func StringEncrypt($bEncrypt, $sData, $sPassword) _Crypt_Startup() ; Start the Crypt library. Local $vReturn = '' If $bEncrypt Then ; If the flag is set to True then encrypt, otherwise decrypt. $vReturn = _Crypt_EncryptData($sData, $sPassword, $CALG_RC4) Else $vReturn = BinaryToString(_Crypt_DecryptData($sData, $sPassword, $CALG_RC4)) EndIf _Crypt_Shutdown() ; Shutdown the Crypt library. Return $vReturn EndFunc ;==>StringEncrypt Kindly assist!
Musashi Posted January 31, 2022 Posted January 31, 2022 You should use the same password for encryption and decryption (here "H@ppy" , not 'securepassword') #include <Crypt.au3> #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <Date.au3> Example() Func Example() ;Encrypt text using a generic password. Local $dEncrypted = StringEncrypt(True, 'SRV19', 'H@ppy') FileOpen (@ScriptDir & "\SampleText.txt", 2) FileWrite(@ScriptDir & "\SampleText.txt",$dEncrypted) ;~ ; Display the encrypted text. MsgBox($MB_SYSTEMMODAL, 'Encrypted', $dEncrypted) ;~ ; Decrypt the encrypted text using the generic password. ;~ Local $sDecrypted = StringEncrypt(False, $dEncrypted, 'securepassword') Local $sDecrypted = StringEncrypt(False, FileRead(@ScriptDir & "\SampleText.txt"), 'H@ppy') ;~ ; Display the decrypted text. MsgBox($MB_SYSTEMMODAL, 'Decrypted', $sDecrypted) EndFunc ;==>Example Func StringEncrypt($bEncrypt, $sData, $sPassword) _Crypt_Startup() ; Start the Crypt library. Local $vReturn = '' If $bEncrypt Then ; If the flag is set to True then encrypt, otherwise decrypt. $vReturn = _Crypt_EncryptData($sData, $sPassword, $CALG_RC4) Else $vReturn = BinaryToString(_Crypt_DecryptData($sData, $sPassword, $CALG_RC4)) EndIf _Crypt_Shutdown() ; Shutdown the Crypt library. Return $vReturn EndFunc ;==>StringEncrypt "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."
Iraj Posted January 31, 2022 Author Posted January 31, 2022 I am getting attached issue while writing to the file Encrypted text is different than what is written to the file.
Solution Musashi Posted January 31, 2022 Solution Posted January 31, 2022 17 minutes ago, Iraj said: Encrypted text is different than what is written to the file. What happens, when you use this : FileOpen (@ScriptDir & "\SampleText.txt", 2 + 16) FileWrite(@ScriptDir & "\SampleText.txt", StringToBinary($dEncrypted)) "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."
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