fid Idcard Code

This commit is contained in:
Looly
2019-09-28 02:09:31 +08:00
parent 32955b9c8c
commit daf85caf9e
20 changed files with 546 additions and 351 deletions

View File

@@ -1,10 +1,14 @@
package cn.hutool.crypto;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.hutool.crypto.digest.SM3;
import cn.hutool.crypto.digest.mac.BCHMacEngine;
import cn.hutool.crypto.digest.mac.MacEngine;
import cn.hutool.crypto.symmetric.SM4;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Sequence;
@@ -14,14 +18,10 @@ import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.hutool.crypto.digest.mac.BCHMacEngine;
import cn.hutool.crypto.digest.mac.MacEngine;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
/**
* SM国密算法工具类<br>
@@ -34,9 +34,6 @@ public class SmUtil {
private final static int RS_LEN = 32;
private static String SM3 = "SM3";
private static String SM4 = "SM4";
/**
* 创建SM2算法对象<br>
* 生成新的私钥公钥对
@@ -79,10 +76,10 @@ public class SmUtil {
* SM3加密sm3().digest(data)<br>
* SM3加密并转为16进制字符串sm3().digestHex(data)<br>
*
* @return {@link Digester}
* @return {@link SM3}
*/
public static Digester sm3() {
return new Digester(SM3);
public static SM3 sm3() {
return new SM3();
}
/**
@@ -92,7 +89,7 @@ public class SmUtil {
* @return SM3字符串
*/
public static String sm3(String data) {
return new Digester(SM3).digestHex(data);
return sm3().digestHex(data);
}
/**
@@ -102,7 +99,7 @@ public class SmUtil {
* @return SM3字符串
*/
public static String sm3(InputStream data) {
return new Digester(SM3).digestHex(data);
return sm3().digestHex(data);
}
/**
@@ -112,7 +109,7 @@ public class SmUtil {
* @return SM3字符串
*/
public static String sm3(File dataFile) {
return new Digester(SM3).digestHex(dataFile);
return sm3().digestHex(dataFile);
}
/**
@@ -126,8 +123,8 @@ public class SmUtil {
*
* @return {@link SymmetricCrypto}
*/
public static SymmetricCrypto sm4() {
return new SymmetricCrypto(SM4);
public static SM4 sm4() {
return new SM4();
}
/**
@@ -143,7 +140,7 @@ public class SmUtil {
* @return {@link SymmetricCrypto}
*/
public static SymmetricCrypto sm4(byte[] key) {
return new SymmetricCrypto(SM4, key);
return new SM4(key);
}
/**

View File

@@ -15,17 +15,24 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.CryptoException;
import cn.hutool.crypto.SecureUtil;
/**
* 抽象的非对称加密对象包装了加密和解密为Hex和Base64的封装
*
* @param <T> 返回自身类型
* @author Looly
*/
public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypto<T>> extends BaseAsymmetric<T> {
// ------------------------------------------------------------------ Constructor start
/**
* 构造
*
* <p>
* 私钥和公钥同时为空时生成一对新的私钥和公钥<br>
* 私钥和公钥可以单独传入一个,如此则只能使用此钥匙来做加密或者解密
*
* @param algorithm 算法
*
* @param algorithm 算法
* @param privateKey 私钥
* @param publicKey 公钥
* @param publicKey 公钥
* @since 3.1.1
*/
public AbstractAsymmetricCrypto(String algorithm, PrivateKey privateKey, PublicKey publicKey) {
@@ -34,10 +41,11 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
// ------------------------------------------------------------------ Constructor end
// --------------------------------------------------------------------------------- Encrypt
/**
* 加密
*
* @param data 被加密的bytes
*
* @param data 被加密的bytes
* @param keyType 私钥或公钥 {@link KeyType}
* @return 加密后的bytes
*/
@@ -45,8 +53,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 编码为Hex字符串
*
* @param data 被加密的bytes
*
* @param data 被加密的bytes
* @param keyType 私钥或公钥 {@link KeyType}
* @return Hex字符串
*/
@@ -56,8 +64,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 编码为Base64字符串
*
* @param data 被加密的bytes
*
* @param data 被加密的bytes
* @param keyType 私钥或公钥 {@link KeyType}
* @return Base64字符串
* @since 4.0.1
@@ -68,8 +76,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 加密
*
* @param data 被加密的字符串
*
* @param data 被加密的字符串
* @param charset 编码
* @param keyType 私钥或公钥 {@link KeyType}
* @return 加密后的bytes
@@ -80,8 +88,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 加密
*
* @param data 被加密的字符串
*
* @param data 被加密的字符串
* @param charset 编码
* @param keyType 私钥或公钥 {@link KeyType}
* @return 加密后的bytes
@@ -92,8 +100,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 加密使用UTF-8编码
*
* @param data 被加密的字符串
*
* @param data 被加密的字符串
* @param keyType 私钥或公钥 {@link KeyType}
* @return 加密后的bytes
*/
@@ -103,8 +111,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 编码为Hex字符串
*
* @param data 被加密的字符串
*
* @param data 被加密的字符串
* @param keyType 私钥或公钥 {@link KeyType}
* @return Hex字符串
* @since 4.0.1
@@ -115,8 +123,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 编码为Hex字符串
*
* @param data 被加密的bytes
*
* @param data 被加密的bytes
* @param charset 编码
* @param keyType 私钥或公钥 {@link KeyType}
* @return Hex字符串
@@ -128,8 +136,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 编码为Base64字符串使用UTF-8编码
*
* @param data 被加密的字符串
*
* @param data 被加密的字符串
* @param keyType 私钥或公钥 {@link KeyType}
* @return Base64字符串
* @since 4.0.1
@@ -140,8 +148,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 编码为Base64字符串
*
* @param data 被加密的字符串
*
* @param data 被加密的字符串
* @param keyType 私钥或公钥 {@link KeyType}
* @return Base64字符串
* @since 4.0.1
@@ -152,8 +160,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 加密
*
* @param data 被加密的数据流
*
* @param data 被加密的数据流
* @param keyType 私钥或公钥 {@link KeyType}
* @return 加密后的bytes
* @throws IORuntimeException IO异常
@@ -164,8 +172,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 编码为Hex字符串
*
* @param data 被加密的数据流
*
* @param data 被加密的数据流
* @param keyType 私钥或公钥 {@link KeyType}
* @return Hex字符串
* @since 4.0.1
@@ -176,8 +184,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 编码为Base64字符串
*
* @param data 被加密的数据流
*
* @param data 被加密的数据流
* @param keyType 私钥或公钥 {@link KeyType}
* @return Base64字符串
* @since 4.0.1
@@ -188,8 +196,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 分组加密
*
* @param data 数据
*
* @param data 数据
* @param keyType 密钥类型
* @return 加密后的密文
* @throws CryptoException 加密异常
@@ -201,8 +209,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 分组加密
*
* @param data 数据
*
* @param data 数据
* @param keyType 密钥类型
* @param charset 加密前编码
* @return 加密后的密文
@@ -214,10 +222,11 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
}
// --------------------------------------------------------------------------------- Decrypt
/**
* 解密
*
* @param bytes 被解密的bytes
*
* @param bytes 被解密的bytes
* @param keyType 私钥或公钥 {@link KeyType}
* @return 解密后的bytes
*/
@@ -225,8 +234,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 解密
*
* @param data 被解密的bytes
*
* @param data 被解密的bytes
* @param keyType 私钥或公钥 {@link KeyType}
* @return 解密后的bytes
* @throws IORuntimeException IO异常
@@ -237,8 +246,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 从Hex或Base64字符串解密编码为UTF-8格式
*
* @param data Hex16进制或Base64字符串
*
* @param data Hex16进制或Base64字符串
* @param keyType 私钥或公钥 {@link KeyType}
* @return 解密后的bytes
* @since 4.5.2
@@ -249,8 +258,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 解密为字符串密文需为Hex16进制或Base64字符串
*
* @param data 数据Hex16进制或Base64字符串
*
* @param data 数据Hex16进制或Base64字符串
* @param keyType 密钥类型
* @param charset 加密前编码
* @return 解密后的密文
@@ -262,8 +271,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 解密为字符串密文需为Hex16进制或Base64字符串
*
* @param data 数据Hex16进制或Base64字符串
*
* @param data 数据Hex16进制或Base64字符串
* @param keyType 密钥类型
* @return 解密后的密文
* @since 4.5.2
@@ -274,8 +283,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 解密BCD
*
* @param data 数据
*
* @param data 数据
* @param keyType 密钥类型
* @return 解密后的密文
* @since 4.1.0
@@ -286,8 +295,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 分组解密
*
* @param data 数据
*
* @param data 数据
* @param keyType 密钥类型
* @param charset 加密前编码
* @return 解密后的密文
@@ -300,8 +309,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 解密为字符串密文需为BCD格式
*
* @param data 数据BCD格式
*
* @param data 数据BCD格式
* @param keyType 密钥类型
* @param charset 加密前编码
* @return 解密后的密文
@@ -313,8 +322,8 @@ public abstract class AbstractAsymmetricCrypto<T extends AbstractAsymmetricCrypt
/**
* 解密为字符串密文需为BCD格式编码为UTF-8格式
*
* @param data 数据BCD格式
*
* @param data 数据BCD格式
* @param keyType 密钥类型
* @return 解密后的密文
* @since 4.5.2

View File

@@ -3,6 +3,7 @@ package cn.hutool.crypto.digest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -25,7 +26,8 @@ import cn.hutool.crypto.SecureUtil;
* @author Looly
*
*/
public class Digester {
public class Digester implements Serializable {
private static final long serialVersionUID = 1L;
private MessageDigest digest;
/** 盐值 */

View File

@@ -3,6 +3,7 @@ package cn.hutool.crypto.digest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
@@ -26,7 +27,8 @@ import cn.hutool.crypto.digest.mac.MacEngineFactory;
* @author Looly
*
*/
public class HMac {
public class HMac implements Serializable {
private static final long serialVersionUID = 1L;
private MacEngine engine;

View File

@@ -17,7 +17,7 @@ public enum HmacAlgorithm {
private String value;
private HmacAlgorithm(String value) {
HmacAlgorithm(String value) {
this.value = value;
}

View File

@@ -11,6 +11,7 @@ import java.nio.charset.Charset;
* @since 4.4.3
*/
public class MD5 extends Digester {
private static final long serialVersionUID = 1L;
/**
* 创建MD5实例

View File

@@ -0,0 +1,63 @@
package cn.hutool.crypto.digest;
/**
* SM3算法
*
* @author looly
* @since 4.6.8
*/
public class SM3 extends Digester {
private static final long serialVersionUID = 1L;
public static final String ALGORITHM_NAME = "SM3";
/**
* 创建SM3实例
*
* @return SM3
* @since 4.6.0
*/
public static SM3 create() {
return new SM3();
}
/**
* 构造
*/
public SM3() {
super(ALGORITHM_NAME);
}
/**
* 构造
*
* @param salt 盐值
*/
public SM3(byte[] salt) {
this(salt, 0, 1);
}
/**
* 构造
*
* @param salt 盐值
* @param digestCount 摘要次数当此值小于等于1,默认为1。
*/
public SM3(byte[] salt, int digestCount) {
this(salt, 0, digestCount);
}
/**
* 构造
*
* @param salt 盐值
* @param saltPosition 加盐位置既将盐值字符串放置在数据的index数默认0
* @param digestCount 摘要次数当此值小于等于1,默认为1。
*/
public SM3(byte[] salt, int saltPosition, int digestCount) {
this();
this.salt = salt;
this.saltPosition = saltPosition;
this.digestCount = digestCount;
}
}

View File

@@ -29,6 +29,7 @@ import cn.hutool.crypto.SecureUtil;
* @since 3.0.8
*/
public class AES extends SymmetricCrypto {
private static final long serialVersionUID = 1L;
//------------------------------------------------------------------------- Constrctor start
@@ -178,28 +179,4 @@ public class AES extends SymmetricCrypto {
super(StrUtil.format("AES/{}/{}", mode, padding), key, iv);
}
//------------------------------------------------------------------------- Constrctor end
/**
* 设置偏移向量
*
* @param iv {@link IvParameterSpec}偏移向量
* @return 自身
*/
public AES setIv(IvParameterSpec iv) {
super.setParams(iv);
return this;
}
/**
* 设置偏移向量
*
* @param iv 偏移向量,加盐
* @return 自身
* @since 3.3.0
*/
public AES setIv(byte[] iv) {
setIv(new IvParameterSpec(iv));
return this;
}
}

View File

@@ -17,6 +17,7 @@ import cn.hutool.crypto.SecureUtil;
* @since 3.0.8
*/
public class DES extends SymmetricCrypto {
private static final long serialVersionUID = 1L;
// ------------------------------------------------------------------------- Constrctor start
/**
@@ -150,28 +151,4 @@ public class DES extends SymmetricCrypto {
super(StrUtil.format("DES/{}/{}", mode, padding), key, iv);
}
// ------------------------------------------------------------------------- Constrctor end
/**
* 设置偏移向量
*
* @param iv {@link IvParameterSpec}偏移向量
* @return 自身
*/
public DES setIv(IvParameterSpec iv) {
super.setParams(iv);
return this;
}
/**
* 设置偏移向量
*
* @param iv 偏移向量,加盐
* @return 自身
* @since 3.3.0
*/
public DES setIv(byte[] iv) {
setIv(new IvParameterSpec(iv));
return this;
}
}

View File

@@ -18,6 +18,7 @@ import cn.hutool.crypto.SecureUtil;
* @since 3.3.0
*/
public class DESede extends SymmetricCrypto {
private static final long serialVersionUID = 1L;
// ------------------------------------------------------------------------- Constructor start
/**
@@ -151,28 +152,4 @@ public class DESede extends SymmetricCrypto {
super(StrUtil.format("{}/{}/{}", SymmetricAlgorithm.DESede.getValue(), mode, padding), key, iv);
}
// ------------------------------------------------------------------------- Constructor end
/**
* 设置偏移向量
*
* @param iv {@link IvParameterSpec}偏移向量
* @return 自身
*/
public DESede setIv(IvParameterSpec iv) {
super.setParams(iv);
return this;
}
/**
* 设置偏移向量
*
* @param iv 偏移向量,加盐
* @return 自身
* @since 3.3.0
*/
public DESede setIv(byte[] iv) {
setIv(new IvParameterSpec(iv));
return this;
}
}

View File

@@ -1,5 +1,6 @@
package cn.hutool.crypto.symmetric;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
@@ -17,14 +18,13 @@ import cn.hutool.crypto.CryptoException;
*
* @author Iurii SergiichukLooly
*/
public class RC4 {
public class RC4 implements Serializable {
private static final long serialVersionUID = 1L;
private static final int SBOX_LENGTH = 256;
/** 密钥最小长度 */
private static final int KEY_MIN_LENGTH = 5;
/** Key array */
private byte[] key;
/** Sbox */
private int[] sbox;
@@ -34,7 +34,7 @@ public class RC4 {
* 构造
*
* @param key 密钥
* @throws CryptoException
* @throws CryptoException key长度小于5或者大于255抛出此异常
*/
public RC4(String key) throws CryptoException {
setKey(key);
@@ -175,8 +175,7 @@ public class RC4 {
final WriteLock writeLock = this.lock.writeLock();
writeLock.lock();
try {
this.key = StrUtil.utf8Bytes(key);
this.sbox = initSBox(this.key);
this.sbox = initSBox(StrUtil.utf8Bytes(key));
} finally {
writeLock.unlock();
}

View File

@@ -0,0 +1,166 @@
package cn.hutool.crypto.symmetric;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
/**
* SM4实现
*
* @author Looly
* @since 4.6.8
*/
public class SM4 extends SymmetricCrypto{
private static final long serialVersionUID = 1L;
public static final String ALGORITHM_NAME = "SM4";
//------------------------------------------------------------------------- Constrctor start
/**
* 构造,使用随机密钥
*/
public SM4() {
super(ALGORITHM_NAME);
}
/**
* 构造
*
* @param key 密钥
*/
public SM4(byte[] key) {
super(ALGORITHM_NAME, key);
}
/**
* 构造,使用随机密钥
*
* @param mode 模式{@link Mode}
* @param padding {@link Padding}补码方式
*/
public SM4(Mode mode, Padding padding) {
this(mode.name(), padding.name());
}
/**
* 构造
*
* @param mode 模式{@link Mode}
* @param padding {@link Padding}补码方式
* @param key 密钥支持三种密钥长度128、192、256位
*/
public SM4(Mode mode, Padding padding, byte[] key) {
this(mode, padding, key, null);
}
/**
* 构造
*
* @param mode 模式{@link Mode}
* @param padding {@link Padding}补码方式
* @param key 密钥支持三种密钥长度128、192、256位
* @param iv 偏移向量,加盐
*/
public SM4(Mode mode, Padding padding, byte[] key, byte[] iv) {
this(mode.name(), padding.name(), key, iv);
}
/**
* 构造
*
* @param mode 模式{@link Mode}
* @param padding {@link Padding}补码方式
* @param key 密钥支持三种密钥长度128、192、256位
*/
public SM4(Mode mode, Padding padding, SecretKey key) {
this(mode, padding, key, (IvParameterSpec) null);
}
/**
* 构造
*
* @param mode 模式{@link Mode}
* @param padding {@link Padding}补码方式
* @param key 密钥支持三种密钥长度128、192、256位
* @param iv 偏移向量,加盐
*/
public SM4(Mode mode, Padding padding, SecretKey key, byte[] iv) {
this(mode, padding, key, ArrayUtil.isEmpty(iv) ? ((IvParameterSpec) null) : new IvParameterSpec(iv));
}
/**
* 构造
*
* @param mode 模式{@link Mode}
* @param padding {@link Padding}补码方式
* @param key 密钥支持三种密钥长度128、192、256位
* @param iv 偏移向量,加盐
*/
public SM4(Mode mode, Padding padding, SecretKey key, IvParameterSpec iv) {
this(mode.name(), padding.name(), key, iv);
}
/**
* 构造
*
* @param mode 模式
* @param padding 补码方式
*/
public SM4(String mode, String padding) {
this(mode, padding, (byte[]) null);
}
/**
* 构造
*
* @param mode 模式
* @param padding 补码方式
* @param key 密钥支持三种密钥长度128、192、256位
*/
public SM4(String mode, String padding, byte[] key) {
this(mode, padding, key, null);
}
/**
* 构造
*
* @param mode 模式
* @param padding 补码方式
* @param key 密钥支持三种密钥长度128、192、256位
* @param iv 加盐
*/
public SM4(String mode, String padding, byte[] key, byte[] iv) {
this(mode, padding,//
SecureUtil.generateKey(ALGORITHM_NAME, key),//
ArrayUtil.isEmpty(iv) ? ((IvParameterSpec) null) : new IvParameterSpec(iv));
}
/**
* 构造
*
* @param mode 模式
* @param padding 补码方式
* @param key 密钥支持三种密钥长度128、192、256位
*/
public SM4(String mode, String padding, SecretKey key) {
this(mode, padding, key, null);
}
/**
* 构造
*
* @param mode 模式
* @param padding 补码方式
* @param key 密钥支持三种密钥长度128、192、256位
* @param iv 加盐
*/
public SM4(String mode, String padding, SecretKey key, IvParameterSpec iv) {
super(StrUtil.format("SM4/{}/{}", mode, padding), key, iv);
}
//------------------------------------------------------------------------- Constrctor end
}

View File

@@ -12,8 +12,10 @@ import cn.hutool.crypto.SecureUtil;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEParameterSpec;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.security.spec.AlgorithmParameterSpec;
import java.util.concurrent.locks.Lock;
@@ -27,7 +29,8 @@ import java.util.concurrent.locks.ReentrantLock;
*
* @author Looly
*/
public class SymmetricCrypto {
public class SymmetricCrypto implements Serializable {
private static final long serialVersionUID = 1L;
/**
* SecretKey 负责保存对称密钥
@@ -131,7 +134,7 @@ public class SymmetricCrypto {
*
* @param algorithm 算法
* @param key 密钥,如果为<code>null</code>自动生成一个key
* @return {@link SymmetricCrypto}
* @return {@link SymmetricCrypto}的子对象,既子对象自身
*/
public SymmetricCrypto init(String algorithm, SecretKey key) {
Assert.notBlank(algorithm, "'algorithm' must be not blank !");
@@ -163,6 +166,28 @@ public class SymmetricCrypto {
return this;
}
/**
* 设置偏移向量
*
* @param iv {@link IvParameterSpec}偏移向量
* @return 自身
*/
public SymmetricCrypto setIv(IvParameterSpec iv) {
setParams(iv);
return this;
}
/**
* 设置偏移向量
*
* @param iv 偏移向量,加盐
* @return 自身
*/
public SymmetricCrypto setIv(byte[] iv) {
setIv(new IvParameterSpec(iv));
return this;
}
// --------------------------------------------------------------------------------- Encrypt
/**

View File

@@ -1,12 +1,19 @@
package cn.hutool.crypto.test;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.KeyUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.symmetric.SM4;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import javax.crypto.SecretKey;
/**
* SM单元测试
@@ -25,17 +32,34 @@ public class SmTest {
@Test
public void sm4Test() {
String content = "test中文";
SymmetricCrypto sm4 = SmUtil.sm4();
SM4 sm4 = SmUtil.sm4();
String encryptHex = sm4.encryptHex(content);
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals(content, decryptStr);
}
@Test
public void sm4ECBPKCS5PaddingTest2() {
String content = "test中文";
SM4 sm4 = new SM4(Mode.ECB, Padding.PKCS5Padding);
Assert.assertEquals("SM4/ECB/PKCS5Padding", sm4.getCipher().getAlgorithm());
String encryptHex = sm4.encryptHex(content);
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals(content, decryptStr);
}
@Test
public void sm4Test2() {
public void sm4TestWithCustomKeyTest() {
String content = "test中文";
SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding");
SecretKey key = KeyUtil.generateKey(SM4.ALGORITHM_NAME);
SM4 sm4 = new SM4(Mode.ECB, Padding.PKCS5Padding, key);
Assert.assertEquals("SM4/ECB/PKCS5Padding", sm4.getCipher().getAlgorithm());
String encryptHex = sm4.encryptHex(content);
String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals(content, decryptStr);