AES128 encryption in swift

旧街凉风 提交于 2019-12-03 08:56:09

Create string extension and use library CryptoSwift

//  String+Addition.swift
import CryptoSwift
extension String {
func aesEncrypt(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
    }
}

and to use

@IBAction func onBtnClicked(_ sender: Any) { 
        let value = "My value to be encrypted"
        let key = "MySixteenCharKey"

        print(key!)
        let encryptedValue = try! value.aesEncrypt(key: key!)

        print(encryptedValue)

    }

For citation of this particular android code: 1) The 16 char length key implies AES-128 2) The code is without iVector, This implies ECB mode 3) Using padding as pkcs5 or pkcs7 did not made any difference in my case

Create an extension of String and use this below function for encrypting and decrypting.

extension String {

//MARK: - Encrypt AES Method
/// This method will encrypt and return Cipher string

func aesEncrypt(key: String = "fqJfdzGDvfwbedsKSUGty3VZ9taXxMVw", iv: String = "1234567890123456") -> String {
    if let data = self.data(using: String.Encoding.utf8) {
        do {
            let enc = try AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt(data.bytes)
            let encData = Data(bytes: enc, count: Int(enc.count))
            let base64String: String = encData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0));
            return base64String
        }
        catch let error {
            print(error.localizedDescription)
            return ""
        }
    }
    return ""
}

//MARK: - Decrypt AES Method
/// This method will decrypt the Cipher string

func aesDecrypt(key: String = "fqJfdzGDvfwbedsKSUGty3VZ9taXxMVw", iv: String = "1234567890123456") -> String {
    if let data = Data(base64Encoded: self, options: Data.Base64DecodingOptions.init(rawValue: 0)) {
        do {
            let dec = try AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt(data.bytes)
            let decData = Data(bytes: dec, count: Int(dec.count))
            let result = String(data: decData, encoding: .utf8)
            return result ?? ""
        }
        catch let error {
            print(error.localizedDescription)
            return ""
        }
    }

    return ""
    }
}

Please use below library for AES encryption and decryption. It is available for iOS, Android and Web as well.

Reference URL

For iOS

OS / Swift 3, 4

Add a bridging header. Apple documentation

#import "CryptLib.h"

let plainText = "this is my plain text"
let key = "your key"

let cryptLib = CryptLib()

let cipherText = cryptLib.encryptPlainTextRandomIV(withPlainText: plainText, key: key)
print("cipherText \(cipherText! as String)")

let decryptedString = cryptLib.decryptCipherTextRandomIV(withCipherText: cipherText, key: key)
print("decryptedString \(decryptedString! as String)")

Android

val plainText = "this is my plain text"
val key = "your key"

val cryptLib = CryptLib()

val cipherText = cryptLib.encryptPlainTextWithRandomIV(plainText, key)
println("cipherText $cipherText")

val decryptedString = cryptLib.decryptCipherTextWithRandomIV(cipherText, key)
println("decryptedString $decryptedString")

avascript / NodeJS / Web

Download the library

npm install @skavinvarnan/cryptlib --save

const plainText = "this is my plain text";
const key = "your key";

const cryptLib = require('@skavinvarnan/cryptlib');

const cipherText = cryptLib.encryptPlainTextWithRandomIV(plainText, key);
console.log('cipherText %s', cipherText);

const decryptedString = cryptLib.decryptCipherTextWithRandomIV(cipherText, key);
console.log('decryptedString %s', decryptedString);

You will get same encryption and decryption result for all plateform ioS/Android/Web.

Try this:

func encryptValue(stringToEncrypt:String) -> String{

    var encryptedString: String = ""

    let value = "MySixteenCharKey"
    let input: Array<UInt8> = Array(stringToEncrypt.utf8)
    let key: Array<UInt8> = Array("MySixteenCharKey".utf8)

    do {
        let encrypted = try AES(key: key, blockMode: .ECB, padding: PKCS7()).encrypt(input)
        let base64 = encrypted.toBase64()
        encryptedString = base64
    } catch {
        print(error)
    }

    return encryptedString
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!