gpt4 book ai didi

ios - 使用来自服务器的字符串公钥进行 RSA 加密

转载 作者:行者123 更新时间:2023-11-30 10:53:36 27 4
gpt4 key购买 nike

我正在使用 RSA 加密字符串密码。但我正在使用的函数正在生成 SecKey 数据类型的公钥,这不是我需要的,因为我从服务器响应中以 String 形式获取公钥。我使用了很多库,但也相同,生成公钥 SecKey 。
下面的类(class)展示了我的意思我想将函数 func encryptBase64(text: String, secKey: SecKey) -> String { 更改为 func encryptBase64(text: String, publicKey: String) -> String {

类 RSAWrapper { private var publicKey : SecKey? private var privateKey : SecKey?

func generateKeyPair(keySize: UInt, privateTag: String, publicTag: String) -> Bool {

self.publicKey = nil
self.privateKey = nil


if (keySize != 512 && keySize != 1024 && keySize != 2048) {
// Failed
print("Key size is wrong")
return false
}
let publicKeyParameters: [NSString: AnyObject] = [
kSecAttrIsPermanent: true as AnyObject,
kSecAttrApplicationTag: publicTag as AnyObject
]
let privateKeyParameters: [NSString: AnyObject] = [
kSecAttrIsPermanent: true as AnyObject,
kSecAttrApplicationTag: publicTag as AnyObject
]
let parameters: [String: AnyObject] = [
kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits as String: keySize as AnyObject,
kSecPrivateKeyAttrs as String: privateKeyParameters as AnyObject,
kSecPublicKeyAttrs as String: publicKeyParameters as AnyObject
];

let status : OSStatus = SecKeyGeneratePair(parameters as CFDictionary, &(self.publicKey), &(self.privateKey))

return (status == errSecSuccess && self.publicKey != nil && self.privateKey != nil)
}

func encrypt(text: String) -> [UInt8] {
let plainBuffer = [UInt8](text.utf8)
var cipherBufferSize : Int = Int(SecKeyGetBlockSize((self.publicKey!)))
var cipherBuffer = [UInt8](repeating:0, count:Int(cipherBufferSize))

// Encrypto  should less than key length
let status = SecKeyEncrypt((self.publicKey)!, SecPadding.PKCS1, plainBuffer, plainBuffer.count, &cipherBuffer, &cipherBufferSize)
if (status != errSecSuccess) {
print("Failed Encryption")
}
return cipherBuffer
}

func decprypt(encrpted: [UInt8]) -> String? {
var plaintextBufferSize = Int(SecKeyGetBlockSize((self.privateKey)!))
var plaintextBuffer = [UInt8](repeating:0, count:Int(plaintextBufferSize))

let status = SecKeyDecrypt((self.privateKey)!, SecPadding.PKCS1, encrpted, plaintextBufferSize, &plaintextBuffer, &plaintextBufferSize)

if (status != errSecSuccess) {
print("Failed Decrypt")
return nil
}
return NSString(bytes: &plaintextBuffer, length: plaintextBufferSize, encoding: String.Encoding.utf8.rawValue)! as String
}


func encryptBase64(text: String, secKey: SecKey) -> String {
let plainBuffer = [UInt8](text.utf8)
var cipherBufferSize : Int = Int(SecKeyGetBlockSize((secKey)))
var cipherBuffer = [UInt8](repeating:0, count:Int(cipherBufferSize))

// Encrypto should less than key length
let status = SecKeyEncrypt((self.publicKey)!, SecPadding.PKCS1, plainBuffer, plainBuffer.count, &cipherBuffer, &cipherBufferSize)
if (status != errSecSuccess) {
print("Failed Encryption")
}

let mudata = NSData(bytes: &cipherBuffer, length: cipherBufferSize)
return mudata.base64EncodedString(options: NSData.Base64EncodingOptions.lineLength64Characters)
}

func decpryptBase64(encrpted: String) -> String? {

let data : NSData = NSData(base64Encoded: encrpted, options: .ignoreUnknownCharacters)!
let count = data.length / MemoryLayout<UInt8>.size
var array = [UInt8](repeating: 0, count: count)
data.getBytes(&array, length:count * MemoryLayout<UInt8>.size)

var plaintextBufferSize = Int(SecKeyGetBlockSize((self.privateKey)!))
var plaintextBuffer = [UInt8](repeating:0, count:Int(plaintextBufferSize))

let status = SecKeyDecrypt((self.privateKey)!, SecPadding.PKCS1, array, plaintextBufferSize, &plaintextBuffer, &plaintextBufferSize)

if (status != errSecSuccess) {
print("Failed Decrypt")
return nil
}
return NSString(bytes: &plaintextBuffer, length: plaintextBufferSize, encoding: String.Encoding.utf8.rawValue)! as String
}


func getPublicKey() -> SecKey? {
return self.publicKey
}

func getPrivateKey() -> SecKey? {
return self.privateKey
}

}

最佳答案

关键概念是:

服务器作为 key 对

  • 公钥
  • 私钥

应用程序也作为 key 对

  • 公钥
  • 私钥

因此服务器应该知道应用程序公钥,应用程序也应该知道服务器公钥。

当服务器想要对应用程序的某些有效负载进行加密时,服务器会使用应用程序公开进行加密并将其发送到应用程序。应用程序使用应用程序私钥解密有效负载。

当应用程序想要加密某些负载到服务器时,应用程序使用服务器公钥进行加密并将其发送到服务器。服务器使用服务器私钥解密有效负载。

这就是 key 对通信的工作方式。

就您而言,您应该只需要知道服务器公钥,因为您只需将内容发送到服务器,而不需要生成应用 key 对。

Check this link以便使用服务器公钥进行加密。

关于ios - 使用来自服务器的字符串公钥进行 RSA 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54200952/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com