Swift: generate the privatekey and publickey with "cannot open file" exception and "undefined error"

I noticed there are issues when generate the privateKey and publicKey on MacOS(already cleanup the keyAccess chain, using macOS Big Sur M1 Apple Sillicon) even though these are generated, but not perfect as you can see in the log:

Step 1: generate publicKey and privateKey!!!
2021-03-04 23:20:50.451195-0600 GenKeyHui[25447:1478275] [logging-persist] cannot open file at line 44580 of [02c344acea]
2021-03-04 23:20:50.456751-0600 GenKeyHui[25447:1478275] [logging-persist] os_unix.c:44580: (0) open(/var/db/DetachedSignatures) - Undefined error: 0
Key pair generated OK
Public Key: AgAAAIcZHKMPyRHUhJoABQK1ISICAAAAAAAAACoAAAAAAAAAAAgAACEAAAAJAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAALB1WwABAAAA
PublicKey without base64Encoding is :
96 bytes
Private Key: AgAAAIcZHKMPyRHUhJoABQK1ISICAAAAAAAAACoAAAABAAAAAAgAACEAAACGAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAACB2WwABAAAA

Code snippet:
import Foundation
import CommonCrypto
print("Step 1: generate publicKey and privateKey!!!")
//Provide tagPublic by whale
let tagPublic: String = "com.one.whale12.public"
let publicKeyAttr: [NSObject: NSObject] = [
kSecAttrIsPermanent:true as NSObject,
kSecAttrApplicationTag:tagPublic.data(using: String.Encoding.utf8)! as NSObject,
kSecClass: kSecClassKey, // added this value
kSecReturnData: kCFBooleanTrue] // added this value
let privateKeyAttr: [NSObject: NSObject] = [
kSecAttrIsPermanent:true as NSObject,
kSecAttrApplicationTag:"com.one.whale12.private".data(using: String.Encoding.utf8)! as NSObject,
kSecClass: kSecClassKey, // added this value
kSecReturnData: kCFBooleanTrue] // added this value
var keyPairAttr = NSObject: NSObject
keyPairAttr[kSecAttrKeyType] = kSecAttrKeyTypeRSA
keyPairAttr[kSecAttrKeySizeInBits] = 2048 as NSObject
keyPairAttr[kSecPublicKeyAttrs] = publicKeyAttr as NSObject
keyPairAttr[kSecPrivateKeyAttrs] = privateKeyAttr as NSObject
var publicKey : SecKey?
var privateKey : SecKey?;
let statusCode = SecKeyGeneratePair(keyPairAttr as CFDictionary, &publicKey, &privateKey)
if statusCode == noErr && publicKey != nil && privateKey != nil {
print("Key pair generated OK")
var resultPublicKey: AnyObject?
var resultPrivateKey: AnyObject?
let statusPublicKey = SecItemCopyMatching(publicKeyAttr as CFDictionary, &resultPublicKey)
let statusPrivateKey = SecItemCopyMatching(privateKeyAttr as CFDictionary, &resultPrivateKey)
if statusPublicKey == noErr {
if let publicKey = resultPublicKey as? Data {
print("Public Key: \((publicKey.base64EncodedString()))")
print("PublicKey without base64Encoding is :")
print(publicKey)
}
}

if statusPrivateKey == noErr {
if let privateKey = resultPrivateKey as? Data {
print("Private Key: \((privateKey.base64EncodedString()))")
}
}
} else {
print("Error generating key pair: (String(describing: statusCode))")
throw NSError(domain: NSOSStatusErrorDomain, code: Int(statusCode), userInfo: nil)
}


I noticed there are issues when generate the privateKey and publicKey
on MacOS

Any chance you can fix the formatting on your post? Right now it’s quite hard to read. Use the code block button (<>) to format your code blocks as… well… code blocks.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Swift: generate the privatekey and publickey with "cannot open file" exception and "undefined error"
 
 
Q