iOS SDK 文档

iOS端是由C语言与Objective-C混编,然后再进行封装对外提供相关算法接口。SDK中实现的所有算法都是线程安全的。

1. 国密SM3


接口描述:

/**
 * SM3密码杂凑算法,国家密码局于2010年12月发布
 * @param input 待计算数据
 * @return 输出256比特(32字节)的杂凑值
 * @throws YiCryptException 计算出错时抛出异常。
 */
- (NSData *) sm3Hash: (NSData *) input;
接口异常:

ERR_ILLEGAL_INPUT: 待计算数据非法,如input为null,或长度小于1
调用示例:

@try {
    NSData * sm3Hash = [[YiSMCore defaultCore] sm3Hash:[[YiSMCore defaultCore] str2Bcd:@"616263"]];
    NSLog(@"sm3Hash: %@", [[YiSMCore defaultCore] bcd2Str:sm3Hash]);
}
@catch(YiCryptException * e) {
    NSLog(@"%@: %@", [e name], [e reason]);
}
输出示例:

sm3Hash: 66C7F0F462EEEDD9D1F2D46BDC10E4E24167C4875CF2F7A2297DA02B8F4BA8E0

2. 国密SMS4


接口描述:

/**
 * 国密SMS4 ECB算法
 * @param key 密钥
 * @param cryptMode 算法模式,ENCRYPT为加密,DECRYPT为解密
 * @param paddingMode 填充模式,支持NO_PADDING及PKCS7_PADDING
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sms4CryptECB: (NSData *) key andCryptMode:(YiCryptMode) cryptMode andPaddingMode:(YiPaddingMode) paddingMode andInput:(NSData *) input;

/**
 * 国密SMS4 CBC算法
 * @param key 密钥
 * @param iv 向量
 * @param cryptMode 算法模式,ENCRYPT为加密,DECRYPT为解密
 * @param paddingMode 填充模式,支持NO_PADDING及PKCS7_PADDING
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sms4CryptCBC: (NSData *) key andIV:(NSData *) iv andCryptMode:(YiCryptMode) cryptMode andPaddingMode:(YiPaddingMode) paddingMode andInput:(NSData *) input;
接口异常:

ERR_ILLEGAL_INPUT: 待计算数据非法,如input为null、长度小于1或长度不满足paddingMode要求
ERR_ILLEGAL_KEY: key为null或长度不等于16
ERR_ILLEGAL_IV: iv为null或长度不等于16
ERR_SMS4_ENCRYPT_FAILED: sms4 加密失败
ERR_SMS4_DECRYPT_FAILED: sms4 解密失败
调用示例:

// ECB NO_PADDING 加密
NSData * sms4ECBNoPaddingEnc = [[YiSMCore defaultCore] sms4CryptECB:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andCryptMode:ENCRYPT andPaddingMode:NO_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"]];

// ECB NO_PADDING 解密
NSData * sms4ECBNoPaddingDec = [[YiSMCore defaultCore] sms4CryptECB:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andCryptMode:DECRYPT andPaddingMode:NO_PADDING andInput:sms4ECBNoPaddingEnc];

// ECB PKCS7_PADDING 加密
NSData * sms4ECBPKCS7PaddingEnc = [[YiSMCore defaultCore] sms4CryptECB:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andCryptMode:ENCRYPT andPaddingMode:PKCS7_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

// ECB PKCS7_PADDING 解密
NSData * sms4ECBPKCS7PaddingDec = [[YiSMCore defaultCore] sms4CryptECB:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andCryptMode:DECRYPT andPaddingMode:PKCS7_PADDING andInput:sms4ECBPKCS7PaddingEnc];

// CBC NO_PADDING 加密
NSData * sms4CBCNoPaddingEnc = [[YiSMCore defaultCore] sms4CryptCBC:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andIV: [[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andCryptMode:ENCRYPT andPaddingMode:NO_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"]];

// CBC NO_PADDING 解密
NSData * sms4CBCNoPaddingDec = [[YiSMCore defaultCore] sms4CryptCBC:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andIV: [[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andCryptMode:DECRYPT andPaddingMode:NO_PADDING andInput:sms4CBCNoPaddingEnc];

// CBC PKCS7_PADDING 加密
NSData * sms4CBCPKCS7PaddingEnc = [[YiSMCore defaultCore] sms4CryptCBC:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andIV: [[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andCryptMode:ENCRYPT andPaddingMode:PKCS7_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

// CBC PKCS7_PADDING 解密
NSData * sms4CBCPKCS7PaddingDec = [[YiSMCore defaultCore] sms4CryptCBC:[[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andIV: [[YiSMCore defaultCore] str2Bcd:@"0123456789abcdeffedcba9876543210"] andCryptMode:DECRYPT andPaddingMode:PKCS7_PADDING andInput:sms4CBCPKCS7PaddingEnc];
输出示例:

sms4ECBNoPaddingEnc: 681EDF34D206965E86B3E94F536E4246
sms4ECBNoPaddingDec: 0123456789ABCDEFFEDCBA9876543210

sms4ECBPKCS7PaddingEnc: 1055435B9ECE612344F8E10016C4943B
sms4ECBPKCS7PaddingDec: 616263

sms4CBCNoPaddingEnc: 2677F46B09C122CC975533105BD4A22A
sms4CBCNoPaddingDec: 0123456789ABCDEFFEDCBA9876543210

sms4CBCPKCS7PaddingEnc: 30283FFF39522AEECE5230E645B59D47
sms4CBCPKCS7PaddingDec: 616263

4. 国密SM2


4.1 SM2 密钥相关


4.1.1 SM2 密钥生成


接口描述:

/**
 * 国密SM2,生成密钥对
 * @return SM2密钥对
 * @throws YiCryptException 计算出错时抛出异常
 */
