简体   繁体   中英

iOS AES 256 Decryption not working in swift

I have scenario in which some the data will come encrypted in JSON Response. So I tried some libraries to decryption in swift. But eventually I failed to do that.

So I tried this library WebCrypto : https://github.com/etienne-martin/WebCrypto.swift

Which works fine but the call is async and I need synchronous.

Earlier I tried these

  1. RNCryptor : https://github.com/RNCryptor/RNCryptor
  2. CryptoSwift : https://github.com/krzyzanowskim/CryptoSwift

RNCryptor: Code

    import RNCryptor

class DemoRNCryptor {

    class func run() {

        // Encryption

        let data = "my message is not ready".data(using: String.Encoding.utf8)!
        let password = "MySecretKey12345"
        let encryptedData = RNCryptor.encrypt(data: data, withPassword: password)
        print("RNCryptor enc:", encryptedData.hexString)


        // Decryption

        do {
            let originalData = try RNCryptor.decrypt(data: encryptedData, withPassword: password)
            let dd = String(data: originalData, encoding: .utf8)
            print("RNCryptor dec:", dd)
        } catch {
            print(error)
        }
    }
    }

CryptoSwift: Code

import CryptoSwift


    extension String {

    func aesEncryptCS(key: String) throws -> String {

        var result = ""

        do {

            let key: [UInt8] = Array(key.utf8) as [UInt8]
            let aes = try AES(key: key, blockMode: ECB(), padding: .pkcs5) // AES128 .ECB pkcs7
            let encrypted = try aes.encrypt(Array(self.utf8))

            result = encrypted.toBase64()!

            print("AES Encryption Result: \(result)")

        } catch {

            print("Error: \(error)")
        }

        return result
    }

    func aesDecryptCS(key: String) throws -> String {

        var result = ""

        do {

            let encrypted = self
            let key: [UInt8] = Array(key.utf8) as [UInt8]
            let aes = try AES(key: key, blockMode: ECB(), padding: .pkcs5) // AES128 .ECB pkcs7
            let decrypted = try aes.decrypt(Array(base64: encrypted))

            result = String(data: Data(decrypted), encoding: .utf8) ?? ""

            print("AES Decryption Result: \(result)")

        } catch {

            print("Error: \(error)")
        }

        return result
    }

}

I have a key = "Message" and Encrypted Text("my message is not ready") = " U2FsdGVkX187YbST7kZBjdhI6tGa2YQE5e3bS4WNhaXgi0iy//q5TOfR/q6Sc1Lx "

How I can decrypt this string using any of above two libraries?

I always get either nil or invalid key size.

"U2FsdGVkX187YbST7kZBjdhI6tGa2YQE5e3bS4WNhaXgi0iy//q5TOfR/q6Sc1Lx"

this is Base64 encoded data, and you need to decode it first, eg:

let encrypted = [UInt8](base64: "U2FsdGVkX187YbST7kZBjdhI6tGa2YQE5e3bS4WNhaXgi0iy//q5TOfR/q6Sc1Lx")

key = "Message"

assuming this is actually a key you use:

let key = "Message".bytes

so that gives us:

let key = "Message".bytes

let aes = try AES(key: key, blockMode: ECB(), padding: .pkcs5)
let encrypted = try aes.encrypt("some string".bytes))
let encryptedBase64 = encrypted.toBase64()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM