我使用 CryptoSwift 加密了一些数据,然后使用 Node.js 加密了相同的数据。但是结果不一样。问了作者,他说不是bug。
我不知道我哪里弄错了。以下是我如何使用 CryptoSwift 和 Node.js 的图片:
密码算法:aes-256-cfb
key: 32 bytes 1
iv: 16 字节 0
CryptoSwift:开发分支 0.1.1
Node .js:长期支持 4.2.3
Data encrypted by Node.js 4.2.3
这是快速代码:
func testAES() {
let key = [UInt8](count: 32, repeatedValue: 1)
let iv = [UInt8](count: 16, repeatedValue: 0)
print(key)
print(iv)
let aes256cfb = try! AES(key: key, iv: iv, blockMode: .CFB)
let en1 = try! aes256cfb.encrypt([0x5, 0x77], padding: nil)
print(en1.map({ i in String(format: "%2x", i)}))
let en2 = try! aes256cfb.encrypt([0x5, 0x0, 0x3, 0x89, 0x20], padding: nil)
print(en2.map({ i in String(format: "%2x", i)}))
}
CryptoSwift:
["77", "ef"]
["77", "98", "c9", "2c", "45"]
Node.js:
<Buffer 77 ef>
<Buffer cf a5 66 8a 3e>
可以看到,前两个字节是相同的,其余的则不同。为什么?我的代码写错了吗?我对加密不太了解,请告诉我原因。非常感谢。
最佳答案
回答这个问题。
您的 NodeJS 代码加密 [0x5, 0x77, 0x5, 0x0, 0x3, 0x89, 0x20],但您的 CryptoSwift 代码加密 [0x5, 0x77],然后加密 [0x5, 0x0, 0x3, 0x89, 0x20]。这就是您得到不同结果的原因。
关于node.js - CryptoSwift 加密的数据与 Node.js 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146316/