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.