The previous article introduced how the widget obtains database data. The IOS widget in the Flutter project accesses the flutter database file_Renxi's blog-CSDN blog
Here is the IOS part of the flutter project, using swift and importing the third-party library CryptoSwift .
Import third-party libraries, which require additional operations in order to be used in widgets
target 'Runner' do
use_frameworks!
use_modular_headers!
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
target '小组件的target name' do
use_frameworks!
use_modular_headers!
pod 'CryptoSwift', '~> 1.4.1'
# flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
Create AESUtil.swift under the widget file
//
// aesUtil.swift
// countWidgetExtension
//
// Created by Metajoy-Release-1 on 2023/6/15.
//
import Foundation
import CryptoSwift
class AESUtil{
let key = Array("你的密钥:32个字符".utf8)
let saltIv = Array("iv加的盐:16个字符".utf8)
func setAES(str: String)->String{
do {
//将传入的字符串转换成UTF8数组
let plaintext = Array(str.utf8)
//加密模式 CBC
let aes = try AES(key: key, blockMode: CBC(iv: saltIv), padding: .pkcs7)
//加密
let ciphertext = try aes.encrypt(plaintext)
//处理加密后的数据
print("Ciphertext: \(ciphertext.toBase64())")
//base64编码
return ciphertext.toBase64()
} catch {
// 处理错误
print("Encryption error: \(error)")
}
return ""
}
func getAES(str: String)->String{
do {
//将传来的字符串解码
guard let ciphertextData = Data(base64Encoded: str) else {
print("Invalid Base64 ciphertext")
return ""
}
//创建相同的参数
let aes = try AES(key: key, blockMode: CBC(iv: saltIv), padding: .pkcs7)
let ciphertext = try aes.decrypt(ciphertextData.bytes)
let decryptedText = String(data: Data(ciphertext), encoding: .utf8)
// 处理加密后的数据
print("decryptedText: \(decryptedText)")
return decryptedText ?? ""
} catch {
// 处理错误
print("Encryption error: \(error)")
}
return ""
}
}
All that's left is to call the encryption and decryption method after calling the database.