Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java Security  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教java.security.Signature类对word文件进行数字签名的例程
alicecyh





发贴: 13
积分: 0
于 2004-03-17 16:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list



作者 Re:请教java.security.Signature类对word文件进行数字签名的例程 [Re:alicecyh]
wuliang





发贴: 46
积分: 20
于 2004-04-30 10:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
private static void trySignature() {
    KeyPair pair = tryGenDSAKeyPair();
    try {
      byte[] data = "This's a test Message".getBytes();
      System.out.println("data: " + toHexString(data));
      Signature dsa = Signature.getInstance("SHA1WithDSA");
      dsa.initSign(pair.getPrivate());
      dsa.update(data);
      byte[] sig = dsa.sign();

      System.out.println("sign with PrivateKey: " + toHexString(sig));
      dsa.initVerify(pair.getPublic());
      dsa.update(data);
      boolean result = dsa.verify(sig);
      System.out.println("verify with PublicKey: " + result);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }



作者 Re:请教java.security.Signature类对word文件进行数字签名的例程 [Re:alicecyh]
menzy



版主


发贴: 754
积分: 113
于 2004-05-11 16:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
wuliang同志在本区积极帮助其他朋友,加分奖励


作者 Re:请教java.security.Signature类对word文件进行数字签名的例程 [Re:alicecyh]
流水年华



发贴: 0
积分: 0
于 2004-05-18 10:58 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923