- (YiSM2KeyPair *) sm2GenerateKeyPair;
接口异常:

ERR_SM2_GEN_KEYPAIR_FAILED: 生成密钥对失败
ERR_SM2_EXTRACT_KEYPAIR_FAILED: 提取密钥失败
调用示例:

// SM2 Gen
YiSM2KeyPair * sm2KeyPair = [[YiSMCore defaultCore] sm2GenerateKeyPair];

NSData * sm2PrivKey = [sm2KeyPair privateKey];
NSData * sm2PubKey = [sm2KeyPair publicKey];
NSData * sm2CompressedPubKey = [sm2KeyPair compressedPublicKey];

YiSM2KeyPair * sm2PrivK = [YiSM2KeyPair createByPrivateKey:sm2PrivKey];
YiSM2KeyPair * sm2PubK = [YiSM2KeyPair createByPublicKey:sm2PubKey];
YiSM2KeyPair * sm2ComPubK = [YiSM2KeyPair createByCompressedPublicKey:sm2CompressedPubKey];
输出示例:

sm2PrivKey: 1FECA33B68D086DC52460B46748E4ADC29D4ED4C5A272E08423F3D7545DBEE4D
sm2PubKey: 04D2E78000DD04560F1D9F35F50FB80744086BB4D96656720C16BC9D446BB396B9F334EB18A7A367F698252B1AF9B05F3AF33837CC60268194662F8B9B8F69F852
sm2CompressedPubKey: 02D2E78000DD04560F1D9F35F50FB80744086BB4D96656720C16BC9D446BB396B9

4.1.2 SM2 私钥生成公钥


接口描述:

