Skip to content

Instantly share code, notes, and snippets.

@gauravds
Forked from yutelin/String+AES.swift
Created October 26, 2015 09:01
Show Gist options
  • Save gauravds/a2d9954d4d3b1f2d747c to your computer and use it in GitHub Desktop.
Save gauravds/a2d9954d4d3b1f2d747c to your computer and use it in GitHub Desktop.
String+AES.swift
import Foundation
import CryptoSwift
extension String {
func aesEncrypt(key: String, iv: String) -> String {
let data = self.dataUsingEncoding(NSUTF8StringEncoding)
let enc = 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) -> String {
let data = NSData(base64EncodedString: self, options: NSDataBase64DecodingOptions(rawValue: 0))
let dec = AES(key: key, iv: iv, blockMode:.CBC)?.decrypt(data!.arrayOfBytes(), padding: PKCS7())
let decData = NSData(bytes: dec!, length: Int(dec!.count))
var result = NSString(data: decData, encoding: NSUTF8StringEncoding)
return String(result!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment