Android SDK 文档

Android端采用效率更高的C语言进行编写封装,大大降低了计算时间。如果采用Java标准库的DES算法来计算PBOC-DES-MAC或 PBOC-3DES-MAC的话,效率太低了,计算200多字节的数据大概要400-600MS,因此我们把这部分算法也放到了C语言中来实现。 SDK中也实现了与公共交通相关的一些算法,为减小SDK大小,并未收录其它更多的算法。SDK中实现的所有算法都是线程安全的。

其中实现了一版基于miracl大数库的版本,算法效率有些低,sm2签名/验签平均都是100ms左右。本文档是基于openssl实现的,效率要高很多。so库大小约600-800KB。

算法效率(测试手机:三星S6,CPU架构:arm64-v8a,测试次数:100000次):

算法 平均耗时/次
sm3 hash 4.1us
sms4 加密 10.1us
sms4 解密 10.0us
whitebox sms4 加密 0.695ms
whitebox sms4 解密 0.696ms
sm2 密钥生成 1.7ms
sm2 从私钥中生成公钥 62us
sm2 公钥压缩 8.4us
sm2 公钥解压 9.1us
sm2 私钥签名 1.06ms
sm2 公钥验签 1.21ms
sm2 公钥加密 1.93ms
sm2 私钥解密 1.134ms
3DES 192 CBC NoPadding加密 0.113ms
3DES 192 CBC NoPadding解密 0.113ms
3DES PBOC Mac 0.111ms
ANSI X9.9 Mac 46.77us
ANSI X9.19 Mac 0.120ms
DES 分散算法 0.179ms

混淆参数:

-keep class net.yiim.yismcore.** {*;}

1. 国密SM3


接口描述:

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

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

byte[] sm3Hash = YiSMCore.getInstance().sm3Hash(
        YiSMCore.getInstance().str2Bcd("616263", 6));
输出示例:

sm3Hash: 66C7F0F462EEEDD9D1F2D46BDC10E4E24167C4875CF2F7A2297DA02B8F4BA8E0

2. 国密SMS4


接口描述:

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

/**
 * 国密SMS4 CBC算法
 * @param key 密钥
 * @param iv 向量
 * @param mode 算法模式,ENCRYPT为加密,DECRYPT为解密
 * @param paddingMode 填充模式,支持NO_PADDING及PKCS7_PADDING
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
public byte[] sms4CryptCBC(byte[] key, byte[] iv, YiCryptMode mode, YiPaddingMode paddingMode, byte[] input) throws 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_PADDING 加密
byte[] sms4ECBNoPaddingEnc = YiSMCore.getInstance().sms4CryptECB(
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiCryptMode.ENCRYPT, YiPaddingMode.NO_PADDING,
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32));

// ECB NO_PADDING 解密
byte[] sms4ECBNoPaddingDec = YiSMCore.getInstance().sms4CryptECB(
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiCryptMode.DECRYPT, YiPaddingMode.NO_PADDING, sms4ECBNoPaddingEnc);

// ECB PKCS7_PADDING 加密
byte[] sms4ECBPKCS7PaddingEnc = YiSMCore.getInstance().sms4CryptECB(
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiCryptMode.ENCRYPT, YiPaddingMode.PKCS7_PADDING,
        YiSMCore.getInstance().str2Bcd("616263", 6));

// ECB PKCS7_PADDING 解密
byte[] sms4ECBPKCS7PaddingDec = YiSMCore.getInstance().sms4CryptECB(
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiCryptMode.DECRYPT, YiPaddingMode.PKCS7_PADDING, sms4ECBPKCS7PaddingEnc);

// CBC NO_PADDING 加密
byte[] sms4CBCNoPaddingEnc = YiSMCore.getInstance().sms4CryptCBC(
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiCryptMode.ENCRYPT, YiPaddingMode.NO_PADDING,
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32));

// CBC NO_PADDING 解密
byte[] sms4CBCNoPaddingDec = YiSMCore.getInstance().sms4CryptCBC(
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiCryptMode.DECRYPT, YiPaddingMode.NO_PADDING, sms4CBCNoPaddingEnc);

// CBC PKCS7_PADDING 加密
byte[] sms4CBCPKCS7PaddingEnc = YiSMCore.getInstance().sms4CryptCBC(
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiCryptMode.ENCRYPT, YiPaddingMode.PKCS7_PADDING,
        YiSMCore.getInstance().str2Bcd("616263", 6));

// CBC PKCS7_PADDING 解密
byte[] sms4CBCPKCS7PaddingDec = YiSMCore.getInstance().sms4CryptCBC(
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiSMCore.getInstance().str2Bcd("0123456789abcdeffedcba9876543210", 32),
        YiCryptMode.DECRYPT, YiPaddingMode.PKCS7_PADDING, 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 计算出错时抛出异常
 */
public YiSM2KeyPair sm2GenerateKeyPair() throws YiCryptException;
接口异常:

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

YiSM2KeyPair sm2KeyPair = YiSMCore.getInstance().sm2GenerateKeyPair();
byte[] sm2PrivKey = sm2KeyPair.getPrivKey();
// PC||x||y,其中PC为单一字节,且PC = 04
byte[] sm2PubKey = sm2KeyPair.getPubKey();
// yTilde||x
byte[] sm2CompressedPubKey = sm2KeyPair.getCompressedPubKey();

YiSM2KeyPair mSM2PrivKey = YiSM2KeyPair.createByPrivateKey(sm2PrivKey);
YiSM2KeyPair mSM2PubKey = YiSM2KeyPair.createByPublicKey(sm2PubKey);
YiSM2KeyPair mSM2ComPubKey = YiSM2KeyPair.createByCompressedPublicKey(sm2CompressedPubKey);
输出示例:

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

4.1.2 SM2 私钥生成公钥


接口描述:

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

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

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

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

sm2PubKey: 040148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B12F58B1E8A661EBF3395459F28945D381259BEEDA76B4886FABF5EE0A55ADEEB2
sm2CompressedPubKey: 020148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B1

4.1.3 SM2 公钥压缩


接口描述:

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

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

byte[] sm2CompressedPubKey = YiSMCore.getInstance().sm2CompressPubKey(mSM2PubKey);
输出示例:

sm2CompressedPubKey: 020148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B1

4.1.4 SM2 公钥解压


接口描述:

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

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

byte[] sm2PubKey = YiSMCore.getInstance().sm2DecompressPubKey(mSM2ComPubKey);
输出示例:

sm2PubKey: 040148E6AF89A0E132E4E7CDA26DF2C2AEB53B741FD00AE85C78CF6EBA13E939B12F58B1E8A661EBF3395459F28945D381259BEEDA76B4886FABF5EE0A55ADEEB2

4.2 SM2 私钥签名


接口描述:

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

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

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

byte[] signResult1 = YiSMCore.getInstance().sm2Sign(mSM2PrivKey,
        YiSMCore.getInstance().str2Bcd("616263", 6));

byte[] 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 计算出错时抛出异常
 */
public boolean sm2Verify(YiSM2KeyPair pubKey, byte[] userId, byte[] input, byte[] signData) throws YiCryptException;

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

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

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

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

boolean sm2VerifyResult3 = YiSMCore.getInstance().sm2Verify(mSM2ComPubKey,
        YiSMCore.getInstance().str2Bcd("616263", 6), signResult1);

boolean sm2VerifyResult4 = 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 计算出错时抛出异常
 */
public byte[] sm2Encrypt(YiSM2KeyPair pubKey, byte[] input) throws YiCryptException;
接口异常:

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

byte[] sm2EncBytes = YiSMCore.getInstance().sm2Encrypt(mSM2PubKey,
        YiSMCore.getInstance().str2Bcd("616263", 6));

byte[] 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 计算出错时抛出异常
 */
public byte[] sm2Decrypt(YiSM2KeyPair privKey, byte[] input) throws YiCryptException;
接口异常:

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

