C/C++ SDK 文档

1. 国密SM3


接口描述:

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

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

string sm3Hash = YiSMCore::getInstance().sm3Hash(YiSMCore::getInstance().str2Bcd("616263"));
输出示例:

sm3Hash: 66C7F0F462EEEDD9D1F2D46BDC10E4E24167C4875CF2F7A2297DA02B8F4BA8E0

2. 国密SMS4


接口描述:

/**
 * 国密SMS4 ECB算法
 * @param key 密钥
 * @param mode 算法模式,ENCRYPT为加密,DECRYPT为解密
 * @param padding_mode 填充模式,支持NO_PAD及PAD_PKCS_7
 * @param in 输入数据
 * @return 计算结果,最大为输入长度+16字节
 * @throws YiCryptException 计算出错时抛出异常。
 */
std::string sms4CryptECB(const std::string & key, YiCryptMode mode, YiPaddingMode padding_mode,
    const std::string & in) throw (YiCryptException);

/**
 * 国密SMS4 ECB算法
 * @param key 密钥
 * @param iv 向量
 * @param mode 算法模式,ENCRYPT为加密,DECRYPT为解密
 * @param padding_mode 填充模式,支持NO_PAD及PAD_PKCS_7
 * @param in 输入数据
 * @return 计算结果,最大为输入长度+16字节
 * @throws YiCryptException 计算出错时抛出异常。
 */
std::string sms4CryptCBC(const std::string & key, const std::string & iv,
    YiCryptMode mode, YiPaddingMode padding_mode,
    const std::string & in) throw (YiCryptException);
