gpt4 book ai didi

swift - cccrypt 在 swift 4 中生成奇怪的输出

转载 作者:行者123 更新时间:2023-11-28 11:56:49 28 4
gpt4 key购买 nike

我尝试编写一个基于 CCCrypt 的加密 (AESCBC256) 函数,而 CCCrypt 正在生成一个随机值。

例如,当我将 1234567 传递给函数时,对于相同的 iv 和 key ,它将返回“HlFP2rCmycZS1269Cm47Q==”或“TTuSJrBcsOmOCDHc5IQ8Dw==”。

这是 iv: b5f89591关键是:366e9c1b4b2ed2b1daf751d7500aaa01

func encrypt(Value: String)->String{
let keyData = keyString.data(using: .utf8, allowLossyConversion: false)!
let iv = SecretKey.data(using: .utf8, allowLossyConversion: false)!
let message = SecretKey+Value
let data = message.data(using: .utf8, allowLossyConversion: false)!
let cryptData = NSMutableData(length: Int(data.count) + kCCBlockSizeAES128)!
let keyLength = size_t(kCCKeySizeAES256)
let operation: CCOperation = CCOperation(UInt32(kCCEncrypt))
let algoritm: CCAlgorithm = CCAlgorithm(UInt32(kCCAlgorithmAES128))
let options: CCOptions = CCOptions(UInt32(kCCOptionPKCS7Padding))
var numBytesEncrypted :size_t = 0
let cryptStatus = CCCrypt(operation,
algoritm,
options,
keyData.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> UnsafePointer<UInt8> in return bytes},
keyLength,
iv.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> UnsafePointer<UInt8> in return bytes},
data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> UnsafePointer<UInt8> in return bytes},
data.count,
cryptData.mutableBytes, cryptData.length,
&numBytesEncrypted)

if UInt32(cryptStatus) == UInt32(kCCSuccess) {
cryptData.length = Int(numBytesEncrypted)
return String(describing: cryptData.base64EncodedString(options: .lineLength64Characters))
}else{
return ""
}
}

最佳答案

您的 IV 长度错误。它有 8 个字节长,但 AES 需要一个 16 字节的 IV,因此它从内存中读取随机数据以获取其他 8 个字节。

关于swift - cccrypt 在 swift 4 中生成奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50801091/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com