Ios 将openssl生成的RSA私钥加载到MacOS SecKeyRef失败

Ios 将openssl生成的RSA私钥加载到MacOS SecKeyRef失败,ios,macos,security,openssl,Ios,Macos,Security,Openssl,我正在从事一个项目,该项目需要将RSA私钥(DER格式)读入MacOS的SecKeyRef对象 我通过 openssl genrsa -out private.pem 1024 openssl rsa -in private.pem -outform DER -out private.der 我使用SecKeyCreateWithData将private.der加载到MacOS: unsigned char *keyBytes; // contains all the bytes from t

我正在从事一个项目,该项目需要将RSA私钥(DER格式)读入MacOS的SecKeyRef对象

我通过

openssl genrsa -out private.pem 1024
openssl rsa -in private.pem -outform DER -out private.der
我使用SecKeyCreateWithData将private.der加载到MacOS:

unsigned char *keyBytes; // contains all the bytes from the file "private.der"
int keyBytesLen; // length of the data loaded
NSData keyData = [NSData dataWithBytes:keyBytes length:keyBytesLen];
NSDictionary* options = @{(id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, 
                          (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate,
                          (id)kSecAttrKeySizeInBits: @1024};
SecKeyRef privateKey = SecKeyCrecateWithData((__bridge CFDataRef) keyData, (__bridge CFDictionaryRef) options, &error);

OSstatus status = SecKeyDecrypt(privateKey, .... some encrypted message);
// status returns -50 (errSecParam)
因此,我注意到私钥已成功加载,但无法解密加密的消息。我100%确信我在SecKeyDecrypt()函数中输入的其他参数是正确的,因为如果我通过macOS的内置函数生成privateKey,而不是从openssl生成的DER格式文件加载,那么它就可以工作

我还注意到,当我转储密钥(二进制格式)时,前几个字节(版本头)与MacOS内置函数和openssl生成的密钥不同。如果密钥由MacOS生成(例如:SecKeyGeneratePair)并由SecKeyCopyExternalRepresentation输出,则前几个字节如下所示:

3082 025d 0201 0002 8181 ...
而openssl生成的密钥如下所示:

3082 025c 0201 0002 8181 ...
我知道(基于PKCS#1),这些字节表示私钥的“版本”,但不确定如何解释它

如需将openssl生成的密钥加载到MacOS API中的任何帮助或类似工作示例,将不胜感激