接口异常:

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_PAD 加密
string sms4ECBNoPaddingEnc = YiSMCore::getInstance().sms4CryptECB(
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    ENCRYPT, NO_PAD,
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"));

// ECB NO_PAD 解密
string sms4ECBNoPaddingDec = YiSMCore::getInstance().sms4CryptECB(
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    DECRYPT, NO_PAD, sms4ECBNoPaddingEnc);

// ECB PAD_PKCS_7 加密
string sms4ECBPKCS7PaddingEnc = YiSMCore::getInstance().sms4CryptECB(
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    ENCRYPT, PAD_PKCS_7,
    YiSMCore::getInstance().str2Bcd("616263"));

// ECB PAD_PKCS_7 解密
string sms4ECBPKCS7PaddingDec = YiSMCore::getInstance().sms4CryptECB(
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    DECRYPT, PAD_PKCS_7, sms4ECBPKCS7PaddingEnc);

// CBC NO_PAD 加密
string sms4CBCNoPaddingEnc = YiSMCore::getInstance().sms4CryptCBC(
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    ENCRYPT, NO_PAD,
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"));

// CBC NO_PAD 解密
string sms4CBCNoPaddingDec = YiSMCore::getInstance().sms4CryptCBC(
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    DECRYPT, NO_PAD, sms4CBCNoPaddingEnc);

// CBC PAD_PKCS_7 加密
string sms4CBCPKCS7PaddingEnc = YiSMCore::getInstance().sms4CryptCBC(
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    ENCRYPT, PAD_PKCS_7,
    YiSMCore::getInstance().str2Bcd("616263"));

// CBC PAD_PKCS_7 解密
string sms4CBCPKCS7PaddingDec = YiSMCore::getInstance().sms4CryptCBC(
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    YiSMCore::getInstance().str2Bcd("0123456789abcdeffedcba9876543210"),
    DECRYPT, PAD_PKCS_7, 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密钥对
 * @param keyPair SM2密钥对
 * @throws YiCryptException 计算出错时抛出异常
 */
void sm2GenerateKeyPair(YiSM2KeyPair & keyPair) throw (YiCryptException);
接口异常:

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

YiSM2KeyPair mSM2KeyPair;
YiSMCore::getInstance().sm2GenerateKeyPair(mSM2KeyPair);

const string & sm2PrivKey = mSM2KeyPair.getPrivKey();
// PC||x||y,其中PC为单一字节,且PC = 04
const string & sm2PubKey = mSM2KeyPair.getPubKey();
// yTilde||x
const string & sm2CompressedPubKey = mSM2KeyPair.getCompressedPubKey();

YiSM2KeyPair mSM2PrivKey;
YiSM2KeyPair mSM2PubKey;
YiSM2KeyPair mSM2ComPubKey;

mSM2PrivKey.setupByPrivateKey(sm2PrivKey);
mSM2PubKey.setupByPublicKey(sm2PubKey);
mSM2ComPubKey.setupByCompressedPublicKey(sm2CompressedPubKey);
输出示例:

sm2PrivKey: 6B8B4567327B23C6643C98696633487374B0DC5119495CFF2AE8944A625558EC
sm2PubKey: 040148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B12F58B1E8A661EBF3395459F28945D381259BEEDA76B4886FABF5EE0A55ADEEB2
sm2CompressedPubKey: 020148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B1

4.1.2 SM2 私钥生成公钥


接口描述:

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

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

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

string sm2PubKey = YiSMCore::getInstance().sm2GetPubKey(mSM2PrivKey);
string sm2CompressedPubKey = YiSMCore::getInstance().sm2GetCompressedPubKey(mSM2PrivKey);
输出示例:

sm2PubKey: 040148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B12F58B1E8A661EBF3395459F28945D381259BEEDA76B4886FABF5EE0A55ADEEB2
sm2CompressedPubKey: 020148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B1

4.1.3 SM2 公钥压缩


接口描述:

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

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

string sm2CompressedPubKey = YiSMCore::getInstance().sm2CompressPubKey(mSM2PubKey);
输出示例:

sm2CompressedPubKey: 020148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B1

4.1.4 SM2 公钥解压


接口描述:

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

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

string sm2PubKey = YiSMCore::getInstance().sm2DecompressPubKey(mSM2ComPubKey);
输出示例:

sm2PubKey: 040148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B12F58B1E8A661EBF3395459F28945D381259BEEDA76B4886FABF5EE0A55ADEEB2

4.2 SM2 私钥签名


接口描述:

/**
 * 国密SM2,私钥签名
 * @param privKey 私钥
 * @param userId user id,长度为16字节
 * @param input 输入数据
 * @return 签名结果r||s
 * @throws YiCryptException 计算出错时抛出异常
 */
std::string sm2Sign(const YiSM2KeyPair & privKey, const std::string & userId,
    const std::string & input) throw (YiCryptException);

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

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

string signResult1 = YiSMCore::getInstance().sm2Sign(mSM2PrivKey,
    YiSMCore::getInstance().str2Bcd("616263", 6));

string signResult2 = YiSMCore::getInstance().sm2Sign(mSM2PrivKey,
    "1234567812345678",
    YiSMCore::getInstance().str2Bcd("616263", 6));
输出示例:

sm2SignResult1: 15B81E50366C3F57EEDC13DF6F24F518BEF8B785AF47427F56E340CA1003AB0FA75540CB508035737B64335255C94E6D51D9698A8CB901CCF32668EC15F9EEAC
sm2SignResult2: E1B6EF02EE8B988AB8438632744C50C41CE4042E0B2C8BE9D46E6C51CCA3796B3E78020929E1C773FCBD3B9333E47156EAC96CF2465C11B4EA416AF08BFA2FD4

4.3 SM2 公钥验签


接口描述:

/**
 * 国密SM2,公钥验签
 * @param pubKey SM2公钥或SM2压缩公钥
 * @param userId user id,长度需小于127字节
 * @param input 输入数据
 * @param signData 待验签名r||s
 * @return 验签结果,true验签成功
 * @throws YiCryptException 计算出错时抛出异常
 */
bool sm2Verify(const YiSM2KeyPair & pubKey, const std::string & userId,
    const std::string & input, const std::string & signData) throw (YiCryptException);

/**
 * 国密SM2,公钥验签,采用默认的userId:1234567812345678
 * @param pubKey SM2公钥或SM2压缩公钥
 * @param input 输入数据
 * @param signData 待验签名r||s
 * @return 验签结果,true验签成功
 * @throws YiCryptException 计算出错时抛出异常
 */
bool sm2Verify(const YiSM2KeyPair & pubKey, const std::string & input,
    const std::string & signData) throw (YiCryptException);
接口异常:

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

bool sm2VerifyResult1 = YiSMCore::getInstance().sm2Verify(mSM2PubKey,
    YiSMCore::getInstance().str2Bcd("616263", 6), signResult1);

bool sm2VerifyResult2 = YiSMCore::getInstance().sm2Verify(mSM2PubKey,
    "1234567812345678",
    YiSMCore::getInstance().str2Bcd("616263", 6), signResult2);

bool sm2VerifyResult1 = YiSMCore::getInstance().sm2Verify(mSM2ComPubKey,
    YiSMCore::getInstance().str2Bcd("616263", 6), signResult1);

bool sm2VerifyResult2 = YiSMCore::getInstance().sm2Verify(mSM2ComPubKey,
    "1234567812345678",
    YiSMCore::getInstance().str2Bcd("616263", 6), signResult2);
输出示例:

sm2VerifyResult1: true
sm2VerifyResult2: true
sm2VerifyResult3: true
sm2VerifyResult4: true

4.4 SM2 公钥加密


接口描述:

/**
 * 国密SM2,公钥加密
 * @param pubKey SM2公钥或SM2压缩公钥
 * @param input 输入数据
 * @return 输出C1||C2||C3,其中C1=PC||x1||y1,C2=密文,C3=sm3_hash
 * @throws YiCryptException 计算出错时抛出异常
 */
std::string sm2Encrypt(const YiSM2KeyPair & pubKey,
    const std::string & input) throw (YiCryptException);
接口异常:

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

string sm2EncBytes = YiSMCore::getInstance().sm2Encrypt(mSM2PubKey,
    YiSMCore::getInstance().str2Bcd("616263", 6));

string sm2CompressedKeyEncBytes = YiSMCore::getInstance().sm2Encrypt(mSM2ComPubKey,
    YiSMCore::getInstance().str2Bcd("616263", 6));
输出示例:

sm2EncBytes: 04EB04AAE0D53FBA1E3611D5B9ED6EFA3EE5BA57C41AA7A09DDC5816AF09057757CE6FA0678392F4716E45F58E7322C76D5997B1FE44C36D8A5A59B146EE162B934C89B77B13DD1DCB76BF6C2F4701876D6673807FACE8696A8D5FD24A473A9DAFC8A44E
sm2CompressedKeyEncBytes: 04511BD81F96C1289DFE360A42AF7AA310FE28BE962CE78D1A3EB9ECAC433372A090FCFE360B22F20385112EA408E3F2D2DD6CB663174C713C88974A917BC6B89B82BEE890D1404E21F12DB72694E601D211B8A93B00240063C6D39347C18CD7A6517C79

4.5 SM2 私钥解密


接口描述:

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

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

string sm2DecBytes1 = YiSMCore::getInstance().sm2Decrypt(mSM2PrivKey, sm2EncBytes);
string sm2DecBytes2 = YiSMCore::getInstance().sm2Decrypt(mSM2PrivKey, 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_PAD及PAD_PKCS_7
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
std::string desCryptECB(const std::string & key, YiCryptMode mode,
    YiPaddingMode paddingMode, const std::string & input) throw (YiCryptException);

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

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_PAD 加密
string desECBNoPaddingEnc = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    ENCRYPT, NO_PAD,
    YiSMCore::getInstance().str2Bcd("0123456789ABCDEF"));

// DES ECB NO_PAD 解密
string desECBNoPaddingDec = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    DECRYPT, NO_PAD, desECBNoPaddingEnc);

// DES ECB PAD_PKCS_7 加密
string desECBPKCS7PaddingEnc = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    ENCRYPT, PAD_PKCS_7,
    YiSMCore::getInstance().str2Bcd("616263"));

// DES ECB PAD_PKCS_7 解密
string desECBPKCS7PaddingDec = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    DECRYPT, PAD_PKCS_7, desECBPKCS7PaddingEnc);