/**
 * 国密SM2,私钥生成公钥
 * @param privKey 私钥
 * @return SM2公钥PC||x||y,其中PC=04
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sm2GetPubKey:(YiSM2KeyPair *) privKey;

/**
 * 国密SM2,私钥生成压缩公钥
 * @param privKey 私钥
 * @return SM2压缩公钥yTilde||x
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sm2GetCompressedPubKey:(YiSM2KeyPair *) privKey;
接口异常:

ERR_SM2_ILLEGAL_PRIVATE_KEY: 私钥非法
ERR_SM2_GET_PUBLIC_KEY_FAILED: 获取公钥失败
ERR_SM2_COMPRESS_PUBLIC_KEY_FAILED: 压缩公钥失败
调用示例:

// SM2 Get Pub Key
NSData * sm2PubKey = [[YiSMCore defaultCore] sm2GetPubKey:sm2PrivK];
NSData * sm2CompressedPubKey = [[YiSMCore defaultCore] sm2GetCompressedPubKey:sm2PrivK];
输出示例:

sm2PubKey: 04D2E78000DD04560F1D9F35F50FB80744086BB4D96656720C16BC9D446BB396B9F334EB18A7A367F698252B1AF9B05F3AF33837CC60268194662F8B9B8F69F852
sm2CompressedPubKey: 02D2E78000DD04560F1D9F35F50FB80744086BB4D96656720C16BC9D446BB396B9

4.1.3 SM2 公钥压缩


接口描述:

/**
 * 国密SM2,公钥压缩
 * @param pubKey 私钥
 * @return SM2压缩公钥yTilde||x
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sm2CompressPubKey:(YiSM2KeyPair *) pubKey;
接口异常:

ERR_SM2_ILLEGAL_PUBLIC_KEY: 公钥非法
ERR_SM2_COMPRESS_PUBLIC_KEY_FAILED: 压缩公钥失败
调用示例:

NSData * sm2CompressedPubKey = [[YiSMCore defaultCore] sm2CompressPubKey:sm2PubK];
输出示例:

sm2CompressedPubKey: 02D2E78000DD04560F1D9F35F50FB80744086BB4D96656720C16BC9D446BB396B9

4.1.4 SM2 公钥解压


接口描述:

/**
 * 国密SM2,公钥解压
 * @param compressedPubKey 压缩公钥
 * @return SM2公钥PC||x||y,其中PC=04
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sm2DecompressPubKey:(YiSM2KeyPair *) compressedPubKey;
接口异常:

ERR_SM2_ILLEGAL_PUBLIC_KEY: 公钥非法
ERR_SM2_DECOMPRESS_PUBLIC_KEY_FAILED: 解压公钥失败
调用示例:

NSData * sm2PubKey = [[YiSMCore defaultCore] sm2DecompressPubKey:sm2ComPubK];
输出示例:

sm2PubKey: 04D2E78000DD04560F1D9F35F50FB80744086BB4D96656720C16BC9D446BB396B9F334EB18A7A367F698252B1AF9B05F3AF33837CC60268194662F8B9B8F69F852

4.2 SM2 私钥签名


接口描述:

/**
 * 国密SM2,私钥签名
 * @param privKey 私钥
 * @param userId user id,长度需小于127字节
 * @param input 输入数据
 * @return 签名结果r||s
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sm2Sign:(YiSM2KeyPair *) privKey andUserId: (NSString *) userId andInput:(NSData *) input;

/**
 * 国密SM2,私钥签名,采用默认的userId:1234567812345678
 * @param privKey 私钥
 * @param input 输入数据
 * @return 签名结果r||s
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sm2Sign:(YiSM2KeyPair *) privKey andInput:(NSData *) input;
接口异常:

ERR_SM2_ILLEGAL_PRIVATE_KEY: 私钥非法
ERR_SM2_ILLEGAL_USERID: User Id非法
ERR_ILLEGAL_INPUT: 输入数据非法
ERR_SM2_SIGN_FAILED: 签名失败
调用示例:

NSData * signResult1 = [[YiSMCore defaultCore] sm2Sign:sm2PrivK andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

NSData * signResult2 = [[YiSMCore defaultCore] sm2Sign:sm2PrivK andUserId:@"1234567812345678" andInput: [[YiSMCore defaultCore] str2Bcd:@"616263"]];
输出示例:

sm2SignResult1: 758DF7D55A96E326D4D9E16EE7247BDE31AB71EF48A1B376280D7AC13C5F353F51C14493096A9B0236D97CA40EDC19393D805FE5EB48F6C3FED2E61C6E55FAC1

sm2SignResult2: 242A94578DA2C69FF5E95D83304AE412A1489F1D484DDEFC9A89719A72547366709C5A3CCDDECCF300FC343DC6532E957B567839E6F1CCA3BD9F5A9216B56FB5

4.3 SM2 公钥验签


接口描述:

/**
 * 国密SM2,公钥验签
 * @param pubKey 公钥
 * @param userId user id,长度需小于127字节
 * @param input 输入数据
 * @param signData 待验签名r||s
 * @return 验签结果,true验签成功
 * @throws YiCryptException 计算出错时抛出异常
 */
