Kitura / BlueCryptor

Swift cross-platform crypto library using CommonCrypto/libcrypto
Apache License 2.0
190 stars 47 forks source link

Invalid Triple-DES encryption result #75

Open MohamedT-Silverkey opened 2 years ago

MohamedT-Silverkey commented 2 years ago

Hi everyone,

I'm going to implement the Triple-DES to encrypt a password and I'm using the following command to validate the encryption output. Screen Shot 2022-06-01 at 3 47 48 PM

but I got a different encryption result.

Here's my code

func tripleDesEncrypt(inputStr: String, keyStr: String) -> String? {
        let key = CryptoUtils.byteArray(fromHex: keyStr)
        let iv = CryptoUtils.byteArray(fromHex: "0000000000000000")
        let plainText = CryptoUtils.byteArray(fromHex: inputStr)

        var textToCipher = plainText
        if plainText.count % Cryptor.Algorithm.tripleDes.blockSize != 0 {
            textToCipher = CryptoUtils.zeroPad(byteArray: plainText, blockSize: Cryptor.Algorithm.tripleDes.blockSize)
        }
        do {
            let cipherText = try Cryptor(operation: .encrypt, algorithm: .tripleDes, options: .pkcs7Padding, key: key, iv: iv).update(byteArray: textToCipher)?.final()

            print(CryptoUtils.hexString(from: cipherText!))

        } catch let error {
            guard let err = error as? CryptorError else {
                return nil
            }

            print(err.description)
        }

        return nil
    }

Input inputStr: 0592389EDCBA96FF keyStr: 0123456789abcdeffedcba9876543210

Output d9f8e02413307c829b81df2a39d8c603

The right output should be a25fbc3a3ed409102e24eeb85aef49ae

Please advise.