DES/ECB/NoPadding 在 swift 3.1 中使用 Common Crypto

DES/ECB/NoPadding in swift 3.1 using Common Crypto

我想在我的 iOS 应用程序中使用 DES 加密和解密字符串(是的,我知道 DES 不再安全,但应用程序接收消息并将消息发送到使用它的 BLE 模块)。 我成功地为 Common Crypto 创建了一个桥接 header 并且我发现它用于加密并将其改编为 DES 和 swift 3 :

func testCrypt(data:NSData, keyData:NSData, ivData:NSData, operation:CCOperation) -> NSData? {

    let keyBytes = keyData.bytes.assumingMemoryBound(to: UInt8.self)
    print("keyLength   = \(keyData.length), keyData   = \(keyData)")

    let ivBytes = ivData.bytes.assumingMemoryBound(to: UInt8.self)
    print("ivLength    = \(ivData.length), ivData    = \(ivData)")

    let dataLength = Int(data.length)

    let dataBytes = data.bytes.assumingMemoryBound(to: UInt8.self)
    print("dataLength  = \(dataLength), data      = \(data)")

    let cryptData: NSMutableData! = NSMutableData(length: Int(dataLength) + kCCBlockSizeDES)

    let cryptPointer = cryptData.mutableBytes.assumingMemoryBound(to: UInt8.self)
    let cryptLength  = size_t(cryptData.length)

    let keyLength              = size_t(kCCKeySizeDES)
    let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmDES)
    let options:   CCOptions   = UInt32(kCCOptionECBMode)

    var numBytesEncrypted :size_t = 0

    let cryptStatus = CCCrypt(operation,
                              algoritm,
                              options,
                              keyBytes, keyLength,
                              ivBytes,
                              dataBytes, dataLength,
                              cryptPointer, cryptLength,
                              &numBytesEncrypted)

    return cryptData;
}

函数调用和参数:

override func viewDidLoad() {
    super.viewDidLoad()

    let ivString = "Use a random iv!"
    let ivData = (ivString as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!

    let key = Data.init(bytes: [0x23, 0x12, 0x4d, 0x89, 0x88, 0x34, 0xCf, 0x50])
    let keyData = NSData.init(data: key)

    let message = "Don´t try to read this text. Top Secret Stuff"
    let data = (message as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!

    print("data: \(data)")
    if let encryptedData = testCrypt(data: data!, keyData:keyData, ivData: ivData!, operation:UInt32(kCCEncrypt)) {
        print("encryptedData: \(encryptedData)")
        if let decryptedData = testCrypt(data: encryptedData, keyData:keyData, ivData: ivData!, operation:UInt32(kCCDecrypt)) {
            let dec = Data.init(referencing: decryptedData)
            print("decryptedData: \(decryptedData))")
        }
    }
}

预期结果=<446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666>

加密数据:<6081ada9 0487c0a5 35e542bd e9600861 4250a693 65573337 39df5525 66c40cd8 b358bf6a d9837f9c 00000000 00000000 00000000>=[050]

decryptedData = <446f6ec2 B4742074 72792074 6F207265 61642074 68697320 74657874 2E20546F 7020546F 70205365 63726574 E0456E.100400 00 00 00 00 00 00 00 00乘

为什么解密returns只有部分正确的结果?

所以似乎没有可用的 NoPadding 选项,最后我只是从 'cryptData' 初始化中删除了 '+ kCCBlockSizeDES' 并在我的字符串中添加了 '0' 个字符,然后加密它以达到正确的大小(例如长度为 8 的倍数),然后在解密后我只是删除了零以获得原始字符串。