根据应用程序的要求,我需要创建大小为 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 长度?
请帮忙。提前致谢。