RSA公钥生成 - 字符中的密钥大小在iOS中是不同的

时间:2018-01-13 10:08:39

标签: ios swift rsa seckeyref

根据应用程序的要求,我需要创建大小为512的RSA公钥。我使用以下代码进行相同的操作:

func generatePublicKey() -> String
{
    var strKey : String = ""

    let publicKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true as NSObject, kSecAttrApplicationTag:"com.nichetech.DEMO.public".data(using: String.Encoding.utf8)! as NSObject]
    let privateKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true as NSObject, kSecAttrApplicationTag:"com.nichetech.DEMO.private".data(using: String.Encoding.utf8)! as NSObject]

    var keyPairAttr = [NSObject: NSObject]()
    keyPairAttr[kSecAttrKeyType] = kSecAttrKeyTypeRSA
    keyPairAttr[kSecAttrKeySizeInBits] = 512 as NSObject
    keyPairAttr[kSecPublicKeyAttrs] = publicKeyAttr as NSObject
    keyPairAttr[kSecPrivateKeyAttrs] = privateKeyAttr as NSObject

    statusCode = SecKeyGeneratePair(keyPairAttr as CFDictionary, &publicKey, &privateKey)
      var error:Unmanaged<CFError>?
    if let cfdata = SecKeyCopyExternalRepresentation(self.publicKey!, &error) {
        let data:Data = cfdata as Data
        //let b64Key = (data).base64EncodedString()

        let b64Key = (data).base64EncodedString(options:NSData.Base64EncodingOptions.lineLength76Characters)

        let strPublicKey = appendPrefixSuffixTo(b64Key, prefix: "-----BEGIN PUBLIC KEY-----\n", suffix: "\n-----END PUBLIC KEY-----")

        strKey = strPublicKey
    }


return strKey
}

我们在Android和.net上都有相同的功能。当我们计算所有其他平台中字符串的长度时,它有181个字符,空格和174个字符,没有空格。

但是,在iOS中,我们只获得了153个空格和146个无空格字符。

我们的代码有什么问题????或者iOS有一些不同的RSA长度?

请帮忙。 在此先感谢。

0 个答案:

没有答案