Ios SECPKCS12导入任何时间返回空项目
这是我的密码。任何时间项目返回都为空。我在Ios SECPKCS12导入任何时间返回空项目,ios,objective-c,swift,Ios,Objective C,Swift,这是我的密码。任何时间项目返回都为空。我在swift和objective c中尝试过,但没有尝试 let certName : String = "private_key"//name of the certificate// //get p12 file path let resourcePath: String = NSBundle.mainBundle().pathForResource(certName, ofType: "p12")! let p
swift
和objective c
中尝试过,但没有尝试
let certName : String = "private_key"//name of the certificate//
//get p12 file path
let resourcePath: String = NSBundle.mainBundle().pathForResource(certName, ofType: "p12")!
let p12Data: NSData = NSData(contentsOfFile: resourcePath)!
//create key dictionary for reading p12 file
let key : NSString = kSecImportExportPassphrase as NSString
let options : NSDictionary = [key : "password"]
//create variable for holding security information
var privateKeyRef: SecKeyRef? = nil
var items : CFArray?
let securityError: OSStatus = SecPKCS12Import(p12Data, options, &items)
print(items)
我正在使用pkcs8。你需要包括
#包括
#包括
从Github下载openssl
+(NSString*)getSignatureData:(NSString*)signableData{
//获取私钥路径
NSString*路径=[[NSBundle mainBundle]路径资源:@“专用”
类型:@“der”];
NSData*DATASS=[signableData dataUsingEncoding:NSNONLOSSYASCISTRINGEncoding];
字符*文本=(字符*)[signableData UTF8String];
无符号字符*数据;
数据=(无符号字符*)文本;
//使用mode创建新文件,mode的含义与stdio函数fopen()相同
BIO*in=BIO_new_文件([path cStringUsingEncoding:NSUTF8StringEncoding],“rb”);
//PKCS#8私钥信息结构
PKCS8_PRIV_KEY_INFO*p8inf=d2i_PKCS8_PRIV_KEY_INFO_bio(in,NULL);
EVP_PKEY*PKEY=EVP_PKCS82PKEY(第8inf页);
免费PKCS8密钥信息(第8INF页);
不含BIO_(in);
uint8_t*cipherBuffer=NULL;
//计算缓冲区大小。
unsigned int-cipherBufferSize=RSA_size(pkey->pkey.RSA);
无符号整数签名长度;
//分配一些缓冲空间。
cipherBuffer=malloc(cipherBufferSize);
memset((void*)cipherBuffer,0x0,cipherBufferSize);
无符号字符hashedChars[32];
//返回指向散列值的指针
unsigned char*openSSLHash=CC_SHA256(datasss.bytes,(CC_LONG)signableData.length,hashedChars);
//unsigned char*openSSLHash1=SHA256(数据,signableData.length,NULL);
/*
*以下函数对内部的X509_SIG ASN1对象进行签名和验证
*PKCS#8填充RSA加密
*/
RSA_符号(NID_sha256,openSSLHash,sha256_DIGEST_LENGTH,cipherBuffer和signatureLength,pkey->pkey.RSA);
NSData*signedData=[NSData dataWithBytes:(const void*)cipherBuffer长度:signatureLength];
免费执行副总裁(PKEY);
NSString*base64String=[signedData base64EncodedStringWithOptions:0];
返回base64String;
}
我正在使用pkcs8。你需要包括
#包括
#包括
从Github下载openssl
+(NSString*)getSignatureData:(NSString*)signableData{
//获取私钥路径
NSString*路径=[[NSBundle mainBundle]路径资源:@“专用”
类型:@“der”];
NSData*DATASS=[signableData dataUsingEncoding:NSNONLOSSYASCISTRINGEncoding];
字符*文本=(字符*)[signableData UTF8String];
无符号字符*数据;
数据=(无符号字符*)文本;
//使用mode创建新文件,mode的含义与stdio函数fopen()相同
BIO*in=BIO_new_文件([path cStringUsingEncoding:NSUTF8StringEncoding],“rb”);
//PKCS#8私钥信息结构
PKCS8_PRIV_KEY_INFO*p8inf=d2i_PKCS8_PRIV_KEY_INFO_bio(in,NULL);
EVP_PKEY*PKEY=EVP_PKCS82PKEY(第8inf页);
免费PKCS8密钥信息(第8INF页);
不含BIO_(in);
uint8_t*cipherBuffer=NULL;
//计算缓冲区大小。
unsigned int-cipherBufferSize=RSA_size(pkey->pkey.RSA);
无符号整数签名长度;
//分配一些缓冲空间。
cipherBuffer=malloc(cipherBufferSize);
memset((void*)cipherBuffer,0x0,cipherBufferSize);
无符号字符hashedChars[32];
//返回指向散列值的指针
unsigned char*openSSLHash=CC_SHA256(datasss.bytes,(CC_LONG)signableData.length,hashedChars);
//unsigned char*openSSLHash1=SHA256(数据,signableData.length,NULL);
/*
*以下函数对内部的X509_SIG ASN1对象进行签名和验证
*PKCS#8填充RSA加密
*/
RSA_符号(NID_sha256,openSSLHash,sha256_DIGEST_LENGTH,cipherBuffer和signatureLength,pkey->pkey.RSA);
NSData*signedData=[NSData dataWithBytes:(const void*)cipherBuffer长度:signatureLength];
免费执行副总裁(PKEY);
NSString*base64String=[signedData base64EncodedStringWithOptions:0];
返回base64String;
}
它会返回什么错误?在securityError
中捕获错误,然后忽略它。securityError等于0,但项始终返回nil@ArthurSahakyan你们得到答案了吗?为什么项目为零?并没有,但我找到了其他解决方案,但在客观C中是什么?你能分享你的发现吗?因为我有一个相同的问题。它会返回什么错误?在securityError
中捕获错误,然后忽略它。securityError等于0,但项始终返回nil@ArthurSahakyan你们得到答案了吗?为什么项目为零?并没有,但我找到了其他解决方案,但在客观C中是什么?你能分享你的发现吗?因为我也有同样的问题。
I'm using pkcs8. And you need to include
#include <CommonCrypto/CommonDigest.h>
#include <openssl/engine.h>
openssl download from Github
+ (NSString*) getSignatureData:(NSString*) signableData {
// get private key path
NSString* path = [[NSBundle mainBundle] pathForResource:@"private"
ofType:@"der"];
NSData* datasss = [signableData dataUsingEncoding:NSNonLossyASCIIStringEncoding];
char* text = (char*) [signableData UTF8String];
unsigned char *data;
data = (unsigned char *) text;
//creates a new file BIO with mode, mode the meaning of mode is the same as the stdio function fopen()
BIO *in = BIO_new_file([path cStringUsingEncoding:NSUTF8StringEncoding], "rb");
//PKCS#8 private key info structure
PKCS8_PRIV_KEY_INFO *p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
EVP_PKEY *pkey = EVP_PKCS82PKEY(p8inf);
PKCS8_PRIV_KEY_INFO_free(p8inf);
BIO_free(in);
uint8_t * cipherBuffer = NULL;
// Calculate the buffer sizes.
unsigned int cipherBufferSize = RSA_size(pkey->pkey.rsa);
unsigned int signatureLength;
// Allocate some buffer space.
cipherBuffer = malloc(cipherBufferSize);
memset((void *)cipherBuffer, 0x0, cipherBufferSize);
unsigned char hashedChars[32];
//return a pointer to the hash value
unsigned char *openSSLHash = CC_SHA256(datasss.bytes, (CC_LONG)signableData.length, hashedChars);
//unsigned char *openSSLHash1 = SHA256(data, signableData.length, NULL);
/*
* The following function sign and verify a X509_SIG ASN1 object inside
* PKCS#8 padded RSA encryption
*/
RSA_sign(NID_sha256, openSSLHash, SHA256_DIGEST_LENGTH, cipherBuffer, &signatureLength, pkey->pkey.rsa);
NSData *signedData = [NSData dataWithBytes:(const void*)cipherBuffer length:signatureLength];
EVP_PKEY_free(pkey);
NSString *base64String = [signedData base64EncodedStringWithOptions:0];
return base64String;
}