Java Digital Signatures - Inconsistent Signature

2019-08-13 23:38发布

问题:

I've been using the Java security api and was following the tutorial on how to generate digital signatures: http://docs.oracle.com/javase/tutorial/security/apisign/gensig.html

The problem is when I run the code, the output signature is always different even when the private key and data which is to be signed stays constant. I have provided the code below which I used to run. "priv" is a file which holds the private key and is used to sign the data: "hello".

Basically the two outputs show the same data: "hello" signed with the same key but it gives a different output. I was expecting the same output to be given. Also when I run the program again, the signed data is different again from the initial run. Any help would be much appreciated. Thanks in advance

public static void main(String[] args) {
   try {
      FileInputStream privfis;
      privfis = new FileInputStream("priv");
      byte[] encKey = new byte[privfis.available()];
      // obtain the encoded key in the file
      privfis.read(encKey);
      privfis.close();
      PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec( encKey);
      KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
      PrivateKey priv = keyFactory.generatePrivate(privKeySpec);
      Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
      dsa.initSign(priv);
      String message = "hello";
      System.out.println(getHexString(message.getBytes()));
      dsa.update(message.getBytes());
      byte[] realSig = dsa.sign();
      System.out.println(getHexString(realSig));
      dsa.update(message.getBytes()); 
      System.out.println(getHexString(message.getBytes()));
      byte[] realSig2 = dsa.sign();
      System.out.println(getHexString(realSig2));
      } catch (Exception e) {
         System.err.println("Caught exception " + e.toString());
         e.printStackTrace();
      }
   }

   private static String getHexString(byte[] b) {
      String result = "";
      for (int i = 0; i < b.length; i++) {
         result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
      }
      return result;
   }        
}