流水年华
发贴: 0
积分: 0
|
于 2004-05-18 10:58
import java.io.*; import java.security.*; import java.security.spec.*; public class TestDSA { public static void main(String[] args) throws NoSuchAlgorithmException,Exception { TestDSA my=new TestDSA(); my.run(args[0]); } public void run(String name) {
//Êý×ÖÇ©ÃûÉú³ÉÃÜÔ¿ //µÚÒ»²½Éú³ÉÃÜÔ¿¶Ô,Èç¹ûÒѾÉú³É¹ý,±¾¹ý³Ì¾Í¿ÉÒÔÌø¹ý,¶ÔÓû§À´½²myprikey.datÒª±£´æÔÚ±¾µØ //¶ømypubkey.dat¸ø·¢²¼¸øÆäËüÓû§ if ((new File("myprikey")).exists()==false) { if (generatekey()==false) { System.out.println("生成密钥失败"); return; } } //µÚ¶þ²½,´ËÓû§ //´ÓÎļþÖжÁÈë˽Կ,¶ÔÒ»¸ö×Ö·û´®½øÐÐÇ©Ãûºó±£´æÔÚÒ»¸öÎļþ(myinfo.dat)ÖÐ //²¢ÇÒÔÙ°Ñmyinfo.dat·¢ËͳöÈ¥ //ΪÁË·½±ãÊý×ÖÇ©ÃûÒ²·Å½øÁËmyifno.datÎļþÖÐ,µ±È»Ò²¿É·Ö±ð·¢ËÍ try { ObjectInputStream in=new ObjectInputStream(new FileInputStream("myprikey")); PrivateKey myprikey=(PrivateKey)in.readObject(); in.close();
// java.security.spec.X509EncodedKeySpec pubX509=new java.security.spec.X509EncodedKeySpec(bX509);
//java.security.spec.X509EncodedKeySpec pubkeyEncode=java.security.spec.X509EncodedKeySpec RandomAccessFile fin=new RandomAccessFile(name,"r"); long f1=fin.length(); int f2=(int)f1; byte[] b=new byte[f2]; int i=0; fin.read(b,0,f2); Signature signet=Signature.getInstance("DSA"); signet.initSign(myprikey); signet.update(b,0,f2); //signet.update(myinfo.getBytes()); byte[] signed=signet.sign(); //¶ÔÐÅÏ¢µÄÊý×ÖÇ©Ãû //System.out.println(b.toString()); //System.out.println("ÄÚÈÝ="+byte2hex(b)); System.out.println("signed(签名内容)="+byte2hex(signed)); ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("myinfo")); out.writeObject(b); out.writeObject(signed); out.close(); System.out.println("签名并生成文件成功"); } catch (java.lang.Exception e) { e.printStackTrace(); System.out.println("签名并生成文件失败"); };
try {
ObjectInputStream in=new ObjectInputStream(new FileInputStream("mypubkey")); PublicKey pubkey=(PublicKey)in.readObject(); in.close(); System.out.println(pubkey.getFormat());
in=new ObjectInputStream(new FileInputStream("myinfo")); Object fo=(Object)in.readObject(); String info=fo.toString(); byte[] signed=(byte[])in.readObject(); in.close();
Signature signetcheck=Signature.getInstance("DSA"); signetcheck.initVerify(pubkey); signetcheck.update(info.getBytes()); if (signetcheck.verify(signed)) { System.out.println("info="+info); System.out.println("签名正常"); } else System.out.println("签名不正常"); } catch (Exception e) {e.printStackTrace();};
}
public boolean generatekey() { try { KeyPairGenerator keygen=KeyPairGenerator.getInstance("DSA"); // SecureRandom secrand=new SecureRandom(); // secrand.setSeed("tttt".getBytes()); // keygen.initialize(576,secrand); keygen.initialize(512); KeyPair keys=keygen.genKeyPair(); // KeyPair keys=keygen.generateKeyPair(); PublicKey pubkey=keys.getPublic(); PrivateKey prikey=keys.getPrivate();
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("myprikey")); out.writeObject(prikey); out.close(); out=new ObjectOutputStream(new FileOutputStream("mypubkey")); out.writeObject(pubkey); out.close(); return true; }catch (Exception e) { e.printStackTrace(); return false; } }
public String byte2hex(byte[] b) { String hs=""; String stmp=""; for (int n=0;n<b.length;n++) { stmp=(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(); }
}
流水年华 edited on 2004-05-18 13:49
|