我试图用pycrypto重写Python中的一些JS(使用SJCL库)。 我不知道如何实现下面的代码
aes = new sjcl.cipher.aes( this.key );
bits = sjcl.codec.utf8String.toBits( text );
cipher = sjcl.mode.ccm.encrypt( aes, bits, iv );
cipherIV = sjcl.bitArray.concat( iv, cipher );
return sjcl.codec.base64.fromBits( cipherIV );
我的问题不是加密,而是库处理fromBits转换的方式。根据SJCL文件:
Most of our crypto primitives operate on arrays of 4-byte words internally, but many of them can take arguments that are not a multiple of 4 bytes. This library encodes arrays of bits (whose size need not be a multiple of 8 bits) as arrays of 32-bit words. The bits are packed, big-endian, into an array of words, 32 bits at a time. Since the words are double-precision floating point numbers, they fit some extra data. We use this (in a private, possibly-changing manner) to encode the number of bits actually present in the last word of the array.
对我来说,这似乎意味着转换为位数组会增加一些额外的信息,我担心这些信息在concat操作期间会普遍存在。另外,在concat之后,结果将作为base64字符串返回。我不确定正确的'struct'包装参数来复制这个。在
如果你仔细观察这段代码,你会发现它是自包含的。运行此代码后,不需要SJCL的“bits”(本机二进制数据表示)。这些内部数据被赋予加密和连接函数,然后将结果转换回“正常”的Base64编码字符串,该字符串是可移植的。在
此代码可能存在的唯一问题是}的编码。在
this.key
和{PyCrypto没有特殊的内部二进制数据编码,因为Python语言已经为我们提供了二进制字符串或
bytes
(取决于Python版本)。但您仍然需要使用Base64编码对字符串进行编码/解码。在相关问题 更多 >
编程相关推荐