// DES CBC NO_PAD 加密
string desCBCNoPaddingEnc = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    ENCRYPT, NO_PAD,
    YiSMCore::getInstance().str2Bcd("0123456789ABCDEF"));

// DES CBC NO_PAD 解密
string desCBCNoPaddingDec = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    DECRYPT, NO_PAD, desCBCNoPaddingEnc);

// DES CBC PAD_PKCS_7 加密
string desCBCPKCS7PaddingEnc = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    ENCRYPT, PAD_PKCS_7,
    YiSMCore::getInstance().str2Bcd("616263"));

// DES CBC PAD_PKCS_7 解密
string desCBCPKCS7PaddingDec = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    DECRYPT, PAD_PKCS_7, desCBCPKCS7PaddingEnc);

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

// 3DES 128 ECB NO_PAD 加密
string des128ECBNoPaddingEnc = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    ENCRYPT, NO_PAD,
    YiSMCore::getInstance().str2Bcd("0123456789ABCDEF"));

// 3DES 128 ECB NO_PAD 解密
string des128ECBNoPaddingDec = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    DECRYPT, NO_PAD, des128ECBNoPaddingEnc);

// 3DES 128 ECB PAD_PKCS_7 加密
string des128ECBPKCS7PaddingEnc = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    ENCRYPT, PAD_PKCS_7,
    YiSMCore::getInstance().str2Bcd("616263"));

