Digital Signature Sample program Java authoring __java

Source: Internet
Author: User
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 ();
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.