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

标签 ios swift encryption rsa

我正在使用 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/

相关文章:

ios - 无法使用 Push Sharp 发送 iOS MDM 推送通知

ios - iOS 和 Android 平台上的应用程序安全

ios - 如何使用未经身份验证的用户从 Swift 中的 AWS S3 存储桶下载文件

ios - 如何将底部边框添加到 TableView 部分标题

ios - 使用模型异步数据和 TableView

encryption - MD5 生成如何取决于文件大小?

ios - Google Plus API GPPSignIn 错误代码 "Mismatched authentication"

ios - Swift - 添加 Parse 后架构 x86_64 的 undefined symbol

java - Java中如何获取IV进行解密?

mysql - 如何在 MySQL 中散列密码?