将java转化为ASN.1、转base64、以十六进制输出;再解析ASN.1。(BC中的ASN1组装和解析)

实习小任务

 要求:

1.写一个Java对象;

2.转化为ASN.1类型(包括ASN1Integer、ASN1Boolean、ASN1String、ASN1Sequence);

3.转换为byte[]、编码为base64;

4.最后以十六进制输出。

 以下为代码: 

pom.xml:


	org.bouncycastle
	bcprov-ext-jdk15on
	1.51

         

Asn1Entity对象:

package com.xdja.timingDemo.model;

import org.bouncycastle.asn1.*;

public class Asn1Entity extends ASN1Object {

    //整数
    private ASN1Integer asn1Integer ;
    //String类型
    private DERUTF8String derutf8String;
    //布尔型
    private DERBoolean derBoolean ;
    //集合、组合
    private ASN1Sequence asn1Sequence;

    public Asn1Entity(ASN1Integer asn1Integer, DERUTF8String derutf8String, DERBoolean derBoolean,ASN1Sequence asn1Sequence) {
        this.asn1Integer = asn1Integer;
        this.derutf8String = derutf8String;
        this.derBoolean = derBoolean;
        this.asn1Sequence = asn1Sequence;

    }
    //get、set省略

    @Override
    public ASN1Primitive toASN1Primitive() {
        //传值
        ASN1EncodableVector vector = new ASN1EncodableVector();
        vector.add(asn1Integer);
        vector.add(derutf8String);
        vector.add(derBoolean);
        vector.add(asn1Sequence);
        return new DERSequence(vector);

    }
}

java转ASN.1:

package com.xdja.timingDemo.asn1;

import com.xdja.timingDemo.model.Asn1Entity;
import org.bouncycastle.asn1.*;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class TestAsn1 {

    public static List sequence() {

        Integer id = 1;
        Boolean status = true;
        String name = "学习强国";

        List list = new ArrayList();
        list.add("ocsp");
        list.add("asn.1");
        list.add("java");
        list.add("base64");

        ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector();
        for (String s : list) {
            asn1EncodableVector.add(new DERUTF8String(s));
        }
        asn1EncodableVector.add(new ASN1Integer(999));
        asn1EncodableVector.add(new DERUTCTime(new Date()));
        System.out.println("size:" + asn1EncodableVector.size());

        //构造函数
        Asn1Entity asn1Entity = new Asn1Entity(new ASN1Integer(id), new DERUTF8String(name), new DERBoolean(status), new DERSequence(asn1EncodableVector));

        //asn1Entity 转换byte[]
        byte[] encode = null;
        try {
            encode = asn1Entity.toASN1Primitive().getEncoded();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("encode----------" + Arrays.toString(encode));
        //byte[]转BASE64
        byte[] b64 = org.bouncycastle.util.encoders.Base64.encode(encode);
        System.out.println("base64----------" + Arrays.toString(b64));
        //b64转换十六进制
        String str = new BigInteger(1, b64).toString(16);
        System.out.println("16进制----------" + str);
             
        return null;
    }

    public static void main(String[] args) {
        TestAsn1.sequence();

    }
}

ASN.1解析:

        //解码为byte数组
        byte[] d64 = org.bouncycastle.util.encoders.Base64.decode(b64);
        System.out.println("decode----------" + Arrays.toString(d64));
        System.out.println("---------------------------------------------------------");
        ASN1InputStream asn1InputStream = new ASN1InputStream(d64);
        ASN1Primitive asn1Primitive;
        try {
            //asn1Entity看作一个组合
            while ((asn1Primitive = asn1InputStream.readObject()) != null) {
                    ASN1Sequence asn1Sequence = (ASN1Sequence) asn1Primitive;
                    ASN1SequenceParser asn1SequenceParser = asn1Sequence.parser();
                    ASN1Encodable asn1Encodable;
                    while ((asn1Encodable = asn1SequenceParser.readObject()) != null) {
                        asn1Primitive = asn1Encodable.toASN1Primitive();

                        if (asn1Primitive instanceof DERUTF8String) {
                            DERUTF8String string = (DERUTF8String) asn1Primitive;
                            System.out.println("DERUTF8String--:" + string.getString());
                        } else if (asn1Primitive instanceof ASN1Boolean) {
                            ASN1Boolean asn1Boolean = (ASN1Boolean) asn1Primitive;
                            System.out.println("ASN1Boolean--:" + asn1Boolean.toString());
                        } else if (asn1Primitive instanceof ASN1Integer) {
                            ASN1Integer asn1Integer = (ASN1Integer) asn1Primitive;
                            System.out.println("ASN1Integer--:" + asn1Integer.getValue());
                        }else{

                            ASN1Sequence asn1Sequence1 = (ASN1Sequence) asn1Primitive;
                            ASN1SequenceParser asn1SequenceParser1 = asn1Sequence1.parser();
                            ASN1Encodable asn1Encodable1;

                            while ((asn1Encodable1 = asn1SequenceParser1.readObject()) != null) {
                                asn1Primitive = asn1Encodable1.toASN1Primitive();

                                if (asn1Primitive instanceof DERUTF8String) {
                                    DERUTF8String string = (DERUTF8String) asn1Primitive;
                                    System.out.println("DERUTF8String--:" + string.getString());
                                }else if (asn1Primitive instanceof ASN1UTCTime) {
                                    ASN1UTCTime asn1utcTime = (ASN1UTCTime) asn1Primitive;
                                    System.out.println("UTCTime:"+asn1utcTime.getTime());
                                }

                            }

                        }

                    }

            }
        } catch (Exception e) {
            e.printStackTrace();

        }

输出:将java转化为ASN.1、转base64、以十六进制输出;再解析ASN.1。(BC中的ASN1组装和解析)_第1张图片

END!!!

你可能感兴趣的:(Java,ASN.1)