我想使用Tcllib中的DES函数,但似乎我没有处理 变量以正确的方式。
以下是代码:
set key DAAE57F813459B3B
set key_b [binary format H* $key]
set data 2D7A99F520D684B4
set data_b [binary format H* $data]
set result [DES::des -dir encrypt -key $key_b -hex $data_b]
使用这些值时出现错误:
bad option "-z...": must be one of -chunksize, -dir, -hex, -in, -iv, -key, -mode, -out, -weak
似乎DES功能将'2D ...'解释为'-z ...',因此作为选项(不允许)。
当我交换值(键< - >数据,数据< - >键)时,我没有收到错误。
此外,当我使用数据1D ...,3D ...等时,该功能正常工作。
我正在使用Tcllib V1.18和包:des,sha1,pki,asn,aes,math :: bignum,md5,base64。
有没有人知道如何将变量'data'移交给DES函数而不将数据解释为选项?
答案 0 :(得分:1)
问题是DES::des
命令中的高级驱动程序代码被以-
(0x2D)字节开头的数据混淆。这是一个可报告的错误;请报告。
但您可以使用Tcllib des软件包中的低级接口来解决这个问题:
package require des
set key DAAE57F813459B3B
set key_b [binary format H* $key]
set data 2D7A99F520D684B4
set data_b [binary format H* $data]
# The [binary format] below is for the initialisation vector (IV) which should usually be
# 64 zero bits to start with if you're using simple encryption.
set d [DES::Init cbc $key_b [binary format I* {0 0}]]
set encBytes [DES::Encrypt $d $data_b]
DES::Final $d
# We've got the bytes out; let's convert to hex for printing
binary scan $encBytes H* encHex
puts $encHex; # --> “4cd33892969591b4”
反方向也很容易:
package require des
set key DAAE57F813459B3B
set key_b [binary format H* $key]
set encHex 4cd33892969591b4
set encBytes [binary format H* $encHex]
set d [DES::Init cbc $key_b [binary format I* {0 0}]]
set decBytes [DES::Decrypt $d $encBytes]
DES::Final $d
binary scan $decBytes H* decHex
puts $decHex; # --> “2d7a99f520d684b4”
答案 1 :(得分:0)
使用开关结束标记“-”似乎也为我解决了该错误(使用des 1.1.0)。
set result [DES::des -dir encrypt -key $key_b -hex -- $data_b]
puts $result
=> 4cd33892969591b4