byte[] sm2DecBytes1 = YiSMCore.getInstance().sm2Decrypt(mSM2PrivKey, sm2EncBytes);
byte[] 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_PADDING及PKCS7_PADDING
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
public byte[] desCryptECB(byte[] key, YiCryptMode mode, YiPaddingMode paddingMode, byte[] input) throws YiCryptException;

/**
 * DES CBC算法
 * @param key 密钥,密钥长度64/128/192
 * @param iv 向量
 * @param mode 算法模式,ENCRYPT为加密,DECRYPT为解密
 * @param paddingMode 填充模式,支持NO_PADDING及PKCS7_PADDING
 * @param input 输入数据
 * @return 输出计算结果
 * @throws YiCryptException 计算出错时抛出异常
 */
public byte[] desCryptCBC(byte[] key, byte[] iv, YiCryptMode mode, YiPaddingMode paddingMode, byte[] input) throws 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_PADDING 加密
byte[] desECBNoPaddingEnc = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiCryptMode.ENCRYPT, YiPaddingMode.NO_PADDING,
        YiSMCore.getInstance().str2Bcd("0123456789ABCDEF"));

// DES ECB NO_PADDING 解密
byte[] desECBNoPaddingDec = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiCryptMode.DECRYPT, YiPaddingMode.NO_PADDING, desECBNoPaddingEnc);

// DES ECB PKCS7_PADDING 加密
byte[] desECBPKCS7PaddingEnc = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiCryptMode.ENCRYPT, YiPaddingMode.PKCS7_PADDING,
        YiSMCore.getInstance().str2Bcd("616263"));

// DES ECB PKCS7_PADDING 解密
byte[] desECBPKCS7PaddingDec = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiCryptMode.DECRYPT, YiPaddingMode.PKCS7_PADDING, desECBPKCS7PaddingEnc);

// DES CBC NO_PADDING 加密
byte[] desCBCNoPaddingEnc = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.ENCRYPT, YiPaddingMode.NO_PADDING,
        YiSMCore.getInstance().str2Bcd("0123456789ABCDEF"));

// DES CBC NO_PADDING 解密
byte[] desCBCNoPaddingDec = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.DECRYPT, YiPaddingMode.NO_PADDING, desCBCNoPaddingEnc);

// DES CBC PKCS7_PADDING 加密
byte[] desCBCPKCS7PaddingEnc = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.ENCRYPT, YiPaddingMode.PKCS7_PADDING,
        YiSMCore.getInstance().str2Bcd("616263"));

// DES CBC PKCS7_PADDING 解密
byte[] desCBCPKCS7PaddingDec = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.DECRYPT, YiPaddingMode.PKCS7_PADDING, desCBCPKCS7PaddingEnc);

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

// 3DES 128 ECB NO_PADDING 加密
byte[] des128ECBNoPaddingEnc = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
        YiCryptMode.ENCRYPT, YiPaddingMode.NO_PADDING,
        YiSMCore.getInstance().str2Bcd("0123456789ABCDEF"));

// 3DES 128 ECB NO_PADDING 解密
byte[] des128ECBNoPaddingDec = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
        YiCryptMode.DECRYPT, YiPaddingMode.NO_PADDING, des128ECBNoPaddingEnc);

// 3DES 128 ECB PKCS7_PADDING 加密
byte[] des128ECBPKCS7PaddingEnc = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
        YiCryptMode.ENCRYPT, YiPaddingMode.PKCS7_PADDING,
        YiSMCore.getInstance().str2Bcd("616263"));

// 3DES 128 ECB PKCS7_PADDING 解密
byte[] des128ECBPKCS7PaddingDec = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
        YiCryptMode.DECRYPT, YiPaddingMode.PKCS7_PADDING, des128ECBPKCS7PaddingEnc);

// 3DES 128 CBC NO_PADDING 加密
byte[] des128CBCNoPaddingEnc = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.ENCRYPT, YiPaddingMode.NO_PADDING,
        YiSMCore.getInstance().str2Bcd("0123456789ABCDEF"));

