# SimKeyLite的接口【应用APP使用】

备注:需要先使用 CISS SDK 在线发证 或者 SKF-C 发证, 然后才可以使用 SimkeyLite 离线使用。使用 CISS SDK 续期或者 SKF-C 续期

# 连接卡

  • 定义:
接口名称 connectSimKey
接口描述 建立和卡的连接
使用者 应用客户端
  • 输入参数:
参数 数据类型 说明 备注
cardName NSString Simkey卡盾名称
error NSError * 错误信息提示
delegate SimkeyLiteImplDelegate 代理事件
  • 返回类型:BOOL
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    BOOL result = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  • 返回:YES 成功连接到卡,NO 失败

# 断开连接

  • 定义:
接口名称 close
接口描述 断开和卡的连接, 如果不断开会造成卡通道一直被占用,参考下面的最佳代码实践
使用者 应用客户端
  • 输入参数:
参数 数据类型 说明 备注
delegate SimkeyLiteImplDelegate 代理事件
  • 返回类型:void
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  	BOOL clo = [SimkeyLiteImpl close:self];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  • 返回:void

# 获取SimKey信息

  • 定义
接口名称 getSimKeyInfo
接口描述 获取SimKey信息
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
delegate SimkeyLiteImplDelegate 代理事件
  • 返回类型:UKInfo
参数 数据类型 说明 备注
appletVersion NSString Applet版本
chipVersion NSString 芯片版本
syncFlag NSString 是否同步
isSupportSM2 NSString 是否支持SM2
isSupportRSA1024 NSString 是否支持RSA1024
isSupportRSA2048 NSString 是否支持RSA2048
iccid NSString ICCID值
totalSpace NSString 总空间
manufacturerId NSString 制造商标识
operatorId NSString 运营商标识
macV4 NSString macV4地址
serialNumber NSString 序列号
imsi NSString IMSI
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	UKInfo *info = [SimkeyLiteImpl getSimKeyInfo:&err delegate:self];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 枚举应用列表

  • 定义
接口名称 enumUapp
接口描述 枚举应用列表
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
error NSError * 错误信息提示
delegate SimkeyLiteImplDelegate 代理事件
  • 返回类型:NSArray

  • 代码示例

@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	NSArray *appArr = [SimkeyLiteImpl enumUapp:&err delegate:self];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 枚举容器列表

  • 定义
接口名称 enumContainerInfo
接口描述 枚举容器列表
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用id
error NSError * 错误信息提示
delegate SimkeyLiteImplDelegate 代理事件
  • 返回类型:NSArray

  • 代码示例

@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	NSArray *conArr = [SimkeyLiteImpl enumContainerInfo:@"uAppId" error:&err delegate:self];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 读取公钥

  • 定义
接口名称 readPublicKey
接口描述 读取公钥
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用名称
containerId NSString 容器名称
isSign BOOL YES:签名公钥;
NO:加密公钥
error NSError * 错误信息提示
delegate SimkeyLiteImplDelegate 代理事件
  • 返回类型:NSString,十六进制字符串
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	NSString *result = [SimkeyLiteImpl readPublicKey:@"Appid" containerId:@"ContainerID" isSign:YES delegate:self error:&err];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 私钥解密

  • 定义
接口名称 privateKeyDecrypt
接口描述 私钥解密
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用名称
containerId NSString 容器名称
cipher NSString 待解密数据
signFlag BOOL true:签名私钥;
false:加密私钥
algFlag int 非对称算法标示
1: RSA1024
2: RSA2048
3:SM2
error NSError * 错误信息提示
delegate SimkeyLiteImplDelegate 代理事件
  • 返回类型:NSData

  • 代码示例

@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	NSData *data = [SimkeyLiteImpl privateKeyDecrypt:_AppID containerId:_ContainerID cipher:cipher signFlag:signFlag algFlag:algFlag pin:self error:&err];
  	NSString *result = [self convertDataToHexStr:data];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}

- (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange,BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i =0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) &0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}
@end
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

# 私钥签名

  • 定义
接口名称 privateKeySignHash
接口描述 对hash值进行私钥签名
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用名称
containerId NSString 容器名称
dataHash NSString 待签名的哈希值,如果是sm2算法,则该值为经过预处理之后的哈希值
hashAlgFlag NSString 哈希算法标示
5: sha1
7: sm3
8: sha-256
asymmetricAlgFlag int 非对称算法标示
1: RSA1024
2: RSA2048
3:SM2
error NSError * 错误信息提示
delegate SimkeyLiteImplDelegate 代理事件

返回类型:NSData

  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	NSString *dataHash = @"a91asdebbc0f456a4104092e5c14dbff8ew12c32a91404ebbc0f456a4104092e";
  	NSData *data = [SimkeyLiteImpl privateKeySignHash:@"AppID" containerId:@"ContainerID" dataHash:dataHash hashAlgorithm:7 algFlagpin:SGD_SM2 pin:self error:&err];
  	NSString *result = [self convertDataToHexStr:data];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}

