本文目录一览:
iOS生成CSR
感谢大神的带领利用Openssl 生成RSA相关的CSR
利用Openssl 生成RSA相关的CSR-Demo
3.继续查阅资料
替换上面说的DEMO, 说的一些函数, 在Openssl 找差不多一样的
比如Demo里面
我就替换成
反正就是尝试...
还有下面的都是替换, 看感觉去找相关的API
是时候耐着性子, 认真研究了
终于控制台有log了:
有输出了, 总比没有好吧, 总算有点收获了.. 不要高兴太早 这么多"//////////////////////", 直觉告诉我, 肯定是错误的.
在线解析
解析是正确的... 你懵不懵
言归正传
后来在git上提问
想要生成这样的数据
** -----BEGIN CERTIFICATE REQUEST-----
MIIBQzCB6wIBAzCBiDEWMBQGA1UEAwwNd3d3LjM0NDU2LmNvbTEMMAoGA1UECgwD
QUJDMQswCQYDVQQLDAJERTEWMBQGA1UECAwNU2hhbmdIYWkgQ2l0eTERMA8GA1UE
BwwIc2hhbmdoYWkxCzAJBgNVBAYMAkNOMRswGQYJKoZIhvcNAQkBDAxhYmNAeWVh
ci5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAT4DOFQPjhnE5bKIUHPyTrj
5D+L9xBw6sYc5tNG5d1fFf/aZSyOFpx+1navCHRnGK1cELiiAyjv081hOftbK/Lf
oAAwCgYIKoZIzj0EAwIDRwAwRAIgFJEEdrllYUVI+ar6lXDwz1p6wFlr8ez4m4Yc
hGfyD9ICIFqHL85rMPNishu8bj/L9P0HoXolKt7S1uctF+vyyM2U
-----END CERTIFICATE REQUEST-----**
要怎么处理, git回复很及时, 很快就得到了解决
选择的的加密曲线不对, 还有就是要加上这2句, (虽然不知道干啥用的)
随后附上demo地址 如果帮到你, 请点一个✨✨✨✨✨
附一个图爬山吗?
细品细品👇👇👇👇👇👇
一女生跟跟男朋友打电话说:
我这边下雨了, 你那里大不大.......
Ios14如何https变成http
申请个证书。
HTTPS无非分两种一种是像正规的颁发机构申请,另外一种就是自建证书,这个CSR是由后台生成然后粘贴到这的,向我们买的这个证书只要有域名信息就可以了,在CSR上传成功后,填写一些管理员信息就可以直接点下一步。
在邮箱验证完之后,就可以回到你买证书的那个网站了,此时就已经将http证书配置完成了。
ios怎么实现RAS加密解密
转载最近几天折腾了一下如何在iOS上使用RSA来加密。iOS上并没有直接的RSA加密API。但是iOS提供了x509的API,而x509是支持RSA加密的。因此,我们可以通过制作自签名的x509证书(由于对安全性要求不高,我们并不需要使用CA认证的证书),再调用x509的相关API来进行加密。接下来记录一下整个流程。
第一步,制作自签名的证书
1.最简单快捷的方法,打开Terminal,使用openssl(Mac OS X自带)生成私钥和自签名的x509证书。
openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650
按照命令行的提示输入内容就行了。
几个说明:
public_key.der是输出的自签名的x509证书,即我们要用的。
private_key.pem是输出的私钥,用来解密的,请妥善保管。
rsa:1024这里的1024是密钥长度,1024是比较安全的,如果需要更安全的话,可以用2048,但是加解密代价也会增加。
-days:证书过期时间,一定要加上这个参数,默认的证书过期时间是30天,一般我们不希望证书这么短就过期,所以写上比较合适的天数,例如这里的3650(10年)。
事实上,这一行命令包含了好几个步骤(我研究下面这些步骤的原因是我手头已经由一个private_key.pem私钥了,想直接用这个来生成x509证书,也就是用到了下面的2-3)
1)创建私钥
openssl genrsa -out private_key.pem 1024
2)创建证书请求(按照提示输入信息)
openssl req -new -out cert.csr -key private_key.pem
3)自签署根证书
openssl x509 -req -in cert.csr -out public_key.der -outform der -signkey private_key.pem -days 3650
2.验证证书。把public_key.der拖到xcode中,如果文件没有问题的话,那么就可以直接在xcode中打开,看到证书的各种信息。
第二步,使用public_key.der来进行加密。
1.导入Security.framework。
2.把public_key.der放到mainBundle中(一般直接拖到Xcode就行啦)。
3.从public_key.der读取公钥。
4.加密。
下面是参考代码(只能用于加密长度小于等于116字节的内容,适合于对密码进行加密。使用了ARC,不过还是要注意部分资源需要使用CFRealse来释放)
RSA.h
//
// RSA.h
//
#import Foundation/Foundation.h
@interface RSA : NSObject {
SecKeyRef publicKey;
SecCertificateRef certificate;
SecPolicyRef policy;
SecTrustRef trust;
size_t maxPlainLen;
}
- (NSData *) encryptWithData:(NSData *)content;
- (NSData *) encryptWithString:(NSString *)content;
@end
RSA.m
//
// RSA.m
//
#import "RSA.h"
@implementation RSA
- (id)init {
self = [super init];
NSString *publicKeyPath = [[NSBundle mainBundle] pathForResource:@"public_key"
ofType:@"der"];
if (publicKeyPath == nil) {
NSLog(@"Can not find pub.der");
return nil;
}
NSDate *publicKeyFileContent = [NSData dataWithContentsOfFile:publicKeyPath];
if (publicKeyFileContent == nil) {
NSLog(@"Can not read from pub.der");
return nil;
}
certificate = SecCertificateCreateWithData(kCFAllocatorDefault, ( __bridge CFDataRef)publicKeyFileContent);
if (certificate == nil) {
NSLog(@"Can not read certificate from pub.der");
return nil;
}
policy = SecPolicyCreateBasicX509();
OSStatus returnCode = SecTrustCreateWithCertificates(certificate, policy, trust);
if (returnCode != 0) {
NSLog(@"SecTrustCreateWithCertificates fail. Error Code: %ld", returnCode);
return nil;
}
SecTrustResultType trustResultType;
returnCode = SecTrustEvaluate(trust, trustResultType);
if (returnCode != 0) {
NSLog(@"SecTrustEvaluate fail. Error Code: %ld", returnCode);
return nil;
}
publicKey = SecTrustCopyPublicKey(trust);
if (publicKey == nil) {
NSLog(@"SecTrustCopyPublicKey fail");
return nil;
}
maxPlainLen = SecKeyGetBlockSize(publicKey) - 12;
return self;
}
- (NSData *) encryptWithData:(NSData *)content {
size_t plainLen = [content length];
if (plainLen maxPlainLen) {
NSLog(@"content(%ld) is too long, must %ld", plainLen, maxPlainLen);
return nil;
}
void *plain = malloc(plainLen);
[content getBytes:plain
length:plainLen];
size_t cipherLen = 128; // 当前RSA的密钥长度是128字节
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 = [NSData dataWithBytes:cipher
length:cipherLen];
}
free(plain);
free(cipher);
return result;
}
- (NSData *) encryptWithString:(NSString *)content {
return [self encryptWithData:[content dataUsingEncoding:NSUTF8StringEncoding]];
}
- (void)dealloc{
CFRelease(certificate);
CFRelease(trust);
CFRelease(policy);
CFRelease(publicKey);
}
@end
使用方法:
RSA *rsa = [[RSA alloc] init];
if (rsa != nil) {
NSLog(@"%@",[rsa encryptWithString:@"test"]);
}
else {
NSLog(@"init rsa error");
}
在iOS中创建及使用自签名SSL证书应该注意什么
自签名的证书不会被信任(项目不能开展),建议申请商用可信SSL证书。