Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid Triple-DES encryption result #75

Open
MohamedT-Silverkey opened this issue Jun 1, 2022 · 0 comments
Open

Invalid Triple-DES encryption result #75

MohamedT-Silverkey opened this issue Jun 1, 2022 · 0 comments

Comments

@MohamedT-Silverkey
Copy link

MohamedT-Silverkey commented Jun 1, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant