我在_Crypt_EncryptData()上遇到了问题。我想加密数据,存储数据,然后把它读回解密。
_Crypt_EncryptData()和_Crypt_DecryptData()似乎不对称;前者对输出值执行隐式十六进制编码。但是后者对输入执行隐式二进制转换(到目前为止还不错),然后对其输出执行隐式十六进制转换!因此,在一个文件中:
$ciphertext=_Crypt_EncryptData($cleartext, $g_hKey, $CALG_3DES)
$cleartext=_HexToString(_Crypt_DecryptData($ciphertext, $g_hKey, $CALG_3DES))(尔克!)会把原稿还给我的。我无法从跨调用的文件中恢复明文。密文每次都在变化,例如使用字符串“这是一个测试”,在随后的执行中,我得到:
0x0B656F9BCC35B73A6EA9D08701E78713
0xEBE1E744668C379CE74480C3A56303A2
0x25F50D6B833B3CEF60FCFAF8AE673CF3如果考虑到不同的初始化向量,我预计会出现这样的情况,但是看"Crypt.au3“,我看不到设置或获取IV的方法(我知道DES3是不安全的--这是一场不同的战斗)。是我还是AutoIt?
以下是重现该问题的脚本的全部来源:
#include <StringConstants.au3>
#include <Crypt.au3>
#include <String.au3>
_Crypt_Startup()
$inifile="C:\test_au_enc.ini"
$g_hKey = _Crypt_DeriveKey("s3cr3t.S4uce", $CALG_3DES)
; test previous invocation
$readback=IniRead($inifile, "main", "pass", "Failed")
if ("Failed"=$readback) Then
MsgBox(0, "Enc Dec", "Failed to read ini file")
Else
$dec=_HexToString(_Crypt_DecryptData($readback, $g_hKey, $CALG_3DES))
MsgBox(0,"Enc Dec", "Read from previous: " & $dec)
; this fails to recover the cleartext
EndIf
$subj=InputBox("Enc Dec", "Please supply a string to encrypt", "This is a test");
; encrypt the string and write it to a file...
$enc=_Crypt_EncryptData($subj, $g_hKey, $CALG_3DES)
IniWrite($inifile, "main", "pass", $enc)
; now read back the value and decrypt
$readback=IniRead($inifile, "main", "pass", "Failed")
$dec=_HexToString(_Crypt_DecryptData($readback, $g_hKey, $CALG_3DES))
InputBox("Enc Dec", "Encrypted:" & $enc & @CRLF & "decrypted:" & $dec, $enc)
; here the decrypted text matches the cleartext 发布于 2017-12-27 18:27:25
根据帮助档案,使用_Crypt_DeriveKey()是正确的,但是当使用自己的派生键时,您应该像这样调用_Crypt_EncryptData()和_Crypt_DecryptData():
$enc = _Crypt_EncryptData($subj, $g_hKey, $CALG_USERKEY)
$dec = _HexToString(_Crypt_DecryptData($readback, $g_hKey, $CALG_USERKEY))$CALG_USERKEY是$iAlgID参数的差异,它告诉我们将$vCryptKey参数视为密钥的句柄,而不是密码。这似乎如预期的那样工作。
以下是完整的代码:
#include <StringConstants.au3>
#include <Crypt.au3>
#include <String.au3>
_Crypt_Startup()
$inifile="C:\test_au_enc.ini"
$g_hKey = _Crypt_DeriveKey("s3cr3t.S4uce", $CALG_3DES)
; test previous invocation
$readback=IniRead($inifile, "main", "pass", "Failed")
if ("Failed"=$readback) Then
MsgBox(0, "Enc Dec", "Failed to read ini file")
Else
$dec=_HexToString(_Crypt_DecryptData($readback, $g_hKey, $CALG_USERKEY))
MsgBox(0,"Enc Dec", "Read from previous: " & $dec)
; this fails to recover the cleartext
EndIf
$subj=InputBox("Enc Dec", "Please supply a string to encrypt", "This is a test");
; encrypt the string and write it to a file...
$enc=_Crypt_EncryptData($subj, $g_hKey, $CALG_USERKEY)
IniWrite($inifile, "main", "pass", $enc)
; now read back the value and decrypt
$readback=IniRead($inifile, "main", "pass", "Failed")
$dec=_HexToString(_Crypt_DecryptData($readback, $g_hKey, $CALG_USERKEY))
InputBox("Enc Dec", "Encrypted:" & $enc & @CRLF & "decrypted:" & $dec, $enc)
; here the decrypted text matches the cleartexthttps://stackoverflow.com/questions/47995507
复制相似问题