rsa从cer证书中拿公钥进行加密
来源:互联网 发布:java语言怎么学 编辑:程序博客网 时间:2024/06/05 03:56
SecCertificateCreateWithData
总是返回为nil,原因是因为这个证书是服务器自己生成的,不是正规的CA机构颁发的,苹果安全性要求是很高的,所以这个读不出来,直接导致后面的崩溃。后来折腾了半天,比如直接从证书里面导出字符串作为公钥,也不行,安卓这样也是不行的。
后来迪哥又生成了个der证书,这样读就是正常的,这样就能正常的拿到公钥。
- (id)init {
self = [superinit];
NSString *publicKeyPath = [[NSBundlemainBundle] pathForResource:@"public-rsa"ofType:@"der"];
//從檔案讀取公鑰
if (publicKeyPath ==nil) {
NSLog(@"Can not find pub.der");
returnnil;
}
NSDate *publicKeyFileContent = [NSDatadataWithContentsOfFile:publicKeyPath];
if (publicKeyFileContent ==nil) {
NSLog(@"Can not read from pub.der");
returnnil;
}
certificate =SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)publicKeyFileContent);
if (certificate ==nil) {
NSLog(@"Can not read certificate from pub.der");
returnnil;
}
policy =SecPolicyCreateBasicX509();
OSStatus returnCode =SecTrustCreateWithCertificates(certificate,policy, &trust);
if (returnCode !=0) {
NSLog(@"SecTrustCreateWithCertificates fail. Error Code: %ld", returnCode);
returnnil;
}
SecTrustResultType trustResultType;
returnCode = SecTrustEvaluate(trust, &trustResultType);
if (returnCode !=0) {
returnnil;
}
publicKey =SecTrustCopyPublicKey(trust);
if (publicKey ==nil) {
NSLog(@"SecTrustCopyPublicKey fail");
returnnil;
}
maxPlainLen =SecKeyGetBlockSize(publicKey) -12;
returnself;
}
//利用公钥 RSA加密
- (NSData *) encryptWithData:(NSData *)content {
size_t plainLen = [contentlength];
if (plainLen >maxPlainLen) {
NSLog(@"content(%ld) is too long, must < %ld", plainLen,maxPlainLen);
returnnil;
}
void *plain =malloc(plainLen);
[content getBytes:plain
length:plainLen];
size_t cipherLen =256; // 目前使用的RSA加密長度為2048bits(即256bytes)
void *cipher =malloc(cipherLen);
OSStatus returnCode =SecKeyEncrypt(publicKey,kSecPaddingPKCS1, plain,
plainLen, cipher, &cipherLen);
NSData *result =nil;
if (returnCode !=0) {
NSLog(@"SecKeyEncrypt fail. Error Code: %ld", returnCode);
}
else {
result = [NSDatadataWithBytes:cipher length:cipherLen];
}
free(plain);
free(cipher);
return result;
}
- (NSData *) encryptWithString:(NSString *)content {
return [selfencryptWithData:[contentdataUsingEncoding:NSUTF8StringEncoding]];
}
- (NSString *) encryptToString:(NSString *)content {
NSData *data = [selfencryptWithString:content];
return [selfbase64forData:data];
}
// convert NSData to NSString
- (NSString *)base64forData:(NSData *)theData {
constuint8_t* input = (constuint8_t*)[theData bytes];
NSInteger length = [theDatalength];
staticchar table[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableDatadataWithLength:((length +2) / 3) *4];
uint8_t* output = (uint8_t*)data.mutableBytes;
NSInteger i;
for (i=0; i < length; i +=3) {
NSInteger value =0;
NSInteger j;
for (j = i; j < (i +3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger theIndex = (i /3) * 4;
output[theIndex + 0] = table[(value >>18) & 0x3F];
output[theIndex + 1] = table[(value >>12) & 0x3F];
output[theIndex + 2] = (i +1) < length ? table[(value >>6) & 0x3F] :'=';
output[theIndex + 3] = (i +2) < length ? table[(value >>0) & 0x3F] :'=';
}
return [[NSStringalloc] initWithData:dataencoding:NSUTF8StringEncoding];
}
- (void)dealloc{
CFRelease(certificate);
CFRelease(trust);
CFRelease(policy);
CFRelease(publicKey);
}
- rsa从cer证书中拿公钥进行加密
- 证书相关后缀文件(SSL,X.509,PEM,DER,CRT,CSR,CER,KEY,P12)及RSA数据加密解密
- 使用RSA证书加密敏感数据
- RSA加密解密及证书
- OpenSSL生成证书进行iOS加密,java解密的RSA非对称加密 详解
- 从自签名证书导出pfx和cer证书
- wolfssl进行rsa加密
- 加密证书和RSA加密解密
- ios、https 加密证书转化 .crt - .cer
- C#利用CER证书文件对远程Service进行验证
- 使用cryptography进行RSA加密
- php RSA加密 JAVA 证书生成
- IdentityServer4 SigningCredential(RSA 证书加密)
- PKCS cer 证书
- PKCS cer 证书
- cer证书安装
- cer证书签名验证
- java解析cer证书
- LeetCode-35. Search Insert Position
- 【机器学习】Tensorflow:理解和实现快速风格化图像fast neural style
- X5的UI部分和传统Web页面开发的差异
- JAVA 常见异常
- 小程序跳转页面传递参数2
- rsa从cer证书中拿公钥进行加密
- web hdfs端口50075和httpfs端口14000的使用
- 为ipad替换图标
- java中函数积累,会不断修改
- css 盒子模型之布局
- java代码弹出一个输入框
- c++11-智能指针和引用计数
- 简单易懂的Tomcat+Apache集群配置
- Disruptor