-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSACryptor.h
70 lines (61 loc) · 1.54 KB
/
RSACryptor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// RSACryptor.h
// EncryptDemo
//
// Created by H on 2018/10/16.
// Copyright © 2018年 hank. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface RSACryptor : NSObject
+ (instancetype)sharedRSACryptor;
/**
* 生成密钥对
*
* @param keySize 密钥尺寸,可选数值(512/1024/2048)
*/
- (void)generateKeyPair:(NSUInteger)keySize;
/**
* 加载公钥
*
* @param publicKeyPath 公钥路径
*
@code
# 生成证书
$ openssl genrsa -out ca.key 1024
# 创建证书请求
$ openssl req -new -key ca.key -out rsacert.csr
# 生成证书并签名
$ openssl x509 -req -days 3650 -in rsacert.csr -signkey ca.key -out rsacert.crt
# 转换格式
$ openssl x509 -outform der -in rsacert.crt -out rsacert.der
@endcode
*/
- (void)loadPublicKey:(NSString *)publicKeyPath;
/**
* 加载私钥
*
* @param privateKeyPath p12文件路径
* @param password p12文件密码
*
@code
openssl pkcs12 -export -out p.p12 -inkey ca.key -in rsacert.crt
@endcode
*/
- (void)loadPrivateKey:(NSString *)privateKeyPath password:(NSString *)password;
/**
* 加密数据
*
* @param plainData 明文数据
*
* @return 密文数据
*/
- (NSData *)encryptData:(NSData *)plainData;
/**
* 解密数据
*
* @param cipherData 密文数据
*
* @return 明文数据
*/
- (NSData *)decryptData:(NSData *)cipherData;
@end