Last active
January 5, 2022 15:59
-
-
Save tlarevo/63840cdd421937ba9174 to your computer and use it in GitHub Desktop.
PHP compatible AES Encryption and Decryption with Swift and CryptoSwift
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
// Credit should be given to; | |
// https://gist.github.com/yutelin/f4f66e0c78474db1de51#file-string-aes-swift | |
// https://gist.github.com/bradbernard/2a7af4c2200cb3794768#file-swift-encryption | |
// And most importantly to https://github.com/krzyzanowskim/CryptoSwift | |
import Foundation | |
import CryptoSwift | |
extension String { | |
func aesEncryptWithStaticIV(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 aesDecryptWithStaticIV(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!) | |
} | |
func aesEncryptWithoutPaddingWithStaticIV(key: String, iv: String) -> String { | |
let data = self.dataUsingEncoding(NSUTF8StringEncoding) | |
let enc = AES(key: key, iv: iv, blockMode:.CBC)?.encrypt(data!.arrayOfBytes()) | |
let encData = NSData(bytes: enc!, length: Int(enc!.count)) | |
let base64String: String = encData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)); | |
let result = String(base64String) | |
return result | |
} | |
func aesDecryptWithoutPaddingWithStaticIV(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()) | |
let decData = NSData(bytes: dec!, length: Int(dec!.count)) | |
var result = NSString(data: decData, encoding: NSUTF8StringEncoding) | |
return String(result!) | |
} | |
func aesEncrypt(key: String) -> String { | |
let data = self.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) | |
let keyData = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!.arrayOfBytes() | |
let ivData = Cipher.randomIV(keyData.count) | |
let encrypted = Cipher.AES(key: keyData, iv: ivData, blockMode:.CBC).encrypt(data!.arrayOfBytes()) | |
let encryptedData = NSData(bytes: encrypted!, length: Int(encrypted!.count)) | |
let sendData = NSMutableData(bytes: ivData, length: Int(ivData.count)) | |
sendData.appendData(encryptedData) | |
let base64String: String = sendData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)); | |
let result = String(base64String) | |
return result | |
} | |
func aesDecrypt(key: String) -> String { | |
let data = NSData(base64EncodedString: self, options: NSDataBase64DecodingOptions(rawValue: 0)) | |
let keyData = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!.arrayOfBytes() | |
let ivData = data!.subdataWithRange(NSRange(location: 0, length: AES.blockSize)).arrayOfBytes() | |
let encryptedData = data!.subdataWithRange(NSRange(location: AES.blockSize, length: (data!.length - AES.blockSize) as Int)) | |
let decrypted = Cipher.AES(key: keyData, iv: ivData, blockMode:.CBC).decrypt(encryptedData.arrayOfBytes()) | |
let decryptedData = NSData(bytes: decrypted!, length: Int(decrypted!.count)) | |
let decryptedString = NSString(data: decryptedData, encoding: NSUTF8StringEncoding)! | |
return String(decryptedString) | |
} | |
} |
Could anyone modify this file to adapt swift 3? thx
can you give me an example of file encryption and decryption with swift 3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@boardmain, @tnavadiya, to fix this error you should replace these two lines:
by these:
Cheers!