p7 证书相关
This commit is contained in:
parent
2e8ea4e077
commit
4404f89dd6
18
pom.xml
18
pom.xml
@ -76,6 +76,24 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.bouncycastle</groupId>
|
||||||
|
<artifactId>bcpkix-jdk15on</artifactId>
|
||||||
|
<version>1.65</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.bouncycastle</groupId>
|
||||||
|
<artifactId>bcprov-jdk15on</artifactId>
|
||||||
|
<version>1.65</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.4.M1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--<dependency>-->
|
<!--<dependency>-->
|
||||||
<!--<groupId>club.fullstack</groupId>-->
|
<!--<groupId>club.fullstack</groupId>-->
|
||||||
<!--<artifactId>serialize</artifactId>-->
|
<!--<artifactId>serialize</artifactId>-->
|
||||||
|
|||||||
139
src/main/java/org/bouncycastle/cms/SydCmsUtil.java
Normal file
139
src/main/java/org/bouncycastle/cms/SydCmsUtil.java
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
package org.bouncycastle.cms;
|
||||||
|
|
||||||
|
import com.sunyard.SydApiException;
|
||||||
|
import org.bouncycastle.asn1.*;
|
||||||
|
import org.bouncycastle.asn1.cms.ContentInfo;
|
||||||
|
import org.bouncycastle.asn1.cms.EncryptedContentInfo;
|
||||||
|
import org.bouncycastle.asn1.cms.OriginatorInfo;
|
||||||
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
public class SydCmsUtil {
|
||||||
|
|
||||||
|
public static Object readContentInfo(
|
||||||
|
byte[] input) {
|
||||||
|
ASN1InputStream is = new ASN1InputStream(input);
|
||||||
|
Object v = null;
|
||||||
|
try {
|
||||||
|
v = is.readObject();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Map<String, Object> getEncryptedContentInfo(ASN1Encodable obj){
|
||||||
|
Map<String, Object>map = new HashMap<String, Object>();
|
||||||
|
ASN1Sequence seq = ASN1Sequence.getInstance(obj );
|
||||||
|
if (seq.size() < 2)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException("Truncated Sequence Found");
|
||||||
|
}
|
||||||
|
|
||||||
|
ASN1ObjectIdentifier contentType = (ASN1ObjectIdentifier)seq.getObjectAt(0);
|
||||||
|
map.put( "contentType", contentType );
|
||||||
|
|
||||||
|
AlgorithmIdentifier contentEncryptionAlgorithm = AlgorithmIdentifier.getInstance(
|
||||||
|
seq.getObjectAt(1));
|
||||||
|
|
||||||
|
map.put( "contentEncryptionAlgorithm", contentEncryptionAlgorithm );
|
||||||
|
|
||||||
|
if (seq.size() > 2)
|
||||||
|
{
|
||||||
|
ASN1OctetString encryptedContent = ASN1OctetString.getInstance(
|
||||||
|
(ASN1TaggedObject)seq.getObjectAt(2), false);
|
||||||
|
map.put( "encryptedContent", encryptedContent );
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> getOriginatorInfo(ASN1TaggedObject obj,
|
||||||
|
boolean explicit){
|
||||||
|
Map<String, Object>map = new HashMap<String, Object>();
|
||||||
|
ASN1Sequence seq = ASN1Sequence.getInstance(obj, explicit);
|
||||||
|
switch (seq.size())
|
||||||
|
{
|
||||||
|
case 0: // empty
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
ASN1TaggedObject o = (ASN1TaggedObject)seq.getObjectAt(0);
|
||||||
|
switch (o.getTagNo())
|
||||||
|
{
|
||||||
|
case 0 :
|
||||||
|
ASN1Set certs = ASN1Set.getInstance(o, false);
|
||||||
|
map.put("certs", certs);
|
||||||
|
break;
|
||||||
|
case 1 :
|
||||||
|
ASN1Set crls = ASN1Set.getInstance(o, false);
|
||||||
|
map.put("crls", crls);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Bad tag in OriginatorInfo: " + o.getTagNo());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
ASN1Set certs = ASN1Set.getInstance((ASN1TaggedObject)seq.getObjectAt(0), false);
|
||||||
|
map.put("certs", certs);
|
||||||
|
ASN1Set crls = ASN1Set.getInstance((ASN1TaggedObject)seq.getObjectAt(1), false);
|
||||||
|
map.put("crls", crls);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("OriginatorInfo too big");
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ContentInfo getContentInfo( byte[] input ) {
|
||||||
|
Object content = readContentInfo( input );
|
||||||
|
ASN1Sequence seq = ASN1Sequence.getInstance( content );
|
||||||
|
return new ContentInfo(seq);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> readCmsInfo( byte[] input ) throws CMSException {
|
||||||
|
Map<String, Object>map = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
Object content = readContentInfo( input );
|
||||||
|
ASN1Sequence seq = ASN1Sequence.getInstance( content );
|
||||||
|
ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) seq.getObjectAt(0);
|
||||||
|
map.put("oid", oid);
|
||||||
|
|
||||||
|
seq = ASN1Sequence.getInstance((ASN1TaggedObject) seq.getObjectAt(1), true);
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
ASN1Integer version = (ASN1Integer)seq.getObjectAt(index++);
|
||||||
|
map.put( "version", version );
|
||||||
|
|
||||||
|
Object tmp = seq.getObjectAt(index++);
|
||||||
|
|
||||||
|
if (tmp instanceof ASN1TaggedObject)
|
||||||
|
{
|
||||||
|
OriginatorInfo originatorInfo = OriginatorInfo.getInstance((ASN1TaggedObject)tmp, false);
|
||||||
|
// Map<String, Object>originatorInfo = getOriginatorInfo((ASN1TaggedObject)tmp, false);
|
||||||
|
map.put( "originatorInfo", originatorInfo );
|
||||||
|
tmp = seq.getObjectAt(index++);
|
||||||
|
}
|
||||||
|
|
||||||
|
ASN1Set recipientInfos = ASN1Set.getInstance(tmp);
|
||||||
|
map.put( "recipientInfos", recipientInfos );
|
||||||
|
|
||||||
|
EncryptedContentInfo encryptedContentInfo = EncryptedContentInfo.getInstance( seq.getObjectAt(index++) );
|
||||||
|
// Map<String, Object> encryptedContentInfo = getEncryptedContentInfo(seq.getObjectAt(index++));
|
||||||
|
map.put( "encryptedContentInfo", encryptedContentInfo );
|
||||||
|
|
||||||
|
if(seq.size() > index)
|
||||||
|
{
|
||||||
|
ASN1Set unprotectedAttrs = ASN1Set.getInstance((ASN1TaggedObject)seq.getObjectAt(index), false);
|
||||||
|
map.put( "unprotectedAttrs", unprotectedAttrs );
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -4,15 +4,13 @@ import com.sunyard.RetWrap;
|
|||||||
import com.sunyard.SydApi;
|
import com.sunyard.SydApi;
|
||||||
import com.sunyard.cert.X509;
|
import com.sunyard.cert.X509;
|
||||||
import com.sunyard.proto.Util;
|
import com.sunyard.proto.Util;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import racal.sunyard.main.SydApi4j;
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.Base64;
|
|
||||||
|
|
||||||
import static com.sunyard.sydapi.util.PrintUtil.printRetWrap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 衡泰例子
|
* 衡泰例子
|
||||||
@ -25,7 +23,7 @@ public class TestHT {
|
|||||||
public void before() {
|
public void before() {
|
||||||
// 通过设置debug的属性开启debug,如果不设置则默认不打开
|
// 通过设置debug的属性开启debug,如果不设置则默认不打开
|
||||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
api = (SydApi4j) new SydApi4j().connect("172.16.18.59", 8889, null, 1000);
|
api = (SydApi4j) new SydApi4j().connect("192.168.0.200", 8889, null, 1000);
|
||||||
// api = (SydApi4j) new SydApi4j().connect("192.168.0.66", 8889, null, 1000);
|
// api = (SydApi4j) new SydApi4j().connect("192.168.0.66", 8889, null, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,6 +37,46 @@ public class TestHT {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecryptDE5() throws UnsupportedEncodingException {
|
||||||
|
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||||
|
String oData = "00001111222233";
|
||||||
|
|
||||||
|
RetWrap retWrap = api.generateDEByDn(dn, oData);
|
||||||
|
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||||
|
System.out.println("数字信封: " + cipherKey);
|
||||||
|
|
||||||
|
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
|
||||||
|
System.out.println("原数据: " + retWrap1.get("oData"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecryptDE5Soft1() throws UnsupportedEncodingException {
|
||||||
|
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||||
|
String oData = "00001111222233";
|
||||||
|
|
||||||
|
RetWrap retWrap = api.generateDEByDnSoft(dn, oData);
|
||||||
|
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||||
|
System.out.println("数字信封: " + cipherKey);
|
||||||
|
|
||||||
|
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
|
||||||
|
System.out.println("原数据: " + retWrap1.get("oData"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDecryptDE5Soft2() throws UnsupportedEncodingException {
|
||||||
|
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||||
|
String oData = "00001111222233";
|
||||||
|
|
||||||
|
RetWrap retWrap = api.generateDEByDn(dn, oData);
|
||||||
|
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||||
|
System.out.println("数字信封: " + cipherKey);
|
||||||
|
|
||||||
|
RetWrap retWrap1 = api.decryptDEByDNSoft(dn, cipherKey);
|
||||||
|
System.out.println("原数据: " + retWrap1.get("oData"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// 测试通过
|
// 测试通过
|
||||||
// 73
|
// 73
|
||||||
@ -121,19 +159,19 @@ public class TestHT {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecryptDE5() throws UnsupportedEncodingException {
|
public void getCert(){
|
||||||
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||||
String oData = "00001111222233";
|
String cert = api.GetX509CertFileBySN("1111111111111151", SydApi.FOR_ENCRYPT );
|
||||||
|
System.out.println( cert );
|
||||||
RetWrap retWrap = api.generateDEByDn(dn, oData);
|
|
||||||
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
|
||||||
System.out.println("数字信封: " + cipherKey);
|
|
||||||
|
|
||||||
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
|
|
||||||
System.out.println("原数据: " + retWrap1.get("oData"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 流程演示
|
* 流程演示
|
||||||
*/
|
*/
|
||||||
|
|||||||
83
src/test/java/com/sunyard/sydapi/util/SydCmsUtilTest.java
Normal file
83
src/test/java/com/sunyard/sydapi/util/SydCmsUtilTest.java
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
package com.sunyard.sydapi.util;
|
||||||
|
|
||||||
|
import com.sunyard.RetWrap;
|
||||||
|
import com.sunyard.SydApi;
|
||||||
|
import com.sunyard.proto.Util;
|
||||||
|
import org.bouncycastle.asn1.cms.ContentInfo;
|
||||||
|
import org.bouncycastle.cms.SydCmsUtil;
|
||||||
|
|
||||||
|
import org.bouncycastle.asn1.ASN1Sequence;
|
||||||
|
import org.bouncycastle.asn1.ASN1Set;
|
||||||
|
import org.bouncycastle.asn1.ASN1TaggedObject;
|
||||||
|
import org.bouncycastle.asn1.DEROctetString;
|
||||||
|
import org.bouncycastle.asn1.cms.ContentInfo;
|
||||||
|
import org.bouncycastle.cms.SydCmsUtil;
|
||||||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
|
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
|
|
||||||
|
public class SydCmsUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
SydApi4j api = (SydApi4j) new SydApi4j().connect("192.168.0.200", 8889, null, 1000);
|
||||||
|
|
||||||
|
try {
|
||||||
|
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||||
|
String oData = "00001111222233";
|
||||||
|
|
||||||
|
RetWrap retWrap = api.generateDEByDnSoft(dn, oData);
|
||||||
|
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||||
|
System.out.println("数字信封: " + cipherKey);
|
||||||
|
|
||||||
|
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
|
||||||
|
System.out.println("原数据: " + retWrap1.get("oData"));
|
||||||
|
}catch ( Exception e ) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if ( null != api ) {
|
||||||
|
api.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
|
|
||||||
|
|
||||||
|
String tmpl = "3082014D06092A864886F70D010703A082013E3082013A0201003181F73081F40201003065305C310B300906035504061302434E3130302E060355040A0C274368696E612046696E616E6369616C2043657274696669636174696F6E20417574686F72697479311B301906035504030C1243464341205445535420534D32204F43413102051004282452300B06092A811CCF5501822D03047B307902203C3AF535307DA548DA02C0AD171D2BC24D22472B42E5E175FCAF2C5CD30B5443022100ED53E7594F35FFACEB57F05898123F78E5B2D045CC47D894CD2E7BD34D66DA1D042014BD71F8CDA9729093A7CC391E2009C5D5F007586C2EBFB55BC175A6E8BDA33E04104AB82510BFDDD4FEE79C3F107D6F7F94303B060A2A811CCF550601040201301B06072A811CCF55016804100000000000000000000000000000000080109FD3D4338DB19E672CC162AF5B9B03E9";
|
||||||
|
|
||||||
|
System.out.println( SydCmsUtil.class.getClassLoader() );
|
||||||
|
|
||||||
|
ContentInfo info = SydCmsUtil.getContentInfo(Util.hexString2Bytes( tmpl ) );
|
||||||
|
|
||||||
|
System.out.println( info );
|
||||||
|
|
||||||
|
info = SydApi4j.genCMSEnvelopedData(new byte[0], new byte[0]);
|
||||||
|
|
||||||
|
SydApi4j api = (SydApi4j) new SydApi4j().connect("192.168.0.200", 8889, null, 1000);
|
||||||
|
|
||||||
|
try {
|
||||||
|
String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
||||||
|
String oData = "00001111222233";
|
||||||
|
|
||||||
|
RetWrap retWrap = api.generateDEByDnSoft(dn, oData);
|
||||||
|
String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY);
|
||||||
|
System.out.println("数字信封: " + cipherKey);
|
||||||
|
|
||||||
|
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
|
||||||
|
System.out.println("原数据: " + retWrap1.get("oData"));
|
||||||
|
}catch ( Exception e ) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if ( null != api ) {
|
||||||
|
api.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user