0

I'm trying to decrypt message with 128 key with following code. This is an extension for String:

func aesDecrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? {
    if let keyData = key.dataUsingEncoding(NSUTF8StringEncoding),
        data = NSData(base64EncodedString: self, options: .IgnoreUnknownCharacters),
        cryptData    = NSMutableData(length: Int((data.length)) + kCCBlockSizeAES128) {

        let keyLength              = size_t(kCCKeySizeAES128)
        let operation: CCOperation = UInt32(kCCDecrypt)
        let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmAES128)
        let options:   CCOptions   = UInt32(options)

        var numBytesEncrypted :size_t = 0

        let cryptStatus = CCCrypt(operation,
                                  algoritm,
                                  options,
                                  keyData.bytes, keyLength,
                                  nil,
                                  data.bytes, data.length,
                                  cryptData.mutableBytes, cryptData.length,
                                  &numBytesEncrypted)

        if UInt32(cryptStatus) == UInt32(kCCSuccess) {
            cryptData.length = Int(numBytesEncrypted)
            let unencryptedMessage = String(data: cryptData, encoding:NSUTF8StringEncoding)
            return unencryptedMessage
        }
        else {
            return nil
        }
    }
    return nil
}

For input vector (iv) I use nil value. There is crypData is exist but I can't read this properly and unencryptedMessage is nil as well. Online tools notifies that data is incorrect, but on backend-side it works fine.

Key-value and message-value are base64Url.

Usage:

let decryptedMessage = message.aesDecrypt(keyTodecrypt, iv: nil)

Swift 2.3

Anton Kashpor
  • 1,255
  • 2
  • 18
  • 34
  • There is no standard settings or format for AES encryption. Unless you can describe exactly how the encryptor is constructing the message, you can't decrypt it. Do you have the encrypting code? – Rob Napier Apr 17 '17 at 14:51
  • You note that the key is Base64 encoded (at least I assume that's what "base64Url" means). But you're decoding it as utf-8. That's likely your primary problem, though there could certainly be more. – Rob Napier Apr 17 '17 at 14:53
  • @zaph I mean that input value is correct and Backend able to decrypt it with java libraries. And the is ECB mode, not CBC. – Anton Kashpor Apr 17 '17 at 15:24
  • @RobNapier Yes, You are right, I want to get utf-8. The encrypted message is json. – Anton Kashpor Apr 17 '17 at 15:27
  • 1
    @AntonKashpor I'm talking about the *key*. You're decoding it this way: `keyData = key.dataUsingEncoding(NSUTF8StringEncoding)`. That decodes the key as UTF-8, not as Base-64. – Rob Napier Apr 17 '17 at 20:57
  • @RobNapier sure, I need to convert to NSData. This is the most common way to achieve this. Isn't right? – Anton Kashpor Apr 18 '17 at 07:57
  • 1
    Only if the data is encoded in utf-8. If you're decoding Base64, then you want `Data.init(base64Encoded:options:)`. There is no "common" way to convert an arbitrary string into data. You have to decode using the system it's encoded with. – Rob Napier Apr 18 '17 at 12:12

1 Answers1

0

As Rob said, the main issue was input data. So, I have converted message and key to hex-value. If you have the same trouble, make sure that your value on client side and backend side has the same encoding parameter. For me it was UTF-8. Also, you should check key length.

Anton Kashpor
  • 1,255
  • 2
  • 18
  • 34