- (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange,BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i =0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) &0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}
@end
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

# 删除容器

  • 定义
接口名称 deleteContainer
接口描述 删除容器
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用名称
containerId NSString 容器名称
error NSError * 错误信息提示
delegate SimkeyLiteImplDelegate 代理事件
  • 返回类型:void
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	BOOL result = [SimkeyLiteImpl deleteContainer:@"AppID" containerId:@"ContainerID" pin:self error:&err];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 写入证书

  • 定义
接口名称 writeCer
接口描述 写入证书
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用名称
containerId NSString 容器名称
isSignCer BOOL YES:签名证书;
NO:加密证书
cer NSData 证书数据
error NSError * 错误信息提示
delegate SimkeyLiteImplDelegate 代理事件
  • 返回类型:void
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	[SimkeyLiteImpl writeCer:@"AppID" containerId:@"ContainerID" isSignCer:YES cer:@"证书" delegate:self error:&err];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 读取证书

  • 定义
接口名称 readCer
接口描述 读取证书
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用名称
containerId NSString 容器名称
isSignCer BOOL YES:签名证书;
NO:加密证书
error NSError * 错误信息提示
delegate SimkeyLiteImplDelegate 代理事件

返回类型:NSData

  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	NSString *dataHash = @"a91asdebbc0f456a4104092e5c14dbff8ew12c32a91404ebbc0f456a4104092e";
  	NSData *data = [SimkeyLiteImpl readCer:@"AppID" containerId:@"ContainerID" isSign:YES delegate:self error:&err];
  	NSString *result = [self convertDataToHexStr:data];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}

- (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange,BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i =0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) &0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}
@end
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

# 更新用户pin

  • 定义
接口名称 updateUserPin
接口描述 验证用户pin成功后更新用户pin
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用id
getUserPinOld SimkeyLiteImplDelegate 代理事件
error NSError * 错误信息提示

返回类型:void

  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	[SimkeyLiteImpl updateUserPin:@"AppID" getUserPinOld:self error:&err];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 生成随机数

  • 定义
接口名称 genRandom
接口描述 生成随机数
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
length int 随机数字符串长度
error NSError * 错误信息提示
  • 返回类型:NSString,十六进制字符串, 字符串长度为 length
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	NSString *result = [SimkeyLiteImpl genRandom:6 error:&err];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 导入对称密钥

  • 定义
接口名称 importSymmetricKey
接口描述 导入对称密钥
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用名称
containerId NSString 容器名称
symmetricAlgFlag int 对称算法标示:
1:3DES
2:SM1
3:SM4
只支持SM1和SM4算法
symmetricKeyEncBySignPubKey NSString 使用签名公钥加密后的对称密钥, 十六进制字符串
pin SimkeyLiteImplDelegate 用户pin, 样例参考SimkeyLiteImplDelegate接口的一个实现样例
error NSError * 错误信息提示

返回类型:void

  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	[SimkeyLiteImpl importSymmetricKey:@"AppID" containerId:@"ContainerID" symmetricAlgFlag:SGD_SM1_ECB symmetricKeyEncBySignPubKey:@"使用签名公钥加密后的对称密钥" pin:self error:&err];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 对称加密

  • 定义
接口名称 encryptBySymmetricKey
接口描述 用对称密钥对数据进行加密
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用名称
symmetricAlgFlag int 对称算法标示:
1:3DES
2:SM1
3:SM4
modeOfOperation int 1:ecb
2:cbc
iv NSData 初始向量, cbc模式下不能为空
sourceData NSData 要进行加密的数据,(最好在外部填充,16byte的倍数位, 否则内部进行pkcs#5填充,但解密不会去填充)
delegate SimkeyLiteImplDelegate 用户pin, 样例参考SimkeyLiteImplDelegate接口的一个实现样例
error NSError * 错误信息提示
  • 返回类型:NSData
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	NSData *data = [SimkeyLiteImpl encryptBySymmetricKey:@"AppID" symmetricAlgFlag:SM1 modeOfOperation:MODE_ECB iv:[@"iv" dataUsingEncoding:NSUTF8StringEncoding] sourceData:[@"明文" dataUsingEncoding:NSUTF8StringEncoding] delegate:self error:&err];
  	NSString *result = [self convertDataToHexStr:data];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}

- (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange,BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i =0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) &0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}
@end
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

# 对称解密

  • 定义
