Skip to content

Invalid Triple-DES encryption result #75

@MohamedT-Silverkey

Description

@MohamedT-Silverkey

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions