cms decrypt iv

1. 数字信封解封时使用数字信封中的 IV
2. 数字信封解封格式错误时抛出异常 IllegalArgumentException("数字信封解封格式错误")
This commit is contained in:
cheney 2022-06-22 17:37:13 +08:00
parent 6bd2cf1197
commit 8e3e0c5f11
2 changed files with 54 additions and 4 deletions

View File

@ -2617,6 +2617,11 @@ public class SydApi4j implements SydApi {
// 密文段
ASN1Sequence seq5 = (ASN1Sequence) seq.getObjectAt( 2 ).toASN1Primitive();
ASN1Sequence seq6 = (ASN1Sequence) seq5.getObjectAt(1).toASN1Primitive();
DEROctetString iv = (DEROctetString) seq6.getObjectAt(1).toASN1Primitive();
ret.put("iv", iv.getOctets() );
ASN1TaggedObject tag = (ASN1TaggedObject) seq5.getObjectAt(2).toASN1Primitive();
DEROctetString data = (DEROctetString) tag.getObject();
ret.put("enData", data.getOctets() );
@ -2679,17 +2684,26 @@ public class SydApi4j implements SydApi {
e.printStackTrace();
}
RetWrap ret = deCMSEnvelopedData( Util.decodeBase64( pCipherKey ) );
RetWrap ret = null;
try {
ret = deCMSEnvelopedData( Util.decodeBase64( pCipherKey ) );
} catch ( Exception e) {
throw new IllegalArgumentException("数字信封解封格式错误");
}
byte[] sessionKeyDer = (byte[]) ret.get("sessionKeyDer");
byte[] enData = (byte[]) ret.get("enData");
// 解密原文
byte[] iv = (byte[]) ret.get("iv"); //new byte[32];
// Arrays.fill(iv, (byte) 0x30);
iv = Util.bytes2HexString( iv ).getBytes();
// 解密会话密钥
ret = decryptDE(false, 1, dnBytes, Util.encodeBase64(sessionKeyDer) );
String sessionKey = new String((byte[]) ret.get("SessionKey") );
// 解密原文
byte[] iv = new byte[32];
Arrays.fill(iv, (byte) 0x30);
RetWrap retWrap = SYMEnDeData(false, 1, sessionKey, 1, 1, enData, iv, 0);
byte[] data = (byte[]) retWrap.get("msg_all");

View File

@ -5,11 +5,13 @@ import com.sunyard.SydApi;
import com.sunyard.cert.X509;
import com.sunyard.proto.Util;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import racal.sunyard.main.SydApi4j;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
@ -179,6 +181,7 @@ public class TestHT {
byte[] sign = api.attachedSignC(orgData, dn);
System.out.println("签名结果: " + sign);
RetWrap verify = api.attachedVerifyAndGetAll(sign);
if (verify != null) {
System.out.println("x509公钥: " + Util.bytes2HexString(((X509)verify.get("x509")).getPubKey()));
@ -186,6 +189,39 @@ public class TestHT {
}
}
@Test
public void testDecryptDE54k12() throws UnsupportedEncodingException {
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
byte[] oData = new byte[ 4 * 1024 ];
RetWrap retWrap = api.generateDEByDnHsm(dn, oData);
byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY);
byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY);
printRetWrap(retWrap);
RetWrap retWrap1 = api.decryptDEByDNSoft(dn, new String(CipherKey));
printRetWrap(retWrap1);
byte[] deData = (byte[]) retWrap1.get("oData");
Assert.assertArrayEquals( deData, oData );
}
@Test
public void testDecryptDE54k21() throws UnsupportedEncodingException {
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
byte[] oData = new byte[ 4 * 1024 ];
RetWrap retWrap = api.generateDEByDnSoft(dn, oData);
byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY);
byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY);
printRetWrap(retWrap);
RetWrap retWrap1 = api.decryptDEByDNHsm(dn, new String(CipherKey));
printRetWrap(retWrap1);
byte[] deData = (byte[]) retWrap1.get("oData");
Assert.assertArrayEquals( deData, oData );
}
@Test
public void testDecryptDE54k() throws UnsupportedEncodingException {
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";