2

I am using "CryptoSwift 1.0.0" and Swift 5 with Xcode 10.2 for an iOS app, CryptoSwift encryption works fine, and also my PHP server Encryption and Decryption.

But I am getting this error: dataPaddingRequired when this method is getting executed:

let cipher = try aes.decrypt(Array<UInt8>(cryptedData))

this is my code:

Encryption function

static func cryptArtisan(strClair: String) -> [String:String] {
    let key = generateStringKey()
    var cryptedMessage = ""
    var cryptedKey = ""
    do {
        let aes = try AES(key: Array<UInt8>(key.utf8), blockMode: ECB(), padding: .pkcs5) // aes128

        let ciphertext = try aes.encrypt(Array(strClair.utf8))
        cryptedMessage = ciphertext.toBase64()!

        let publicKey = try PublicKey(derNamed: "public")
        let clear = try ClearMessage(string: key, using: .utf8)
        let encrypted = try clear.encrypted(with: publicKey, padding: .PKCS1)
        cryptedKey = encrypted.base64String
    } catch { }

    cryptedMessage = cryptedMessage.replacingOccurrences(of: "+", with: "%2B")
    cryptedKey = cryptedKey.replacingOccurrences(of: "+", with: "%2B")

    return ["msg":cryptedMessage, "key":cryptedKey, "clairKey": key]
}

Decryption function

static func decryptArtisan(cryptedMessage: String , key:String) -> String? {
    var clairMessage:String? = nil;

    if let cryptedData = Data(base64Encoded: cryptedMessage) {
        do {
            let aes = try AES(key: Array<UInt8>(key.utf8), blockMode: ECB(), padding: .pkcs5) // aes128
            let cipher = try aes.decrypt(Array<UInt8>(cryptedData))
            clairMessage = String(bytes: cipher, encoding: .utf8)
        }catch{
            print(error)
        }
    }

    return clairMessage
}

How can I decrypt the encrypted message sent by the server?

Update: I tried to not cast error to NSError, I noticed a dataPaddingRequired error was throw.

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Molax
  • 39
  • 1
  • 10
  • `key.utf8` is wrong. – Maarten Bodewes May 14 '19 at 15:41
  • I checked the key parameter, it is same of the key used for encryption & decryption. I tried to not cast error to NSError, I noticed a dataPaddingRequired error was throw. – Molax May 14 '19 at 16:46
  • 1
    Yes, but you don't base 64 decode it, you encode it as UTF-8 instead. Your encryption code shows base 64 encoding. – Maarten Bodewes May 14 '19 at 16:51
  • Ok I see, my parameter key looks like this characters `uyxXwpUD5CgYD=7L` and AES constructor just accept UTF-8 parameter, could you tell me what am I doing wrong here please? – Molax May 14 '19 at 20:40
  • Sorry, I'm trying to read your crypto code *again*, there are some very weird decisions being made, if just because of the variable naming. Just a second. – Maarten Bodewes May 14 '19 at 20:59
  • Ah, `cryptKey` is the RSA encrypted AES key, which is send using base 64. However, I don't see you try and decrypt the AES key at all, you would need 1. base64 decoding and 2. RSA / PKSC#1 decrypting using the correct private key. Where is your RSA decryption taking place? – Maarten Bodewes May 14 '19 at 21:04
  • I use the clairKey returned by `generateStringKey(_ length:Int = 16)` function for decryption sir – Molax May 14 '19 at 21:34
  • Yeah, OK, but is that the same key as used during encryption or is that *another*, newly generated string? Do you have a description on what the PHP server requires as input? There are many ways of encrypting using RSA / AES after all. – Maarten Bodewes May 14 '19 at 21:37
  • Yes, that the same key as used during encryption and decryption on iOS app. and I send only the cryptedMessage and cryptedKey to the server, and I make decryption like that : $key = base64_decode($key); $key = $this->get("artisan.crypto")->decryptRSA($key); And I get the same key used on the iOS app, i checked the result, it is same. so I don't send any key from the server, I use just the clearKey generated on my iOS app.I would be grateful for any help you are able to provide. – Molax May 14 '19 at 22:23
  • OK, these are all things that we could not see in the original question (try and create an MCVE next time). I guess then that the most likely culprit is `Array(key.utf8)` conversion, but I don't know enough about Swift for that. – Maarten Bodewes May 14 '19 at 22:44
  • Ok, thank you for your time, I appreciate that – Molax May 15 '19 at 00:48
  • Sorry, my bad, the bug is not from the iOS app, but it is from my PHP encryption function... I apologize for the inconvenience. – Molax May 16 '19 at 04:52
  • Welcome to crypto :) Try and make your methods as balanced as possible next time. If your decryption method performs a different operation or is much smaller then there is probably something wrong. And again, post all the code required to replicate the issue! – Maarten Bodewes May 16 '19 at 09:43
  • Ok, Thanks for your advices, I appreciate it – Molax May 16 '19 at 16:57

0 Answers0