相关疑难解决方法(0)

如何从DER/PEM文件中获取SecKeyRef

我需要将我的iPhone应用程序与系统集成,他们需要通过给定的公钥加密数据,有3种不同格式的文件.xml .der和.pem,我研究过并发现了一些关于从中获取SecKeyRef的文章DER/PEM,但它们总是返回零.以下是我的代码:

NSString *pkFilePath = [[NSBundle mainBundle] pathForResource:@"PKFile" ofType:@"der"];
NSData *pkData = [NSData dataWithContentsOfFile:pkFilePath]; 

SecCertificateRef   cert; 
cert = SecCertificateCreateWithData(NULL, (CFDataRef) pkData);
assert(cert != NULL);

OSStatus err;

    if (cert != NULL) {
        err = SecItemAdd(
                         (CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:
                                            (id) kSecClassCertificate,  kSecClass, 
                                            (id) cert,                  kSecValueRef,
                                            nil
                                            ], 
                         NULL
                         );
        if ( (err == errSecSuccess) || (err == errSecDuplicateItem) ) {
            CFArrayRef certs = CFArrayCreate(kCFAllocatorDefault, (const void **) &cert, 1, NULL); 
            SecPolicyRef policy = SecPolicyCreateBasicX509();
            SecTrustRef trust;
            SecTrustCreateWithCertificates(certs, policy, &trust);
            SecTrustResultType trustResult;
            SecTrustEvaluate(trust, …
Run Code Online (Sandbox Code Playgroud)

iphone rsa pem der public-key

19
推荐指数
3
解决办法
2万
查看次数

将NSData转换为SecKeyRef

我有一个公钥,我从远程服务器收集,我想用该公钥执行RSA加密.但问题是我将公钥数据作为缓冲区中的字节数组.我可以将它转换为NSData但我无法转换为SecKeyRef所以我可以继续加密.我的加密代码如下:

+(NSString *)encryptRSA:(NSString *)plainTextString withKey:(SecKeyRef)publicKey {
size_t cipherBufferSize = SecKeyGetBlockSize(publicKey);
uint8_t *cipherBuffer = malloc(cipherBufferSize);
uint8_t *nonce = (uint8_t *)[plainTextString UTF8String];
SecKeyEncrypt(publicKey,
              kSecPaddingOAEP,
              nonce,
              strlen( (char*)nonce ),
              &cipherBuffer[0],
              &cipherBufferSize);
NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];
return [encryptedData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Run Code Online (Sandbox Code Playgroud)

}

如您所见,我需要SecKeyRef对象类型来完成加密.但我的RSA公钥是在NSData变量中.那么我怎样才能将NSData转换为SecKeyRef对象类型.提前致谢.

encryption rsa public-key-encryption nsdata ios

7
推荐指数
1
解决办法
6134
查看次数

标签 统计

rsa ×2

der ×1

encryption ×1

ios ×1

iphone ×1

nsdata ×1

pem ×1

public-key ×1

public-key-encryption ×1