_Crypt_EncryptData() 和 _Crypt_DecryptData() 混淆
_Crypt_EncryptData() and _Crypt_DecryptData() confusion
我 运行 遇到 _Crypt_EncryptData()
的问题。我想加密数据,存储它,然后解密读回。
好像_Crypt_EncryptData()
and _Crypt_DecryptData()
不对称;前者对输出值进行隐式十六进制编码。但后者对输入进行了隐式二进制转换(到目前为止还不错),但随后对其输出进行了隐式十六进制转换!因此在单个文件中:
$ciphertext=_Crypt_EncryptData($cleartext, $g_hKey, $CALG_3DES)
$cleartext=_HexToString(_Crypt_DecryptData($ciphertext, $g_hKey, $CALG_3DES))
(erk!) 会把原始的明文还给我。我无法跨调用从文件中恢复明文。密文每次都在变化,例如使用字符串 "This is a test",在随后的执行中我得到:
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
根据help file;使用 _Crypt_DeriveKey()
是正确的,但是在使用您自己的派生密钥时,您应该这样调用 _Crypt_EncryptData()
和 _Crypt_DecryptData()
:
$enc = _Crypt_EncryptData($subj, $g_hKey, $CALG_USERKEY)
$dec = _HexToString(_Crypt_DecryptData($readback, $g_hKey, $CALG_USERKEY))
$iAlgID
参数的区别是 $CALG_USERKEY
,它告诉我们将 $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 cleartext
我 运行 遇到 _Crypt_EncryptData()
的问题。我想加密数据,存储它,然后解密读回。
好像_Crypt_EncryptData()
and _Crypt_DecryptData()
不对称;前者对输出值进行隐式十六进制编码。但后者对输入进行了隐式二进制转换(到目前为止还不错),但随后对其输出进行了隐式十六进制转换!因此在单个文件中:
$ciphertext=_Crypt_EncryptData($cleartext, $g_hKey, $CALG_3DES)
$cleartext=_HexToString(_Crypt_DecryptData($ciphertext, $g_hKey, $CALG_3DES))
(erk!) 会把原始的明文还给我。我无法跨调用从文件中恢复明文。密文每次都在变化,例如使用字符串 "This is a test",在随后的执行中我得到:
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
根据help file;使用 _Crypt_DeriveKey()
是正确的,但是在使用您自己的派生密钥时,您应该这样调用 _Crypt_EncryptData()
和 _Crypt_DecryptData()
:
$enc = _Crypt_EncryptData($subj, $g_hKey, $CALG_USERKEY)
$dec = _HexToString(_Crypt_DecryptData($readback, $g_hKey, $CALG_USERKEY))
$iAlgID
参数的区别是 $CALG_USERKEY
,它告诉我们将 $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 cleartext