Bouncy Castle – 从 TimeStampResponse 获取签名前的哈希值

huangapple go评论73阅读模式
英文:

Bouncy Castle - Get Hash before sign from TimeStampResponse

问题

以下是翻译好的代码部分:

TimeStampResponse GetSignedHashFromTsa(byte[] hash)
{
    TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();

    TimeStampRequest request = reqGen.Generate(
                TspAlgorithms.Sha1,
                hash,
                BigInteger.ValueOf(100)
            );
    byte[] reqData = request.GetEncoded();

    HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create("http://www.cryptopro.ru/tsp/tsp.srf");
    httpReq.Method = "POST";
    httpReq.ContentType = "application/timestamp-query";
    httpReq.ContentLength = reqData.Length;

    // 写入请求内容
    Stream reqStream = httpReq.GetRequestStream();
    reqStream.Write(reqData, 0, reqData.Length);
    reqStream.Close();

    HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

    // 读取响应
    Stream respStream = new BufferedStream(httpResp.GetResponseStream());
    TimeStampResponse response = new TimeStampResponse(respStream);
    respStream.Close();

    return response;
}
byte[] hashToSign = ....;
TimeStampResponse response = GetSignedHashFromTsa(hashToSign);
byte[] signedByteToSaveInFile = response.GetEncoded();
byte[] signedByteToSaveInFile = ....; // 从文件中读取字节数组
TimeStampResponse previouslyTsaSignedDataResponse = new TimeStampResponse(signedByteToSaveInFile);

希望这对你有所帮助。

英文:

I am getting signed hash from TSA by using Bouncy Castle like this-

TimeStampResponse GetSignedHashFromTsa(byte[] hash)
{
    TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();

    TimeStampRequest request = reqGen.Generate(
                TspAlgorithms.Sha1,
                hash,
                BigInteger.ValueOf(100)
            );
    byte[] reqData = request.GetEncoded();

    HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create("http://www.cryptopro.ru/tsp/tsp.srf");
    httpReq.Method = "POST";
    httpReq.ContentType = "application/timestamp-query";
    httpReq.ContentLength = reqData.Length;

    // Write the request content
    Stream reqStream = httpReq.GetRequestStream();
    reqStream.Write(reqData, 0, reqData.Length);
    reqStream.Close();

    HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

    // Read the response
    Stream respStream = new BufferedStream(httpResp.GetResponseStream());
    TimeStampResponse response = new TimeStampResponse(respStream);
    respStream.Close();

    return response;
}

From this function, I can get a TimeStampResponse object (same in Java and C#) from a byte[].

I like to get the byte[] from the TimeStampResponse object in another class. Is there any way?

Thanks in advance for helping.

Re-

For a better understanding of Sai Ye Yan Naing Aye, I am calling the function like this-

byte[] hashToSign = ....;
TimeStampResponse response = GetSignedHashFromTsa(hashToSign);
byte[] signedByteToSaveInFile = response.GetEncoded();

Then I am saving signedByteToSaveInFile in a file. Later I am trying to find the byte[] what is signed. Say, I am doing this-

byte[] signedByteToSaveInFile = ....; //Read byte array from file
TimeStampResponse previouslyTsaSignedDataResponse = new TimeStampResponse(signedByteToSaveInFile);

Now I like to get the byte array what was sent to TSA server before sign from previouslyTsaSignedDataResponse object. So, I like to get byte[] hash what was sent to TSA server to sign. In another word, I like to get the main content before sign.

Think, now the question is more clear.

答案1

得分: 0

我已经这样自行解决了 -

bool ValidateTimestamp(TimeStampResponse tr, byte[] hash)
{
    try
    {
        TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
        TimeStampRequest request = reqGen.Generate(
                TspAlgorithms.Sha1,
                hash,
                BigInteger.ValueOf(100)
            );
    
        tr.Validate(request);
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
        return false;
    }
    return tr.GetFailInfo() == null;
}
英文:

I have solved it myself like this-

bool ValidateTimestamp(TimeStampResponse tr, byte[] hash)
{
    try
    {
        TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
        TimeStampRequest request = reqGen.Generate(
                TspAlgorithms.Sha1,
                hash,
                BigInteger.ValueOf(100)
            );
    
        tr.Validate(request);
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
        return false;
    }
    return tr.GetFailInfo() == null;
}

huangapple
  • 本文由 发表于 2020年10月1日 18:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64153435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定