- (BOOL) sm2Verify:(YiSM2KeyPair *) pubKey andUserId:(NSString *) userId andInput:(NSData *) input andSignData:(NSData *) signData;

/**
 * 国密SM2,公钥验签,采用默认的userId:1234567812345678
 * @param pubKey 公钥
 * @param input 输入数据
 * @param signData 待验签名r||s
 * @return 验签结果,true验签成功
 * @throws YiCryptException 计算出错时抛出异常
 */
- (BOOL) sm2Verify:(YiSM2KeyPair *) pubKey andInput:(NSData *) input andSignData:(NSData *) signData;
接口异常:

ERR_SM2_ILLEGAL_PUBLIC_KEY: 公钥非法
ERR_SM2_ILLEGAL_USERID: User Id非法
ERR_ILLEGAL_INPUT: 输入数据非法
ERR_SM2_ILLEGAL_SIGN_DATA: 签名数据非法
调用示例:

BOOL sm2VerifyResult1 = [[YiSMCore defaultCore] sm2Verify:sm2PubK andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"] andSignData:signResult1];

BOOL sm2VerifyResult2 = [[YiSMCore defaultCore] sm2Verify:sm2PubK andUserId:@"1234567812345678" andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"] andSignData:signResult2];

BOOL sm2VerifyResult3 = [[YiSMCore defaultCore] sm2Verify:sm2ComPubK andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"] andSignData:signResult1];

BOOL sm2VerifyResult4 = [[YiSMCore defaultCore] sm2Verify:sm2ComPubK andUserId:@"1234567812345678" andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"] andSignData:signResult2];
输出示例:

sm2VerifyResult1: YES
sm2VerifyResult2: YES
sm2VerifyResult3: YES
sm2VerifyResult4: YES

4.4 SM2 公钥加密


接口描述:

/**
 * 国密SM2,公钥加密
 * @param pubKey 公钥x||y
 * @param input 输入数据
 * @return 输出C1||C2||C3,其中C1=PC||x1||y1,C2=密文,C3=sm3_hash
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sm2Encrypt:(YiSM2KeyPair *) pubKey andInput:(NSData *) input;
接口异常:

ERR_SM2_ILLEGAL_PUBLIC_KEY: 公钥非法
ERR_ILLEGAL_INPUT: 输入数据非法
ERR_SM2_ENCRYPT_FAILED: 加密失败
调用示例:

NSData * sm2EncBytes = [[YiSMCore defaultCore] sm2Encrypt:sm2PubK andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

NSData * sm2CompressedKeyEncBytes = [[YiSMCore defaultCore] sm2Encrypt:sm2ComPubK andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];
输出示例:

sm2EncBytes: 04092B2E6D12EF8F1ACCA5944A50B983A2BE789F30FD7A2888D5096707DF5A9F26AE8973925C191748E23E2C5FC99D9150EE4F52434FC0DB364C6D604CC44D1C3435BFEBD1A208DF3F08EBD54390C396ACC84B543A50B76695472D90C83EB7E70C8B2D4A

sm2CompressedKeyEncBytes: 04B11A95C04A315430CFE766BA3C2984009234DB0EB714C1B0208A518B08570A2C4794DFFD927BC015A3EEB64C0759A111270C1188F6D7770F8493CE6D726A134ADCF3F3B0825BF23038C0CDA513701A0669BA9E8938703F00C205397AF52E8B0027E559

4.5 SM2 私钥解密


接口描述:

/**
 * 国密SM2,私钥解密
 * @param privKey 私钥
 * @param input 输入C1||C2||C3,其中C1=PC||x1||y1,C2=密文,C3=sm3_hash
 * @return 解密结果
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) sm2Decrypt:(YiSM2KeyPair *) privKey andInput:(NSData *) input;
接口异常:

ERR_SM2_ILLEGAL_PRIVATE_KEY: 私钥非法
ERR_ILLEGAL_INPUT: 输入数据
ERR_SM2_DECRYPT_FAILED: 解密失败
调用示例:

NSData * sm2DecBytes1 = [[YiSMCore defaultCore] sm2Decrypt:sm2PrivK andInput:sm2EncBytes];

NSData * sm2DecBytes2 = [[YiSMCore defaultCore] sm2Decrypt:sm2PrivK andInput:sm2CompressedKeyEncBytes];
输出示例:

sm2DecBytes1: 616263
sm2DecBytes2: 616263

5. DES


5.1 DES & 3DES


接口描述:

/**
 * DES ECB算法
 * @param key 密钥,密钥长度64/128/192
 * @param mode 算法模式,ENCRYPT为加密,DECRYPT为解密
 * @param paddingMode 填充模式,支持NO_PADDING及PKCS7_PADDING
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) desCryptECB:(NSData *) key andCryptMode:(YiCryptMode) mode andPaddingMode:(YiPaddingMode) paddingMode andInput:(NSData *) input;

/**
 * DES CBC算法
 * @param key 密钥,密钥长度64/128/192
 * @param iv 向量
 * @param mode 算法模式,ENCRYPT为加密,DECRYPT为解密
 * @param paddingMode 填充模式,支持NO_PADDING及PKCS7_PADDING
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) desCryptCBC:(NSData *) key andIV:(NSData *) iv andCryptMode:(YiCryptMode) mode andPaddingMode:(YiPaddingMode) paddingMode andInput:(NSData *) input;
接口异常:

ERR_ILLEGAL_INPUT: 待计算数据非法,如input为null、长度小于1或长度不满足paddingMode要求
ERR_ILLEGAL_KEY: key为null或长度不等于8/16/24
ERR_ILLEGAL_IV: iv为null或长度不等于8
ERR_DES_CRYPT_FAILED: 加解密失败
调用示例:

// DES ECB NO_PADDING 加密
NSData * desECBNoPaddingEnc = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andCryptMode:ENCRYPT andPaddingMode:NO_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"0123456789ABCDEF"]];

// DES ECB NO_PADDING 解密
NSData * desECBNoPaddingDec = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andCryptMode:DECRYPT andPaddingMode:NO_PADDING andInput:desECBNoPaddingEnc];

// DES ECB PKCS7_PADDING 加密
NSData * desECBPKCS7PaddingEnc = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andCryptMode:ENCRYPT andPaddingMode:PKCS7_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

// DES ECB PKCS7_PADDING 解密
NSData * desECBPKCS7PaddingDec = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andCryptMode:DECRYPT andPaddingMode:PKCS7_PADDING andInput:desECBPKCS7PaddingEnc];

// DES CBC NO_PADDING 加密
NSData * desCBCNoPaddingEnc = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:ENCRYPT andPaddingMode:NO_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"0123456789ABCDEF"]];

// DES CBC NO_PADDING 解密
NSData * desCBCNoPaddingDec = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:DECRYPT andPaddingMode:NO_PADDING andInput:desCBCNoPaddingEnc];

// DES CBC PKCS7_PADDING 加密
NSData * desCBCPKCS7PaddingEnc = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:ENCRYPT andPaddingMode:PKCS7_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

// DES CBC PKCS7_PADDING 解密
NSData * desCBCPKCS7PaddingDec = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:DECRYPT andPaddingMode:PKCS7_PADDING andInput:desCBCPKCS7PaddingEnc];

====================================================================================

// 3DES 128 ECB NO_PADDING 加密
NSData * des128ECBNoPaddingEnc = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andCryptMode:ENCRYPT andPaddingMode:NO_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"0123456789ABCDEF"]];

// 3DES 128 ECB NO_PADDING 解密
NSData * des128ECBNoPaddingDec = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andCryptMode:DECRYPT andPaddingMode:NO_PADDING andInput:des128ECBNoPaddingEnc];

// 3DES 128 ECB PKCS7_PADDING 加密
NSData * des128ECBPKCS7PaddingEnc = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andCryptMode:ENCRYPT andPaddingMode:PKCS7_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

// 3DES 128 ECB PKCS7_PADDING 解密
NSData * des128ECBPKCS7PaddingDec = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andCryptMode:DECRYPT andPaddingMode:PKCS7_PADDING andInput:des128ECBPKCS7PaddingEnc];

// 3DES 128 CBC NO_PADDING 加密
NSData * des128CBCNoPaddingEnc = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:ENCRYPT andPaddingMode:NO_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"0123456789ABCDEF"]];

// 3DES 128 CBC NO_PADDING 解密
NSData * des128CBCNoPaddingDec = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:DECRYPT andPaddingMode:NO_PADDING andInput:des128CBCNoPaddingEnc];

// 3DES 128 CBC PKCS7_PADDING 加密
NSData * des128CBCPKCS7PaddingEnc = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:ENCRYPT andPaddingMode:PKCS7_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

// 3DES 128 CBC PKCS7_PADDING 解密
NSData * des128CBCPKCS7PaddingDec = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:DECRYPT andPaddingMode:PKCS7_PADDING andInput:des128CBCPKCS7PaddingEnc];

====================================================================================

// 3DES 192 ECB NO_PADDING 加密
NSData * des192ECBNoPaddingEnc = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C9869663348730123456789ABCDEF"] andCryptMode:ENCRYPT andPaddingMode:NO_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"0123456789ABCDEF"]];

// 3DES 192 ECB NO_PADDING 解密
NSData * des192ECBNoPaddingDec = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C9869663348730123456789ABCDEF"] andCryptMode:DECRYPT andPaddingMode:NO_PADDING andInput:des192ECBNoPaddingEnc];

// 3DES 192 ECB PKCS7_PADDING 加密
NSData * des192ECBPKCS7PaddingEnc = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C9869663348730123456789ABCDEF"] andCryptMode:ENCRYPT andPaddingMode:PKCS7_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

// 3DES 192 ECB PKCS7_PADDING 解密
NSData * des192ECBPKCS7PaddingDec = [[YiSMCore defaultCore] desCryptECB:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C9869663348730123456789ABCDEF"] andCryptMode:DECRYPT andPaddingMode:PKCS7_PADDING andInput:des192ECBPKCS7PaddingEnc];

// 3DES 192 CBC NO_PADDING 加密
NSData * des192CBCNoPaddingEnc = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C9869663348730123456789ABCDEF"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:ENCRYPT andPaddingMode:NO_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"0123456789ABCDEF"]];

// 3DES 192 CBC NO_PADDING 解密
NSData * des192CBCNoPaddingDec = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C9869663348730123456789ABCDEF"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:DECRYPT andPaddingMode:NO_PADDING andInput:des192CBCNoPaddingEnc];

// 3DES 192 CBC PKCS7_PADDING 加密
NSData * des192CBCPKCS7PaddingEnc = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C9869663348730123456789ABCDEF"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:ENCRYPT andPaddingMode:PKCS7_PADDING andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

// 3DES 192 CBC PKCS7_PADDING 解密
NSData * des192CBCPKCS7PaddingDec = [[YiSMCore defaultCore] desCryptCBC:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C9869663348730123456789ABCDEF"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andCryptMode:DECRYPT andPaddingMode:PKCS7_PADDING andInput:des192CBCPKCS7PaddingEnc];
输出示例:

desECBNoPaddingEnc: 27F35F0D802FA92A
desECBNoPaddingDec: 0123456789ABCDEF

desECBPKCS7PaddingEnc: 2F3CA2F2A2070979
desECBPKCS7PaddingDec: 616263

desCBCNoPaddingEnc: 67DD9EC058F1CA71
desCBCNoPaddingDec: 0123456789ABCDEF

desCBCPKCS7PaddingEnc: C7458F55A7353D4C
desCBCPKCS7PaddingDec: 616263

====================================================================================

des128ECBNoPaddingEnc: E81045B859F79E19
des128ECBNoPaddingDec: 0123456789ABCDEF

des128ECBPKCS7PaddingEnc: 498FB860C1119F46
des128ECBPKCS7PaddingDec: 616263

des128CBCNoPaddingEnc: 2977A4685ED0E0F4
des128CBCNoPaddingDec: 0123456789ABCDEF

des128CBCPKCS7PaddingEnc: D1C1699E2EB6F862
des128CBCPKCS7PaddingDec: 616263

====================================================================================

des192ECBNoPaddingEnc: 7904C58988966DCF
des192ECBNoPaddingDec: 0123456789ABCDEF

des192ECBPKCS7PaddingEnc: 93E5BAD390AE5B2A
des192ECBPKCS7PaddingDec: 616263

des192CBCNoPaddingEnc: E9B4CCD5CBD05645
des192CBCNoPaddingDec: 0123456789ABCDEF

des192CBCPKCS7PaddingEnc: C59561873DCF0E91
des192CBCPKCS7PaddingDec: 616263

5.2 PBOC-(DES/3DES)-MAC


接口描述:

/**
 * PBOC DES & 3DES MAC
 * @param key 密钥,密钥长度64/128/192
 * @param iv 向量
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) pbocDESMac:(NSData *) key andIV:(NSData *) iv andInput:(NSData *) input;
接口异常:

ERR_ILLEGAL_KEY: 密钥非法
ERR_ILLEGAL_IV: 向量非法
ERR_ILLEGAL_INPUT: 输入数据非法
ERR_DES_PBOC_MAC_FAILED: 计算MAC失败
调用示例:

NSData * pbocDESMac = [[YiSMCore defaultCore] pbocDESMac:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

NSData * pboc3DESMac = [[YiSMCore defaultCore] pbocDESMac:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andIV:[[YiSMCore defaultCore] str2Bcd:@"643C986966334873"] andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];
输出示例:

pbocDESMac: 041B7BE33A472DE5
pboc3DESMac: 99CEBE5F74AA4264

5.3 ANSI-(X9.9/X9.19)-MAC


接口描述:

/**
 * ANSI X9.9 MAC算法
 * @param key 密钥,64Bit
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) ansiX9_9Mac:(NSData *) key andInput:(NSData *) input;

/**
 * ANSI X9.19 MAC算法
 * @param key 密钥, 128Bit
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) ansiX9_19Mac:(NSData *) key andInput:(NSData *) input;
接口异常:

ERR_ILLEGAL_KEY: 密钥非法
ERR_ILLEGAL_IV: 向量非法
ERR_ILLEGAL_INPUT: 输入数据非法
ERR_ANSI_MAC_FAILED: 计算MAC失败
调用示例:

NSData * ansiX9_9Mac = [[YiSMCore defaultCore] ansiX9_9Mac:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6"] andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];

NSData * ansiX9_19Mac = [[YiSMCore defaultCore] ansiX9_19Mac:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andInput:[[YiSMCore defaultCore] str2Bcd:@"616263"]];
输出示例:

ansiX9_9Mac: C635A010A858E999
ansiX9_19Mac: F13E7628BC539D7A

5.4 分散算法


接口描述:

/**
 * 分散算法
 * @param key 密钥, 128Bit
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
- (NSData *) diversify:(NSData *) key andInput:(NSData *) input;
接口异常:

ERR_ILLEGAL_KEY: 密钥非法
ERR_ILLEGAL_INPUT: 输入非法
ERR_DES_DIVERSIFY_FAILED: 分散失败
调用示例:

NSData * desDiversify = [[YiSMCore defaultCore] diversify:[[YiSMCore defaultCore] str2Bcd:@"6B8B4567327B23C6643C986966334873"] andInput:[[YiSMCore defaultCore] str2Bcd:@"055EFB6966334873"]];
输出示例:

desDiversify: FFE8F39C6DB592D0F76E75AAEF7416B9