// 3DES 128 ECB PAD_PKCS_7 解密
string des128ECBPKCS7PaddingDec = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    DECRYPT, PAD_PKCS_7, des128ECBPKCS7PaddingEnc);

// 3DES 128 CBC NO_PAD 加密
string des128CBCNoPaddingEnc = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    ENCRYPT, NO_PAD,
    YiSMCore::getInstance().str2Bcd("0123456789ABCDEF"));

// 3DES 128 CBC NO_PAD 解密
string des128CBCNoPaddingDec = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    DECRYPT, NO_PAD, des128CBCNoPaddingEnc);

// 3DES 128 CBC PAD_PKCS_7 加密
string des128CBCPKCS7PaddingEnc = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    ENCRYPT, PAD_PKCS_7,
    YiSMCore::getInstance().str2Bcd("616263"));

// 3DES 128 CBC PAD_PKCS_7 解密
string des128CBCPKCS7PaddingDec = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    DECRYPT, PAD_PKCS_7, des128CBCPKCS7PaddingEnc);

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

// 3DES 192 ECB NO_PAD 加密
string des192ECBNoPaddingEnc = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
    ENCRYPT, NO_PAD,
    YiSMCore::getInstance().str2Bcd("0123456789ABCDEF"));

// 3DES 192 ECB NO_PAD 解密
string des192ECBNoPaddingDec = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
    DECRYPT, NO_PAD, des192ECBNoPaddingEnc);

// 3DES 192 ECB PAD_PKCS_7 加密
string des192ECBPKCS7PaddingEnc = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
    ENCRYPT, PAD_PKCS_7,
    YiSMCore::getInstance().str2Bcd("616263"));

// 3DES 192 ECB PAD_PKCS_7 解密
string des192ECBPKCS7PaddingDec = YiSMCore::getInstance().desCryptECB(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
    DECRYPT, PAD_PKCS_7, des192ECBPKCS7PaddingEnc);

// 3DES 192 CBC NO_PAD 加密
string des192CBCNoPaddingEnc = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    ENCRYPT, NO_PAD,
    YiSMCore::getInstance().str2Bcd("0123456789ABCDEF"));

// 3DES 192 CBC NO_PAD 解密
string des192CBCNoPaddingDec = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    DECRYPT, NO_PAD, des192CBCNoPaddingEnc);

// 3DES 192 CBC PAD_PKCS_7 加密
string des192CBCPKCS7PaddingEnc = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    ENCRYPT, PAD_PKCS_7,
    YiSMCore::getInstance().str2Bcd("616263"));

// 3DES 192 CBC PAD_PKCS_7 解密
string des192CBCPKCS7PaddingDec = YiSMCore::getInstance().desCryptCBC(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    DECRYPT, PAD_PKCS_7, 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 计算出错时抛出异常
 */
std::string pbocDESMac(const std::string & key, const std::string & iv,
    const std::string & input) throw (YiCryptException);
接口异常:

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

string pbocDESMac = YiSMCore::getInstance().pbocDESMac(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    YiSMCore::getInstance().str2Bcd("616263"));

string pboc3DESMac = YiSMCore::getInstance().pbocDESMac(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    YiSMCore::getInstance().str2Bcd("643C986966334873"),
    YiSMCore::getInstance().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 计算出错时抛出异常
 */
std::string ansiX9_9Mac(const std::string & key,
    const std::string & input) throw (YiCryptException);

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

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

string ansiX9_9Mac = YiSMCore::getInstance().ansiX9_9Mac(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6"),
    YiSMCore::getInstance().str2Bcd("616263"));

string ansiX9_19Mac = YiSMCore::getInstance().ansiX9_19Mac(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    YiSMCore::getInstance().str2Bcd("616263"));
输出示例:

ansiX9_9Mac: C635A010A858E999
ansiX9_19Mac: F13E7628BC539D7A

5.4 分散算法


接口描述:

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

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

string desDiversify = YiSMCore::getInstance().diversify(
    YiSMCore::getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
    YiSMCore::getInstance().str2Bcd("055EFB6966334873"));
输出示例:

desDiversify: FFE8F39C6DB592D0F76E75AAEF7416B9