RSA加密如何让字符串NSString如何转成SecKeyRef?
Posted ruixin-jia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RSA加密如何让字符串NSString如何转成SecKeyRef?相关的知识,希望对你有一定的参考价值。
RSA加密如何让字符串NSString如何转成SecKeyRef?
看到网上有很多朋友都提出过这个问题,也没有详细的解答,感觉挺坑的,因为我之前也在为这个问题纠结了好久,之后是在技术群有高人相助才解决掉的,废话不多说直接上代码吧!
这是当时后台提供给我的公钥字符串:服务器是java
#defineRSA_PUBLIC_KEY @"MIICLTCCAZagAwIBAgIEVXVrlDANBgkqhkiG9w0BAQUFADBaMQ0wCwYDVQQGEwRtYWhxMQ0wCwYDVQQIEwRtYWhxMQ0wCwYDVQQHEwRtYWhxMQ0wCwYDVQQKEwRtYWhxMQ0wCwYDVQQLEwRtYWhxMQ0wCwYDVQQDEwRtYWhxMCAXDTE1MDYwODEwMTY1MloYDzIxMTMxMjMxMTAxNjUyWjBaMQ0wCwYDVQQGEwRtYWhxMQ0wCwYDVQQIEwRtYWhxMQ0wCwYDVQQHEwRtYWhxMQ0wCwYDVQQKEwRtYWhxMQ0wCwYDVQQLEwRtYWhxMQ0wCwYDVQQDEwRtYWhxMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDzwZjejBFhG+1K5f2frVCJ2UeIUCkwwCg1ip50hYN1yJLCje+Iya3LHEtv/smqMxxuQuMiE7bOUkN/NHFssfGDt99ff98b0xdWXj4x9/WqNWcwteJvcJwgrQpHhfy+wXOI7DDz14b/qJJ6ES42EZ+M/rg7jw9tQ/Z2cWc56gLVGwIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAE0wDcQTY+gp7Hd2fWl4Yn/6zOmk6p0z5CBHKzFGw1+aTMcKGli4m13Dq5IIvPQV/CY0cKrcXSvNzTs9OXy4gPPyCnhZ+XavKI9y2jlCDzPPWKcMiMWu1mBfIQOV2hwpRpE1sG8uSPqf2S+nsREwHj95tEdPFHCn2OwLOjLgAMMc"
注意:跟后台服务器一定要沟通好,用什么格式 不然会有很多坑等着你去跳,编码格式建议采用(utf-8)
//获取公钥
static SecKeyRef _public_key=nil;
- (SecKeyRef) getPublicKey{// 从公钥证书文件中获取到公钥的SecKeyRef指针
if(_public_key == nil){
//由于后台给我的字符串时base过的所以要转回来,没有的请忽视
NSData *certificateData = [GTMBase64 decodeString:RSA_PUBLIC_KEY];
SecCertificateRef myCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certificateData);
SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
SecTrustRef myTrust;
OSStatus status = SecTrustCreateWithCertificates(myCertificate,myPolicy,&myTrust);
SecTrustResultType trustResult;
if (status == noErr) {
status = SecTrustEvaluate(myTrust, &trustResult);
}
_public_key = SecTrustCopyPublicKey(myTrust);
CFRelease(myCertificate);
CFRelease(myPolicy);
CFRelease(myTrust);
}
return _public_key;
}
以上是关于RSA加密如何让字符串NSString如何转成SecKeyRef?的主要内容,如果未能解决你的问题,请参考以下文章