在某些情况下,由于解密的字符串变为空,因此将通用加密AESEncryption迁移到CryptoSWIFT [英] Migrating Common Crypto AESEncryption to CryptoSWIFT as decrypted string turns null in some cases
问题描述
我使用以下代码通过AES加密对字符串进行加密和解密.我通过添加桥接头来使用普通加密.
Im using the following code to encrypt and decrypt a string using AES Encryption.I'm using common crypto by adding a Bridging Header.
extension String {
func aesEncrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? {
if let keyData = key.data(using: String.Encoding.utf8),
let data = self.data(using: String.Encoding.utf8),
let cryptData = NSMutableData(length: Int((data.count)) + kCCBlockSizeAES128) {
let keyLength = size_t(kCCKeySizeAES128)
let operation: CCOperation = UInt32(kCCEncrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128)
let options: CCOptions = UInt32(options)
var numBytesEncrypted :size_t = 0
let cryptStatus = CCCrypt(operation,
algoritm,
options,
(keyData as NSData).bytes, keyLength,
iv,
(data as NSData).bytes, data.count,
cryptData.mutableBytes, cryptData.length,
&numBytesEncrypted)
if UInt32(cryptStatus) == UInt32(kCCSuccess) {
cryptData.length = Int(numBytesEncrypted)
let base64cryptString = cryptData.base64EncodedString(options: .lineLength64Characters)
return base64cryptString
}
else {
return nil
}
}
return nil
}
func aesDecrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? {
if let keyData = key.data(using: String.Encoding.utf8),
let data = NSData(base64Encoded: self, options: .ignoreUnknownCharacters),
let cryptData = NSMutableData(length: Int((data.length)) + kCCBlockSizeAES128) {
let keyLength = size_t(kCCKeySizeAES128)
let operation: CCOperation = UInt32(kCCDecrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128)
let options: CCOptions = UInt32(options)
var numBytesEncrypted :size_t = 0
let cryptStatus = CCCrypt(operation,
algoritm,
options,
(keyData as NSData).bytes, keyLength,
iv,
data.bytes, data.length,
cryptData.mutableBytes, cryptData.length,
&numBytesEncrypted)
if UInt32(cryptStatus) == UInt32(kCCSuccess) {
cryptData.length = Int(numBytesEncrypted)
let unencryptedMessage = String(data: cryptData as Data, encoding:String.Encoding.utf8)
return unencryptedMessage
}
else {
return nil
}
}
return nil
}
}
在某些情况下,解密后的字符串返回null,这会导致程序崩溃.我目前正在尝试用CryptoSwift替换它,但是我不知道如何设置加密方案以匹配这些设置.
In some cases the decrypted string returns null, this leads to program crash.Im currently trying to replace this with CryptoSwift,but I cannot figure out how to set the Encryption Scheme to match these settings.Please advice.
推荐答案
检查自述文件 https://github.com/krzyzanowskim/CryptoSwift#aes 在这里,您将找到可以直接用于您的需求的AES示例.例如.要加密数据,请执行以下操作:
Check the README https://github.com/krzyzanowskim/CryptoSwift#aes here you'll find the AES examples that should be straightforward to use for your needs. Eg. To encrypt data, you do this:
do {
let aes = try AES(key: "keykeykeykeykeyk", iv: "drowssapdrowssap") // aes128
let ciphertext = try aes.encrypt(data.bytes)
} catch { }
这篇关于在某些情况下,由于解密的字符串变为空,因此将通用加密AESEncryption迁移到CryptoSWIFT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!