// 3DES 128 CBC NO_PADDING 解密
byte[] des128CBCNoPaddingDec = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.DECRYPT, YiPaddingMode.NO_PADDING, des128CBCNoPaddingEnc);

// 3DES 128 CBC PKCS7_PADDING 加密
byte[] des128CBCPKCS7PaddingEnc = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.ENCRYPT, YiPaddingMode.PKCS7_PADDING,
        YiSMCore.getInstance().str2Bcd("616263"));

// 3DES 128 CBC PKCS7_PADDING 解密
byte[] des128CBCPKCS7PaddingDec = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C986966334873"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.DECRYPT, YiPaddingMode.PKCS7_PADDING, des128CBCPKCS7PaddingEnc);

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

// 3DES 192 ECB NO_PADDING 加密
byte[] des192ECBNoPaddingEnc = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
        YiCryptMode.ENCRYPT, YiPaddingMode.NO_PADDING,
        YiSMCore.getInstance().str2Bcd("0123456789ABCDEF"));

// 3DES 192 ECB NO_PADDING 解密
byte[] des192ECBNoPaddingDec = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
        YiCryptMode.DECRYPT, YiPaddingMode.NO_PADDING, des192ECBNoPaddingEnc);

// 3DES 192 ECB PKCS7_PADDING 加密
byte[] des192ECBPKCS7PaddingEnc = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
        YiCryptMode.ENCRYPT, YiPaddingMode.PKCS7_PADDING,
        YiSMCore.getInstance().str2Bcd("616263"));

// 3DES 192 ECB PKCS7_PADDING 解密
byte[] des192ECBPKCS7PaddingDec = YiSMCore.getInstance().desCryptECB(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
        YiCryptMode.DECRYPT, YiPaddingMode.PKCS7_PADDING, des192ECBPKCS7PaddingEnc);

// 3DES 192 CBC NO_PADDING 加密
byte[] des192CBCNoPaddingEnc = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.ENCRYPT, YiPaddingMode.NO_PADDING,
        YiSMCore.getInstance().str2Bcd("0123456789ABCDEF"));

// 3DES 192 CBC NO_PADDING 解密
byte[] des192CBCNoPaddingDec = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.DECRYPT, YiPaddingMode.NO_PADDING, des192CBCNoPaddingEnc);

// 3DES 192 CBC PKCS7_PADDING 加密
byte[] des192CBCPKCS7PaddingEnc = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.ENCRYPT, YiPaddingMode.PKCS7_PADDING,
        YiSMCore.getInstance().str2Bcd("616263"));

// 3DES 192 CBC PKCS7_PADDING 解密
byte[] des192CBCPKCS7PaddingDec = YiSMCore.getInstance().desCryptCBC(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6643C9869663348730123456789ABCDEF"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiCryptMode.DECRYPT, YiPaddingMode.PKCS7_PADDING, 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 计算出错时抛出异常
 */
public byte[] pbocDESMac(byte[] key, byte[] iv, byte[] input) throws YiCryptException;
接口异常:

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

byte[] pbocDESMac = YiSMCore.getInstance().pbocDESMac(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiSMCore.getInstance().str2Bcd("643C986966334873"),
        YiSMCore.getInstance().str2Bcd("616263"));

byte[] 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 计算出错时抛出异常
 */
public byte[] ansiX9_9Mac(byte[] key, byte[] input) throws YiCryptException;

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

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

byte[] ansiX9_9Mac = YiSMCore.getInstance().ansiX9_9Mac(
        YiSMCore.getInstance().str2Bcd("6B8B4567327B23C6"),
        YiSMCore.getInstance().str2Bcd("616263"));

byte[] 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 计算出错时抛出异常
 */
public byte[] diversify(byte[] key, byte[] input) throws YiCryptException;
接口异常:

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

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

desDiversify: FFE8F39C6DB592D0F76E75AAEF7416B9