Modify

#3516 closed Bug (Fixed)

Specify types of parameters of _Crypt_EncryptData

Reported by: Sponge Jhan Owned by: J-Paul Mesnage
Milestone: 3.3.15.1 Component: Documentation
Version: 3.3.15.0 Severity: None
Keywords: _Crypt_EncryptData Cc:

Description

The input $vData and $vKey of _Crypt_EncryptData must be bytes rather than other types.
However, the documentation in [1] and AutoIt.chm both don't explicitly specify this.
StringEncrypt function in [1] also cannot decrypt encrypted non-ASCII characters correctly, such as Chinese, because it doesn't call StringToBinary in advance.

[1] https://www.autoitscript.com/autoit3/docs/libfunctions/_Crypt_EncryptData.htm

Attachments (0)

Change History (5)

comment:1 by J-Paul Mesnage, on Nov 25, 2016 at 3:45:49 PM

Component: Standard UDFsDocumentation

In fact the doc say implicitly that the parameters are variants ($v*) so it is the user responsability to give string or binary.
I understand that for chinease "characters" it seems better to use Binary as the AutoIt strings (UCS2) is not enough to handle all Chinese "characters"

Can you provide a repro script so I can verify what should be done in the doc?

Thanks

comment:2 by jchd18, on Nov 26, 2016 at 10:39:37 PM

Sidenote: strings really should be converted to UTF8 before encryption and back from UTF8 after decryption, so that portability is achieved in all use cases (non-codepage chars in plaintext string or different codepages between encryption and decryption machines).

Maybe the doc should mention that and perhaps give a multi-lingual example (e.g. plaintext = "Hello! ជំរាបសួរ! Allô! Привет! 您好!مرحبا! હેલો! שלום! こんにちは!").

in reply to:  1 comment:3 by Sponge Jhan, on Nov 28, 2016 at 8:14:55 PM

Replying to Jpm:

Can you provide a repro script so I can verify what should be done in the doc?

Thanks

Please run the code at the end of this comment.
I do the same as the documentation, encrypting without explicit StringToBinary call and

decrypting with BinaryToString using default encoding.
My machine reports 59/56 for two lengths.
Thanks!

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

$plaintext = "Hello! ជំរាបសួរ! Allô! Привет! 您好!مرحبا! હેલો! שלום! こんにちは!"
$ciphertext = _Crypt_EncryptData($plaintext, "test", $CALG_RC4)
$result = BinaryToString(_Crypt_DecryptData($ciphertext, "test", $CALG_RC4))
MsgBox($MB_OK, "Result", $result & @LF & _

"Length original: " & StringLen($plaintext) & @LF & _
"Length after decryption: " & StringLen($result))

comment:4 by J-Paul Mesnage, on Nov 29, 2016 at 8:17:10 AM

Thanks,
I was figuring out your pb.
As thre is no way to be sure that input data can be decrypted if internal conversion is done during encryption. It is the user responsability do do it.
I will change the doc to reflect what has to be done.
see the following example to do the right handling

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

Local $sPlaintext = "Hello! ជំរាបសួរ! Allô! Привет! 您好!مرحبا! હેલો! שלום! こんにちは!"

Local $dPlaintextUTF8 = StringToBinary($sPlaintext, $SB_UTF8) ; Convert to Binary string converting Unicode char as UTF8
;~ $dPlaintextUTF8 = $sPlaintext ; If Uncommented willshow why UTF8 conversion is needed

Local $iAlgorithm = $CALG_3DES
Local $g_hKey = _Crypt_DeriveKey("CryptPassword", $iAlgorithm)

Local $dEncrypted = _Crypt_EncryptData($dPlaintextUTF8, $g_hKey, $CALG_USERKEY) ; Encrypt the text with the new cryptographic key.

Local $dDecrypted = _Crypt_DecryptData($dEncrypted, $g_hKey, $CALG_USERKEY) ; Decrypt the data using the generic password string. The return value is a binary string.

Local $sDecrypted = BinaryToString($dDecrypted, $SB_UTF8) ; Convert the binary string using BinaryToString to display the initial data we encrypted.

If $sPlaintext = $sDecrypted Then
	MsgBox($MB_SYSTEMMODAL, "Decrypted data", $sDecrypted)
Else
	MsgBox($MB_SYSTEMMODAL, "BAD Decrypted data", $sPlaintext & @CRLF & "-->" & @CRLF & $sDecrypted)
EndIf

Last edited on Nov 29, 2016 at 9:25:11 AM by J-Paul Mesnage (previous) (diff)

comment:5 by J-Paul Mesnage, on Nov 30, 2016 at 5:51:57 PM

Milestone: 3.3.15.1
Owner: set to J-Paul Mesnage
Resolution: Fixed
Status: newclosed

Fixed by revision [11822] in version: 3.3.15.1

Modify Ticket

Action
as closed The owner will remain J-Paul Mesnage.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.