对称密钥测试
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.sunyard.chsm.pool;
|
||||
|
||||
import com.sunyard.chsm.constant.CryptoConst;
|
||||
import com.sunyard.chsm.enums.AlgMode;
|
||||
import com.sunyard.chsm.enums.Padding;
|
||||
import com.sunyard.chsm.sdf.SdfApiService;
|
||||
import com.sunyard.chsm.sdf.context.AlgId;
|
||||
@@ -9,6 +11,7 @@ import com.sunyard.chsm.sdf.model.EccPriKey;
|
||||
import com.sunyard.chsm.sdf.model.EccPubKey;
|
||||
import com.sunyard.chsm.sdf.model.EccSignature;
|
||||
import com.sunyard.chsm.sdf.util.PaddingUtil;
|
||||
import com.sunyard.chsm.utils.CodecUtils;
|
||||
import com.sunyard.chsm.utils.gm.BCECUtils;
|
||||
import com.sunyard.chsm.utils.gm.BCSM3Utils;
|
||||
import com.sunyard.chsm.utils.gm.SM2PreprocessSigner;
|
||||
@@ -16,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.bouncycastle.crypto.CipherParameters;
|
||||
import org.bouncycastle.crypto.params.ParametersWithID;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.function.Function;
|
||||
@@ -48,6 +52,17 @@ public class LoadBalancedSdfApiService implements SdfApiService {
|
||||
|
||||
@Override
|
||||
public byte[] symEncrypt(AlgId alg, Padding padding, byte[] key, byte[] iv, byte[] data) {
|
||||
if (alg.name().contains(AlgMode.CBC.getCode())) {
|
||||
Assert.notNull(iv, "CBC模式下, iv不能为空");
|
||||
Assert.isTrue(iv.length >= key.length, "CBC模式下, iv长度不能低于key的长度");
|
||||
}
|
||||
log.info("symEncrypt: alg:{}, padding: {}, key: {}, iv:{}, plainData:{}",
|
||||
alg.name(),
|
||||
padding.getCode(),
|
||||
CodecUtils.encodeHex(key),
|
||||
CodecUtils.encodeHex(iv),
|
||||
CodecUtils.encodeHex(data)
|
||||
);
|
||||
byte[] paddingData = PaddingUtil.padding(padding, data);
|
||||
return apply(s -> {
|
||||
String keyHandle = s.getSdfApiAdapter().importKey(s.getSessionHandle(), key);
|
||||
@@ -59,6 +74,17 @@ public class LoadBalancedSdfApiService implements SdfApiService {
|
||||
|
||||
@Override
|
||||
public byte[] symDecrypt(AlgId alg, Padding padding, byte[] key, byte[] iv, byte[] data) {
|
||||
if (alg.name().contains(AlgMode.CBC.getCode())) {
|
||||
Assert.notNull(iv, "CBC模式下, iv不能为空");
|
||||
Assert.isTrue(iv.length >= key.length, "CBC模式下, iv长度不能低于key的长度");
|
||||
}
|
||||
log.info("symDecrypt: alg:{}, padding: {}, key: {}, iv:{}, cipherData:{}",
|
||||
alg.name(),
|
||||
padding.getCode(),
|
||||
CodecUtils.encodeHex(key),
|
||||
CodecUtils.encodeHex(iv),
|
||||
CodecUtils.encodeHex(data)
|
||||
);
|
||||
byte[] decrypt = apply(s -> {
|
||||
String keyHandle = s.getSdfApiAdapter().importKey(s.getSessionHandle(), key);
|
||||
byte[] d = s.getSdfApiAdapter().symDecrypt(s.getSessionHandle(), keyHandle, alg, iv, data);
|
||||
@@ -157,12 +183,15 @@ public class LoadBalancedSdfApiService implements SdfApiService {
|
||||
@Override
|
||||
public byte[] encryptByTMK(byte[] data) {
|
||||
byte[] pad = PaddingUtil.PKCS7Padding(data);
|
||||
return apply(s -> s.getSdfApiAdapter().symEncrypt(s.getSessionHandle(), s.getKeyHandle(), AlgId.SGD_SM4_CBC, new byte[8], pad));
|
||||
return apply(s -> s.getSdfApiAdapter().symEncrypt(s.getSessionHandle(), s.getKeyHandle(), AlgId.SGD_SM4_CBC, CryptoConst.iv, pad));
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] decryptByTMK(byte[] data) {
|
||||
byte[] decrypt = apply(s -> s.getSdfApiAdapter().symDecrypt(s.getSessionHandle(), s.getKeyHandle(), AlgId.SGD_SM4_CBC, new byte[8], data));
|
||||
byte[] decrypt = apply(s -> s.getSdfApiAdapter().symDecrypt(s.getSessionHandle(), s.getKeyHandle(), AlgId.SGD_SM4_CBC, CryptoConst.iv, data));
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("decryptByTMK res: {}", CodecUtils.encodeHex(decrypt));
|
||||
}
|
||||
return PaddingUtil.PKCS7Unpadding(decrypt);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user