Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
String+AES.swift
import Foundation
import CryptoSwift
extension String {
func aesEncrypt(key: String, iv: String) throws -> String{
let data = self.dataUsingEncoding(NSUTF8StringEncoding)
let enc = try AES(key: key, iv: iv, blockMode:.CBC).encrypt(data!.arrayOfBytes(), padding: PKCS7())
let encData = NSData(bytes: enc, length: Int(enc.count))
let base64String: String = encData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0));
let result = String(base64String)
return result
}
func aesDecrypt(key: String, iv: String) throws -> String {
let data = NSData(base64EncodedString: self, options: NSDataBase64DecodingOptions(rawValue: 0))
let dec = try AES(key: key, iv: iv, blockMode:.CBC).decrypt(data!.arrayOfBytes(), padding: PKCS7())
let decData = NSData(bytes: dec, length: Int(dec.count))
let result = NSString(data: decData, encoding: NSUTF8StringEncoding)
return String(result!)
}
}
@seonar22
Copy link
Author

seonar22 commented Nov 4, 2021

extension String {
    func cryptoSwiftAESEncrypt(key: String, iv: String ) -> String? {
        do {
            let dec = try AES(key: key, iv: iv, padding: .pkcs7).encrypt(Array(self.utf8))
            let decData = Data(bytes: dec, count: Int(dec.count)).base64EncodedString(options: .lineLength64Characters)
            return decData
        } catch {
            print(error)
            return nil
        }
        
    }
    
    func cryptoSwiftAESDecrypt(key: String, iv: String) -> String? {
        do {
            let data = Data(base64Encoded: self)!
            let dec = try AES(key: key, iv: iv, padding: .pkcs7).decrypt(data.bytes)
            
            print("dec ok")
            let decData = Data(bytes: dec, count: Int(dec.count))
            
            print("decData ok")
            return String(data: decData, encoding: .utf8)
        } catch {
            print(error)
            return nil
        }
    }
    
    func cryptoSwiftAESEncryptGCM(key: String, iv: String) -> (String?) {
        do {
            let gcm = GCM(iv: Array(iv.utf8), mode: .combined)
            
            let dec = try AES(key: Array(key.utf8), blockMode: gcm, padding: .pkcs7).encrypt(Array(self.utf8))
            let decData = dec.toHexString()
            let tag = gcm.authenticationTag?.toBase64()
            
            return (decData)
        } catch {
            print(error)
            return (nil)
        }
        
    }
    
    func cryptoSwiftAESDecryptGCM(key: String, iv: String) -> String? {
        do {
            let gcm = GCM(iv: Array(iv.utf8), mode: .combined)
            let aes = try AES(key: Array(key.utf8), blockMode: gcm, padding: .pkcs7)
            let data = Data(hex: self)
            
            let dec = try aes.decrypt(data.bytes)
            return String(bytes: dec, encoding: .utf8)
        } catch {
            print(error)
            return nil
        }
    }
    
    func cryptoSwiftAESEncryptAEADPoly1305(key: String, iv: String) -> (String?, String?) {
        do {
            let encrypt = try AEADChaCha20Poly1305.encrypt(Array(self.utf8), key: Array(key.utf8), iv: Array(iv.utf8), authenticationHeader: Array((key + iv).utf8))
            
            return(encrypt.cipherText.toBase64(), encrypt.authenticationTag.toBase64())
            
        } catch {
            print("Unexpected error: \(error).")
            return (nil, nil)
        }
    }
    
    func cryptoSwiftAESDecryptAEADPoly1305(key: String, iv: String, tag: String) -> String? {
        
        do {
            let cipherData = Data(base64Encoded: self)!
            let tagData = Data(base64Encoded: tag)!

            let decrypt = try AEADChaCha20Poly1305.decrypt(cipherData.bytes, key: Array(key.utf8), iv: Array(iv.utf8), authenticationHeader: Array((key + iv).utf8), authenticationTag: tagData.bytes)
            assert(decrypt.success)

            return String(bytes: decrypt.plainText, encoding: .utf8)
        } catch {
            print("Unexpected error: \(error).")
            return nil
        }
    }
    
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment