0

以下是我生成签名的代码片段:

File file = new File("privatekey.pkcs8");
        FileInputStream fis = new FileInputStream(file);
        pemBytes = new byte[fis.available()];
        fis.read(pemBytes);
        fis.close();
        File filedata = new File("hi");
        FileInputStream fis2 = new FileInputStream(filedata);
        dataBytes = new byte[fis2.available()];
        fis2.read(dataBytes);
        fis2.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    PrivateKey privKey=null;
    try {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemBytes);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        privKey = kf.generatePrivate(keySpec);
    } catch (Exception e) {
        e.printStackTrace();
    }
    /* Create a Signature object and initialize it with the private key */
    byte[] realSig = null;
    try{
        Signature signature = Signature.getInstance("SHA256withRSA");
      signature.initSign(privKey); 
        signature.update(dataBytes); 
        realSig = signature.sign();
        byte[] res = Base64.encodeBase64(realSig); 
        FileOutputStream sigfos = new FileOutputStream("mysignature");
        sigfos.write(res);
        sigfos.close();
    } catch (Exception e) {
        e.printStackTrace();

签名工作正常,但目前我想实现一个时间戳功能,这会导致签名有时会在 30 天后过期。但是,我没有处理时间戳的经验,我也找不到解释得足够好的示例或教程。因此,我想询问有关如何开始的链接/解释/教程!如果可能的话,我可以使用的代码片段!提前致谢!

4

0 回答 0