接口名称 decryptBySymmetricKey
接口描述 用对称密钥对数据进行解密
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
uAppId NSString 应用名称
symmetricAlgFlag int 对称算法标示:
1:3DES
2:SM1
3:SM4
modeOfOperation int 1:ecb
2:cbc
iv NSData 初始向量, cbc模式下不能为空
cipher NSData 要进行解密的数据, 方法不会去填充,需要在外部自己去填充
delegate SimkeyLiteImplDelegate 用户pin, 样例参考SimkeyLiteImplDelegate接口的一个实现样例
error NSError * 错误信息提示
  • 返回类型:NSData
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	NSData *data = [SimkeyLiteImpl decryptBySymmetricKey:@"AppID" symmetricAlgFlag:SM1 modeOfOperation:MODE_ECB iv:[@"iv" dataUsingEncoding:NSUTF8StringEncoding] cipher:[@"密文" dataUsingEncoding:NSUTF8StringEncoding] delegate:self error:&err];
  	NSString *result = [self convertDataToHexStr:data];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}

- (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange,BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i =0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) &0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}
@end
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

# 获取导入OTP密钥的参数

  • 定义
接口名称 getImportOTPKeyParam
接口描述 获取导入OTP密钥的参数
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
error NSError * 错误信息提示
  • 返回类型:OTPKeyParam
参数 数据类型 说明 备注
macV4 NSString SimKey的蓝牙地址
serialNumber NSString SimKey的序列号
publicKey NSString 公钥
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	OTPKeyParam *result = [SimkeyLiteImpl getImportOTPKeyParam:&err];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}

- (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange,BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i =0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) &0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}
@end
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

# 导入OTP密钥

  • 定义
接口名称 importOTPKey
接口描述 导入OTP密钥
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
otpKeyEncByPubKey NSString 导入OTP根密钥,使用获取导入OTP密钥的参数 得到的公钥进行加密保护, 十六进制字符串
error NSError * 错误信息提示
  • 返回类型:void
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	OTPKeyParam *result = [SimkeyLiteImpl getImportOTPKeyParam:&err];
  	
  	[SimkeyLiteImpl importOTPKey:@"OTP根密钥" error:&err];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}
@end
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

# 获取OTP

  • 定义
接口名称 getOTP
接口描述 获取OTP
使用者 应用客户端
  • 输入参数
参数 数据类型 说明 备注
T Byte * 参与运算的时间因子 = T0(当前时间) / TC (以秒为单位的口令变化周期)
C Byte * 参与运算的事件因子, 长度固定为4
Q NSString 认证双方通过协商输入的挑战因子, 字符串长度不少于4
N int 令牌或其他终端显示口令的位数, 不小于6
error NSError * 错误信息提示
  • 返回类型:NSString
  • 代码示例
@interface ViewController ()<SimkeyLiteImplDelegate>
@end
  
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
  
    BOOL conn = [SimkeyLiteImpl connectSimKey:@"卡盾名称" error:&err delegate:self];
  
  	NSError *err;
  	long t = [[NSDate date] timeIntervalSince1970] / 60;
  	t = swap_uint64(t);
  	NSString *otpCode = [[CISS alloc] getOTP:&t c:nil q:nil otpLength:6 error:&err];
}

- (NSString *)getUserPin:(NSString *)title{
    return @"123456";//用户pin码
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";//管理员pin码
}

- (NSString *)setUserPin:(NSString *)title{
    return @"123456";//设置pin码
}

uint64_t swap_uint64( uint64_t val )
{
    val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
    val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
    val = (val << 32) | (val >> 32);
    return val;
}
@end
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

# GetPin接口的一个实现样例

  • 代码示例
- (NSString *)getUserPin:(NSString *)title{
    return @"123456";
}

- (NSString *)getAdminPin:(NSString *)title{
    return @"123456";
}

1
2
3
4
5
6
7
8

# SetPin接口的一个实现样例

  • 代码示例
- (NSString *)setUserPin:(NSString *)title{
  return @"123456";
}
1
2
3

# SimkeyLiteImplDelegate定义

方法名 方法和描述 是否必须
getUserPin 获取用户PIN码
setUserPin 设置用户PIN码
getAdminPin 获取管理员PIN码
startSendCode 方法开始执行时调用
sendingCodeWithDes 方法执行中调用
endSendCodeWithError 方法遇到错误时调用
endSendCodeWithDes 方法执行完成时调用

# 方法标识码定义

方法名称 标识码
connectSimKey 100
cancelSimKey 101
getSimKeyInfo 102
genSignKeyPair 103
genSignKeyPairWithCardSDK 104
writeEncryptKeyPair 105
readPublicKey 106
privateKeyDecrypt 107
privateKeySignHash 108
deleteContainer 109
deleteUApp 110
writeCer 111
readCer 112
enumUapp 113
enumContainerInfo 114
updateUserPin 115
resetUserPin 116
genRandom 117
importSymmetricKey 118
encryptBySymmetricKey 119
decryptBySymmetricKey 120
sm3Hash 121
getImportOTPKeyParam 122
importOTPKey 123
getOTP 124