Search code examples
swiftencryptionencodingcharacter-encoding3des

3DES Encryption result different from example


I have an example for a 3DES encryption that I have to follow in order to authenticate on NFC cards. Here is the example:enter image description here

So 51E764602678DF2B becomes 577293FD2F34CA51 with key = 49454D4B41455242214E4143554F5946 and IV = 0000000000000000 I succeed to have the right result on this site: http://tripledes.online-domain-tools.com/

I tried on swift with https://github.com/sgl0v/SCrypto as follow:

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
}
}

and the result is:

cipher = d4c4a9637bcb4a435982330a42d1357b9e4539886a983535
plaintext2 = 35314537363436303236373844463242

35314537363436303236373844463242 is 51E764602678DF2B if i convert from hexstring to plaintext but the other string is not 577293FD2F34CA51 at all

I also tried this lib https://www.example-code.com/swift/crypt2_3des.asp but the result is still wrong

I don't know if anybody has an idea how to do this on swift or is an expert in encryption ?

thanks !


Solution

  • I succeed to fix the issue The issue was that the key was only 16 bytes and need to be 24, so I guess it was randomly filled but it was expected to be the first 8 bytes to be put back at the end of the 16 in order to do 24 ?

    like this it works:

    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
    
        }
    }