首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >_Crypt_EncryptData()和_Crypt_DecryptData()混淆

_Crypt_EncryptData()和_Crypt_DecryptData()混淆
EN

Stack Overflow用户
提问于 2017-12-27 16:28:55
回答 1查看 557关注 0票数 0

我在_Crypt_EncryptData()上遇到了问题。我想加密数据,存储数据,然后把它读回解密。

_Crypt_EncryptData()_Crypt_DecryptData()似乎不对称;前者对输出值执行隐式十六进制编码。但是后者对输入执行隐式二进制转换(到目前为止还不错),然后对其输出执行隐式十六进制转换!因此,在一个文件中:

代码语言:javascript
复制
$ciphertext=_Crypt_EncryptData($cleartext, $g_hKey, $CALG_3DES)
$cleartext=_HexToString(_Crypt_DecryptData($ciphertext, $g_hKey, $CALG_3DES))

(尔克!)会把原稿还给我的。我无法从跨调用的文件中恢复明文。密文每次都在变化,例如使用字符串“这是一个测试”,在随后的执行中,我得到:

代码语言:javascript
复制
 0x0B656F9BCC35B73A6EA9D08701E78713
 0xEBE1E744668C379CE74480C3A56303A2
 0x25F50D6B833B3CEF60FCFAF8AE673CF3

如果考虑到不同的初始化向量,我预计会出现这样的情况,但是看"Crypt.au3“,我看不到设置或获取IV的方法(我知道DES3是不安全的--这是一场不同的战斗)。是我还是AutoIt?

以下是重现该问题的脚本的全部来源:

代码语言:javascript
复制
#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 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-27 18:27:25

根据帮助档案,使用_Crypt_DeriveKey()是正确的,但是当使用自己的派生键时,您应该像这样调用_Crypt_EncryptData()_Crypt_DecryptData()

代码语言:javascript
复制
$enc = _Crypt_EncryptData($subj, $g_hKey, $CALG_USERKEY)
$dec = _HexToString(_Crypt_DecryptData($readback, $g_hKey, $CALG_USERKEY))

$CALG_USERKEY$iAlgID参数的差异,它告诉我们将$vCryptKey参数视为密钥的句柄,而不是密码。这似乎如预期的那样工作。

以下是完整的代码:

代码语言:javascript
复制
#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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47995507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档