0

Below is a snippet of my code on generating signature:

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();

the signature works fine but currently I would like to implement a time stamp function which will cause the signature to expire after sometimes, perhaps 30 days. However, I have no experience dealing with time stamp, I was also unable to find example or tutorials which explain well enough. Therefore, I would like to ask for links/explanation/tutorial on how to get started! And if possible, snippet of codes that I could use! Thanks in advance!

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Timestamp is not used to make something "expire" directly, but just to prove the time when the signature was made. It's your code that can compare the times, but such comparison can be circumvented easily by changing computer time. – Eugene Mayevski 'Callback Jul 30 '14 at 16:55
  • @EugeneMayevski'EldoSCorp So are there existing methods that are implemented? Also,is there anyway I can compare the times(in my signature) with any existing servers through internet to prevent the comparison from being circumvented? – user3884132 Jul 31 '14 at 01:27

0 Answers0