Java Security SignedObject verify

Description

Java Security SignedObject verify

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignedObject;

import javax.security.cert.X509Certificate;

public class Main {
   public static void main(String args[]) throws Exception {
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream("so.dat"));
      SignedObject so = (SignedObject) ois.readObject();
      PublicKey pub = X509Certificate.getInstance(new FileInputStream("test.cer")).getPublicKey();

      Signature sg = Signature.getInstance(so.getAlgorithm());
      if (so.verify(pub, sg)) {
         String s = (String) so.getObject();
         System.out.println(s);/*from w  ww  .j  a v a2  s  . co m*/
      }
      ois.close();
   }
}



PreviousNext

Related