我需要使用公钥将用户密码加密为base64字符串。
公钥是NSString。
这样的事情:
“MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCgWO7p1AvCaHUeaM6rSczBBAqt mKObHxGW3VgTom2zGwswGj9t / Hr7NdJQCGAiq0ijcW9 / oYnM / JobbsyEijHKqIQm OVMsV4JRoG68PEDszH / ebkqWhzu7vG9IQ6VYIkaKHqk7cg + mQ1qFDoigOooFJ2Pf Uzbmg + Z / DuYDuwg + bwIDAQBC“
如何从中制作SecKeyRef?
我找到了一个教程here
但是我无法使其工作,因为SecCertificateCreateWithData总是返回nil。
这是我的代码
NSData* data = [NSString base64DataFromString:PUBLIC_KEY];
SecCertificateRef cert = NULL;
SecPolicyRef policy = NULL;
cert = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)(data));
policy = SecPolicyCreateBasicX509();
OSStatus status = noErr;
SecKeyRef *publicKey = NULL;
SecTrustRef trust = NULL;
SecTrustResultType trustType = kSecTrustResultInvalid;
if (cert != NULL){
SecCertificateRef certArray[1] = {cert};
CFArrayRef certs = CFArrayCreate(kCFAllocatorDefault, (void *)certArray, 1, NULL);
status = SecTrustCreateWithCertificates(certs, policy, &trust);
if (status == errSecSuccess){
status = SecTrustEvaluate(trust, &trustType);
// Evaulate the trust.
switch (trustType) {
case kSecTrustResultInvalid:
case kSecTrustResultConfirm:
case kSecTrustResultDeny:
case kSecTrustResultUnspecified:
case kSecTrustResultFatalTrustFailure:
case kSecTrustResultOtherError:
break;
case kSecTrustResultRecoverableTrustFailure:
*publicKey = SecTrustCopyPublicKey(trust);
break;
case kSecTrustResultProceed:
*publicKey = SecTrustCopyPublicKey(trust);
break;
}
}
} else {
NSLog(@"CERT == NULL");
}
if (publicKey == NULL) {
NSLog(@"PUBLIC KEY == NULL");
}
SecKeyRef总是为零,因为证书也是零。 请帮帮我。