时间戳错误bouncycastle

时间:2013-04-01 07:30:35

标签: java timestamp

我试图从tsa服务器获取时间戳,但是我收到了这个错误。我不知道问题出在哪里。

java.lang.IllegalArgumentException: unknown object in 'TimeStampResp' factory : org.bouncycastle.asn1.DERUnknownTag

在本声明中

TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());

这是我的代码:

    String TSA_URL    = "http://tsa.starfieldtech.com/";
    //String TSA_URL = "http://ca.signfiles.com/TSAServer.aspx";
    //String TSA_URL = "http://timestamping.edelweb.fr/service/tsp";
    try {
        //byte[] digest = calcularMessageDigest(leerByteFichero("C:\\deskSign.txt"));
        byte[] digest = leerByteFichero("C:\\deskSign.txt");

        TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator();
        TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA1, digest);
        byte request[] = req.getEncoded();

        URL url = new URL(TSA_URL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-type:", "application/timestamp-query");
        con.setRequestProperty("Content-length:", String.valueOf(request.length));

        if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage());
        }
        InputStream in = con.getInputStream();
        TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
        TimeStampResponse response = new TimeStampResponse(resp);
        response.validate(req);
        System.out.println(response.getTimeStampToken().getTimeStampInfo().getGenTime());

任何人都可以帮助我?

1 个答案:

答案 0 :(得分:0)

你犯了两个错误:

  1. Content-type和Content-length之后没有冒号
  2. 您忘记了实际发送请求
  3. 以下是适用于您的代码:

    TimeStampRequest request;
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/timestamp-query");
    con.setRequestProperty("Content-length", String.valueOf(request.getEncoded().length));
    Authenticator.setDefault(new Authenticator() { 
    con.getOutputStream().write(request.getEncoded());
    
    if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
    throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage());
    }
    InputStream in = con.getInputStream();
    
    TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
    TimeStampResponse response = new TimeStampResponse(resp);