代码之家  ›  专栏  ›  技术社区  ›  willcodejavaforfood

时间戳响应不正确-BouncyCastle

  •  2
  • willcodejavaforfood  · 技术社区  · 16 年前

    尝试使用BouncyCastle并连接到请求时间戳(RFC 3161) http://timestamping.edelweb.fr/service/tsp

    public static void main(String[] args) {
        String ocspUrl = "http://timestamping.edelweb.fr/service/tsp";
        byte[] digest = "hello".getBytes();
        OutputStream out = null;
    
        try {
            TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator();
            TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA1, digest);
            byte request[] = req.getEncoded();
    
            URL url = new URL(ocspUrl);
            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));
            out = con.getOutputStream();
            out.write(request);
            out.flush();
    
            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());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    问题如下: 有没有人使用Bouncycastle的时间戳库,碰巧知道不同的状态代码及其含义?或者只是一般来说,为什么这似乎行不通。

    我希望看到日期的这一行只抛出了一个NullPointer:

    System.out.println(response.getTimeStampToken().getTimeStampInfo().getGenTime());
    

    如果你想运行代码,你需要bouncycastle罐子,可以从以下网址下载 here 。您需要:供应商、邮件、tsp。

    3 回复  |  直到 14 年前
        1
  •  3
  •   endre    14 年前

    分析与wireshark的通信,这个例子给了我一个“坏消息摘要”错误。 对我有效的摘要代码是:

        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        messageDigest.update("messageImprint".getBytes());
        byte[] digest = messageDigest.digest();
    
        2
  •  1
  •   willcodejavaforfood    16 年前

    问题似乎是内容的格式/长度错误。

    TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA1, digest);
    

    "hello".getBytes();
    

    static public byte[] calculateMessageDigest()
            throws NoSuchAlgorithmException, IOException {
        SHA1Digest md = new SHA1Digest();
    
        byte[] dataBytes = "helloooooooooooooo".getBytes();
        int nread = dataBytes.length;
        md.update(dataBytes, 0, nread);
        byte[] result = new byte[32];
        md.doFinal(result, 0);
        return result;
    

    Digistamp

        3
  •  0
  •   willcodejavaforfood    16 年前
    推荐文章