Java Security SignedObject create

Description

Java Security SignedObject create

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignedObject;

public class Main {
   public static void main(String args[]) throws Exception {
      char[] password = "123456".toCharArray();
      KeyStore ks = KeyStore.getInstance("JKS");
      ks.load(new FileInputStream("test.ks"), password);

      String str = "Hello World!";
      PrivateKey pri = (PrivateKey) ks.getKey("test", password);
      Signature sig = Signature.getInstance("SHA1withRSA");
      SignedObject so = new SignedObject(str, pri, sig);

      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("so.dat"));
      oos.writeObject(so);// w  w  w  .  j a  v  a  2  s  .c  o  m
   }
}



PreviousNext

Related