我尝试用
exponent:
let exponent = "10001"
modulus:
let modulus = "D6250B831F82EC984513922E797283E4D3879E1F0AD52364EBDA5A5696F6E75CDCE0704A993F3F95AA557A6882A525EC4B8344DA3E7DFDECCBACCEF18131E461D3C5D3D7E1334C6AE27E5CDEF8A577857542BCBEF6CF021B0EE5604534E6C6CBAEFA6EFFC1AB93DEE7CE51A8C8F2B7345680BDF840841C3A6F654CD1F10BA2FD5CA1C6E782A8FAEC79BD22FA12116D75FFAEDB2DEC151E0B60DB91F2E74BA78EFBB45DF739AF9CDD41C482DC22FC76E03C8E2141BDAE5406C0DA230E2C7EFFC68C8811E1544496332B03BCFF0F627A8DF51D2E2B32B0771D1C6F87AD56010DCB7A3862C63B88B2CF7D7AD40CC53AF0CFEC0820777C9CCE95A58848D67779AE8D"
作为 publicKey 在 Swift 中加密文本。有人能帮我吗?
这是我的代码:
import Foundation
import Security
class Encryption {
var publicKeyPtr, privateKeyPtr: Unmanaged<SecKey>?
var publicKey, privateKey: SecKey?
let parameters: [String:String] = [kSecAttrKeyType: kSecAttrKeyTypeRSA, kSecAttrKeySizeInBits: "2048"]
init(){
}
func genKey() {
let status = SecKeyGeneratePair(parameters, &publicKeyPtr, &privateKeyPtr)
publicKey = publicKeyPtr!.takeRetainedValue()
privateKey = privateKeyPtr!.takeRetainedValue()
}
func encrypt(plainText: String, publicKey: SecKey) -> [UInt8]{
let blockSize = SecKeyGetBlockSize(publicKey)
let plainTextData = [UInt8](plainText.utf8)
let plainTextDataLength = UInt(countElements( plainText))
var encryptedData = [UInt8](count: Int(blockSize), repeatedValue: 0)
var encryptedDataLength = blockSize
let result = SecKeyEncrypt(publicKey, SecPadding(kSecPaddingPKCS1),
plainTextData, plainTextDataLength, &encryptedData, &encryptedDataLength)
return encryptedData
}
func decrypt(data: [UInt8]) -> String{
let blockSize = SecKeyGetBlockSize(publicKey)
var decryptedData = [UInt8](count: Int(blockSize), repeatedValue: 0)
var decryptedDataLength = blockSize
let result = SecKeyDecrypt(privateKey, SecPadding(kSecPaddingPKCS1),
data, blockSize,
&decryptedData, &decryptedDataLength)
let decryptedText = String(bytes: decryptedData,
encoding:NSUTF8StringEncoding)
return decryptedText!
}
}