Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用TSA URL和Java API的时间戳_Java_Security_Digital Signature_Timestamping_Rfc3161 - Fatal编程技术网

使用TSA URL和Java API的时间戳

使用TSA URL和Java API的时间戳,java,security,digital-signature,timestamping,rfc3161,Java,Security,Digital Signature,Timestamping,Rfc3161,有谁能帮助我理解签名时间戳时使用的流程和JavaAPI吗 我需要签署一个文件,并使用Java API使用TSA url“”为其添加时间戳 我可以使用java.security API对文件进行签名,但无法为其添加时间戳。您的问题有点宽泛。。。我会给你一些信息,我希望它能为你指明正确的方向 问题是,您希望使用时间戳服务来执行时间戳签名,使用服务:http://timestamp.globalsign.com/scripts/timstamp.dll 首先,该服务是一个时间戳协议(TSP)RFC31

有谁能帮助我理解签名时间戳时使用的流程和JavaAPI吗

我需要签署一个文件,并使用Java API使用TSA url“”为其添加时间戳


我可以使用java.security API对文件进行签名,但无法为其添加时间戳。

您的问题有点宽泛。。。我会给你一些信息,我希望它能为你指明正确的方向

问题是,您希望使用时间戳服务来执行时间戳签名,使用服务:
http://timestamp.globalsign.com/scripts/timstamp.dll

首先,该服务是一个
时间戳协议(TSP)RFC3161
编译器,请查看以了解其工作原理

无论如何,我认为您正在寻找一个java代码示例,因此下面我给您一个示例代码,它使用RFC3161的时间戳服务器执行时间戳签名

本示例中的步骤基本上是:

  • 首先创建时间戳请求,然后将请求发送到 服务,最后读取响应

    时间戳请求具有以下定义:

    TimeStampReq ::= SEQUENCE  {
       version                      INTEGER  { v1(1) },
       messageImprint               MessageImprint,
       --a hash algorithm OID and the hash value of the data to be time-stamped
       reqPolicy             TSAPolicyId              OPTIONAL,
       nonce                 INTEGER                  OPTIONAL,
       certReq               BOOLEAN                  DEFAULT FALSE,
       extensions            [0] IMPLICIT Extensions  OPTIONAL  }
    
    由于您只能看到所需的
    消息印记
    ,其余的都是 可选选项取决于tsp服务提供给您的选项

  • 第二步是使用
    POST
    方法发送此时间戳请求 指定为
    内容类型
    http头:
    应用程序/时间戳查询

  • 最后一部分是解析响应并获取时间戳令牌

  • 下面是代码:

    总而言之:

    import java.math.BigInteger;
    import java.security.MessageDigest;
    import java.util.Date;
    import java.util.Random;
    
    import org.bouncycastle.asn1.ASN1Sequence;
    import org.bouncycastle.asn1.ASN1StreamParser;
    import org.bouncycastle.asn1.DERBoolean;
    import org.bouncycastle.asn1.DERInteger;
    import org.bouncycastle.asn1.DERObjectIdentifier;
    import org.bouncycastle.asn1.tsp.MessageImprint;
    import org.bouncycastle.asn1.tsp.TimeStampReq;
    import org.bouncycastle.asn1.tsp.TimeStampResp;
    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    import org.bouncycastle.tsp.TimeStampResponse;
    import org.bouncycastle.tsp.TimeStampToken;
    
    public class TimeStampGenerationSample {
    
        public static void main(String args[]) throws Exception{
    
            // for this sample we will use SHA1 to perform the hashes
            // however feel free to use another algorithm since sha1 is weakness
            String sha1Oid = "1.3.14.3.2.26";
            // data to be timestamped
            byte[] data = "some sample data... or your signature...".getBytes();
    
            // perform the hash of your data
            byte[] digestData = MessageDigest.getInstance(sha1Oid, new BouncyCastleProvider()).digest(data);
            // generate random data to perform your ts, it's optional depends on your ts service
            Random rand = new Random(new Date().getTime()); 
            String nonce = BigInteger.valueOf(rand.nextLong()).toString();          
            // require cert optional (default false... so use false)
            boolean requireCert = false;
            // timestampPolicy it's an oid to identify a policy, if it's required
            // must be provided by your ts service... it's optional so we put null
            String timestampPolicy = null;      
    
            TimeStampReq ts_req = createTimeStampRequest(digestData, nonce, requireCert, sha1Oid, timestampPolicy);
    
            // the data to be send to the service
            byte[] dataToSend = ts_req.getEncoded();
    
            // simply send your data using POST method
            // don't forget to specify http-header content-type as "application/timestamp-query"
            byte[] response = // send the request as you want
            // parse the response 
            ASN1StreamParser asn1Sp = new ASN1StreamParser(response);
            TimeStampResp tspResp = new TimeStampResp((ASN1Sequence)asn1Sp.readObject());
            TimeStampResponse tsr = new TimeStampResponse(tspResp);
            // and get the timestamp token :)
            TimeStampToken token = tsr.getTimeStampToken();
        }
    
        /**
         * Create the timestamp request
         * @param hashedData
         * @param nonce
         * @param requireCert
         * @param digestAlgorithm
         * @param timestampPolicy
         * @return
         * @throws TimeStampGenerationException
         */
        public static TimeStampReq createTimeStampRequest(byte[] hashedData, String nonce, boolean requireCert, String digestAlgorithm, String timestampPolicy) throws TimeStampGenerationException {
    
            MessageImprint imprint = new MessageImprint(new AlgorithmIdentifier(digestAlgorithm), hashedData);
    
            TimeStampReq request = new TimeStampReq(
                    imprint, 
                    timestampPolicy!=null?new DERObjectIdentifier(timestampPolicy):null, 
                    nonce!=null?new DERInteger(nonce.getBytes()):null, 
                    new DERBoolean(requireCert), 
                    null
            );      
    
            return request;
        }
    }
    
    请注意,我在示例中使用了
    BouncyCastleAPI


    希望这能有所帮助,

    非常感谢@albciff的回复……但我已经看完了这篇文章。我的问题是,我必须只使用Java API,而不使用外部JAR或API。请通过这篇文章告诉我其他解决方法?你什么意思?无论如何,如果要实现
    TimeStampReq
    结构元素和其他相关asn1对象,而不使用
    bouncycastle
    。。。阅读RFC规范并有幸实现它。。。