swift - 3DES 加密结果与示例不同

标签 swift encryption encoding character-encoding 3des

我有一个 3DES 加密示例,我必须遵循它才能在 NFC 卡上进行身份验证。这是示例:enter image description here

因此 51E764602678DF2B 变为 577293FD2F34CA51, key = 49454D4B41455242214E4143554F5946,IV = 0000000000000000 我成功地在这个网站上得到了正确的结果: http://tripledes.online-domain-tools.com/

我用 https://github.com/sgl0v/SCrypto 在 swift 上试过如下:

func testScrypto() {
    let plaintext = "51E764602678DF2B".data(using: String.Encoding.utf8)!
    let sharedSecretKey = "49454D4B41455242214E4143".data(using: String.Encoding.utf8)!
    let IV = "0000000000000000".data(using: String.Encoding.utf8)!

    let ciphertext = try! plaintext.encrypt(.tripleDES, options: .PKCS7Padding, key: sharedSecretKey, iv: IV)     

    let plaintext2 = try! ciphertext.decrypt(.tripleDES, options: .PKCS7Padding, key: sharedSecretKey, iv: IV)

    print("cipher = \(ciphertext.hexString())")
    print("plaintext2 = \(plaintext2.hexString())")
}

public extension Data {

func bytesArray<T: ExpressibleByIntegerLiteral>() -> [T] {
    var bytes = Array<T>(repeating: 0, count: self.count)
    (self as NSData).getBytes(&bytes, length:self.count * MemoryLayout<T>.size)
    return bytes
}

func hexString() -> String {
    let hexString = NSMutableString()
    let bytes: [UInt8] = self.bytesArray()
    for byte in bytes {
        hexString.appendFormat("%02x", UInt(byte))
    }
    return hexString as String
}
}

结果是:

cipher = d4c4a9637bcb4a435982330a42d1357b9e4539886a983535
plaintext2 = 35314537363436303236373844463242

35314537363436303236373844463242 是 51E764602678DF2B 如果我从十六进制字符串转换为明文但另一个字符串根本不是 577293FD2F34CA51

我也试过这个库https://www.example-code.com/swift/crypt2_3des.asp 但结果还是错误

我不知道是否有人知道如何在 swift 上执行此操作或者是加密方面的专家?

谢谢!

最佳答案

我成功解决了这个问题 问题是 key 只有 16 个字节,需要 24 个字节,所以我猜它是随机填充的,但预计它是第一个 8 个字节要放回到 16 个字节的末尾以便执行 24 个?

像这样工作:

func fillKey(keyLength: size_t, key: Data) -> Data {
    let missingBytes = keyLength - key.count
    if missingBytes > 0 {
        let keyBytes = (key as NSData).bytes
        var bytes = [UInt8](repeating: UInt8(0), count: keyLength)
        memccpy(&bytes[0], keyBytes.advanced(by: 0), Int32(key.count), key.count)
        memccpy(&bytes[key.count], keyBytes.advanced(by: 0), Int32(missingBytes), missingBytes)
        return Data(bytes: bytes)
    } else {
        return key
    }
}

func my3DESEncrypt(encryptData: String, key: String, iv: String) -> Data? {
    var myKeyData : Data = key.hexadecimal()!
    let myIvData : Data = iv.hexadecimal()!
    var myRawData : Data = encryptData.hexadecimal()!
    let buffer_size : size_t = myRawData.count + kCCBlockSize3DES
    var buffer = [UInt8](repeating: UInt8(0), count: buffer_size)
    var num_bytes_encrypted : size_t = 0

    let operation: CCOperation = UInt32(kCCEncrypt)
    let algoritm:  CCAlgorithm = UInt32(kCCAlgorithm3DES)
    let options:   CCOptions   = 0
    let keyLength        = size_t(kCCKeySize3DES)

    myKeyData = self.fillKey(keyLength: keyLength, key: myKeyData)
    let Crypto_status: CCCryptorStatus = CCCrypt(operation, algoritm, options, (myKeyData as NSData).bytes, keyLength, (myIvData as NSData).bytes, (myRawData as NSData).bytes, myRawData.count, &buffer, buffer_size, &num_bytes_encrypted)
    if UInt32(Crypto_status) == UInt32(kCCSuccess) {
        let data = Data(bytes: buffer, count: num_bytes_encrypted)
        return data
    } else{
        return nil
    }
}


func my3DESDecrypt(decryptData : Data, key: String, iv: String) -> Data? {
    let mydata_len : Int = decryptData.count
    var myKeyData : Data = key.hexadecimal()!
    let myIvData : Data = iv.hexadecimal()!

    let buffer_size : size_t = mydata_len+kCCBlockSize3DES
    var buffer = [UInt8](repeating: UInt8(0), count: buffer_size)
    var num_bytes_encrypted : size_t = 0

    let operation: CCOperation = UInt32(kCCDecrypt)
    let algoritm:  CCAlgorithm = UInt32(kCCAlgorithm3DES)
    let options:   CCOptions   = UInt32(kCCOptionPKCS7Padding)
    let keyLength        = size_t(kCCKeySize3DES)


    myKeyData = self.fillKey(keyLength: keyLength, key: myKeyData)
    let decrypt_status : CCCryptorStatus = CCCrypt(operation, algoritm, options, (myKeyData as NSData).bytes, keyLength, (myIvData as NSData).bytes, (decryptData as NSData).bytes, mydata_len, &buffer, buffer_size, &num_bytes_encrypted)

    if UInt32(decrypt_status) == UInt32(kCCSuccess){
        let data = Data(bytes: buffer, count: num_bytes_encrypted)
        return data
    } else{
        return nil

    }
}

关于swift - 3DES 加密结果与示例不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50802241/

相关文章:

ios - Swift:如何使用标签访问 UITextField 属性

algorithm - 为什么分支位移的 "start small"算法不是最优的?

xcode - 如何初始化相互依赖的属性

python - 如何将字符串加密成大小相同的字符串是Python

c# - 奇尔卡特 : How to decrypt an encrypted email?

c - 在不检查证书的情况下使用 OpenSSL,但我的数据是否仍在加密?

源码出现奇怪字符导致编译失败

ruby-on-rails - Ruby 1.8.7(或 Rails 2.x)中的 String.force_encoding()

ios - Realm 文件大小因保存相同数据而异

ios - Swift 中的回调函数语法