Я успешно RSA-Encrypting
строка, но когда я делаю это на iPhone 4s (iOS 9.3.2), она завершается с ошибкой и возвращает 'nil'
в результате. Однако он успешно работает на всех других iPhone (5,6,7,8, X.)
Я использую этот открытый ключ RSA:
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQClTlHEResIvOPHR0 + o4exJVEI5RQ4NnBBXV9tdoCbqavSgsiuFtZWn5RUVTLb0h7ULpOh8GDcu0yI4lnpMVDZ5U2w0ra2 / BNl6XDt9bwwoOh5w2lsdVmdP94t / qVBX4C0OcXw + RdSD1pshucTO7m2YLxtzLuc4ChUwjWZXVEoHdQIDAQAB"
На самом деле именно в этой строке кода я получаю & keyRef 'nil'
err = SecItemCopyMatching(dictionary as CFDictionary, &keyRef);
Вот мой код;
func encryptString(stringToEncrypt:String) -> String {
print("stringToEncrypt64 = " + stringToEncrypt)
let keyData = NSData(base64Encoded: Constants.RSA_Public_Key, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
let dictionary: [NSString: AnyObject] = [
kSecClass: kSecClassKey,
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeyClass: kSecAttrKeyClassPublic,
kSecAttrApplicationTag: "HBLMobilePublicKeyTag" as AnyObject,
kSecValueData: keyData!,
kSecAttrKeySizeInBits: NSNumber(value: 1024),
kSecReturnRef: true as AnyObject
];
var err = SecItemAdd(dictionary as CFDictionary, nil);
if ((err != noErr) && (err != errSecDuplicateItem)) {
print("error loading public key");
}
var keyRef: AnyObject?;
var base64String: String?
err = SecItemCopyMatching(dictionary as CFDictionary, &keyRef);
if (err == noErr) {
if let keyRef = keyRef as! SecKey? {
let plaintextLen = stringToEncrypt.lengthOfBytes(using: String.Encoding.utf8);
let plaintextBytes = [UInt8](stringToEncrypt.utf8);
var encryptedLen: Int = SecKeyGetBlockSize(keyRef);
var encryptedBytes = [UInt8](repeating: 0, count: encryptedLen);
err = SecKeyEncrypt(keyRef, SecPadding.PKCS1, plaintextBytes, plaintextLen, &encryptedBytes, &encryptedLen);
let data = NSData(bytes: encryptedBytes, length: encryptedBytes.count)
base64String = data.base64EncodedString(options: [])
}
}
SecItemDelete(dictionary as CFDictionary);
return base64String!
}