-
-
Save dayitv89/9018ec7f30844a5c575ff4afd33139c5 to your computer and use it in GitHub Desktop.
String+AES.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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