Saturday, 6 July 2019

AES256 Issue using swift 5

I am using AES256 algorithm CBC mode with pkc7 padding. I have beckend in node.js. but getting first 12 random characters. here is my swift code

    func encrypt(data: Data, key: Data, iv: Data) throws -> Data? {

        // Output buffer (with padding)
        let outputLength = data.count + kCCBlockSizeAES128

        var outputBuffer = Array<UInt8>(repeating: 0,
                                        count: outputLength)
//        var outputBuffer: [UInt8] = []
        var numBytesEncrypted = 0
        let status = CCCrypt(CCOperation(kCCEncrypt),
                             CCAlgorithm(kCCAlgorithmAES),
                             CCOptions(kCCOptionPKCS7Padding),
                             Array(key),
                             kCCKeySizeAES256,
                             Array(iv),
                             Array(data),
                             data.count,
                             &outputBuffer,
                             outputLength,
                             &numBytesEncrypted)
        guard status == kCCSuccess else {

            return nil
        }

        let outputBytes = iv + outputBuffer.prefix(numBytesEncrypted)

        return Data(bytes: outputBytes)
    }

How can I do without padding? or what should be done from beckend?

enter image description here



from AES256 Issue using swift 5

No comments:

Post a Comment