Digital Signature Sample Program Java writing
Package com.eos.lighting.java.test; Import Java.security.KeyPair; Import Java.security.PrivateKey; Import Java.security.PublicKey; public class Testds { /** * @param args */ public static void Main (string[] args) { TODO auto-generated Method Stub Testds test=new Testds (); Test.run (); }
public void Run () { if ((New Java.io.File ("Myprikey.dat")). Exists () ==false) { if (GenerateKey () ==false) { Return } }
Try { Java.io.ObjectInputStream in=new Java.io.ObjectInputStream (New Java.io.FileInputStream ("Myprikey.dat")); Privatekey myprikey= (Privatekey) in.readobject (); In.close ();
Java.security.spec.X509EncodedKeySpec pubx509=new Java.security.spec.X509EncodedKeySpec (); String myinfo= "This is my message this is my information this is my information this is my information this is my information this is my information this is my message this is my information this is my information this is my information this is my message"; Java.security.Signature signet=java.security.signature.getinstance ("DSA"); Signet.initsign (Myprikey); Signet.update (Myinfo.getbytes ()); Byte[] Signed=signet.sign (); System.out.println ("Signed (signature content) =" +this.byte2hex (signed)); Keep information and digital signatures in a file Java.io.ObjectOutputStream out=new Java.io.ObjectOutputStream (New Java.io.FileOutputStream ("Myinfo.dat")); Out.writeobject (MyInfo); Out.writeobject (Signed); Out.close (); System.out.println ("Signing and generating file success"); } catch (Exception e) { E.printstacktrace (); }
Other people get this user's public key and files by common means
try{ Java.io.ObjectInputStream in=new Java.io.ObjectInputStream (New Java.io.FileInputStream ("Mypubkey.dat")); PublicKey pubkey= (PublicKey) in.readobject (); In.close ();
System.out.println (Pubkey.getformat ());
In=new Java.io.ObjectInputStream (New Java.io.FileInputStream ("Myinfo.dat")); String info= (String) in.readobject (); Byte[] signed= (byte[]) in.readobject (); In.close ();
Java.security.Signature signetcheck=java.security.signature.getinstance ("DSA"); Signetcheck.initverify (PubKey); Signetcheck.update (Info.getbytes ()); if (Signetcheck.verify (signed)) { System.out.println ("info =" +info); System.out.println ("signature normal"); } Else SYSTEM.OUT.PRINTLN ("Non-signature normal"); } catch (Exception e) { E.printstacktrace (); }
}
public boolean GenerateKey () { Try { Java.security.KeyPairGenerator keygen=java.security.keypairgenerator.getinstance ("DSA"); Keygen.initialize (1024); KeyPair Keys=keygen.genkeypair (); PublicKey pubkey=keys.getpublic (); Privatekey prikey=keys.getprivate ();
Java.io.ObjectOutputStream out=new Java.io.ObjectOutputStream (New Java.io.FileOutputStream ("Myprikey.dat")); Out.writeobject (Prikey); Out.close ();
System.out.println ("Write Object Prikeys OK");
Out=new Java.io.ObjectOutputStream (New Java.io.FileOutputStream ("Mypubkey.dat")); Out.writeobject (PubKey); Out.close ();
System.out.println ("Write Object Pubkeys od");
SYSTEM.OUT.PRINTLN ("Generate key pair success");
return true; } catch (Exception e) { E.printstacktrace (); SYSTEM.OUT.PRINTLN ("Generate key pair failed"); return false; } return true; }
Public String Byte2hex (byte[] b) { String hs= ""; String stmp= ""; for (int n=0;n<b.length;n++) { Stmp= (Java.lang.Integer.toHexString (b[n) & 0XFF)); if (Stmp.length () ==1) hs=hs+ ' 0 ' +stmp; Else hs=hs+stmp; if (n<b.length-1) hs=hs+ ': '; } return Hs.touppercase (); } } |