服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

在jini,rmi和applet中如何实现代码签名


  第一段代码:生成公开/私有密钥对并在命令行中指定文件,把密钥对写入该文件.
  import java.security.*;
  import java.io.*;
  public class keypairgen
  {
  public static void main(string[] args)
  {
  if(args.length!=1)
  {
  system.out.println("usage: java keypairgen keyfile");
  system.exit(1);
  }
  keypairgen obj=new keypairgen();
  try{
  obj.gen(args[0]);
  }catch(nosuchalgorithmexception ex)
  {
  system.out.println("nosuchalgorithmexception");
  }
  catch(filenotfoundexception ex)
  {
  system.out.println("filenotfoundexception");
  }
  catch(ioexception ex)
  {
  system.out.println("ioexception");
  }
  }
  public void gen(string source) throws nosuchalgorithmexception,
  filenotfoundexception,ioexception
  {
  keypairgenerator kpgen=keypairgenerator.getinstance("dsa");
  kpgen.initialize(512);
  keypair kpair=kpgen.genkeypair();
  fileoutputstream fos=new fileoutputstream(source);
  objectoutputstream oos=new objectoutputstream(fos);
  oos.writeobject(kpair);
  fos.close();
  oos.close();
  }
  }
  第二段代码,命令行中指定存放密钥的文件,用于签名的字符串(这里使用字符串只是为了简单,其实在真正实际使用中应该换成用md5或sha1算法计算某一文件流的消息摘要值)和签名所存放的文件.功能是计算出签名并把该签名存放在文件中.
  import java.security.*;
  import java.io.*;
  public class signgen
  {
  public static void main(string[] args)
  {
  if(args.length!=3)
  {
  system.out.println("usage: java signgen keyfile string sigfile");
  system.exit(1);
  }
  signgen obj=new signgen();
  try{
  obj.gensignature(args[0],args[1],args[2]);
  }catch(nosuchalgorithmexception ex)
  {
  system.out.println("nosuchalgorithmexception");
  }
  catch(invalidkeyexception ex)
  {
  system.out.println("invalidkeyexception");
  }
  catch(signatureexception ex)
  {
  system.out.println("signatureexception");
  }
  catch(classnotfoundexception ex)
  {
  system.out.println("classnotfoundexception");
  }
  catch(filenotfoundexception ex)
  {
  system.out.println("filenotfoundexception");
  }
  catch(ioexception ex)
  {
  system.out.println("ioexception");
  }
  }
  public void gensignature(string keyfile,string str,string sigfile)
  throws nosuchalgorithmexception,invalidkeyexception,signatureexception,
  classnotfoundexception,filenotfoundexception,ioexception
  {
  fileinputstream fis=new fileinputstream(keyfile);
  objectinputstream ois=new objectinputstream(fis);
  keypair kp=(keypair)ois.readobject();
  publickey pubkey=kp.getpublic();
  privatekey prikey=kp.getprivate();
  fis.close();
  ois.close();
  signature sig=signature.getinstance("sha1withdsa");
  sig.initsign(prikey);
  sig.update(str.getbytes());
  byte[] b=sig.sign();
  fileoutputstream fos=new fileoutputstream(sigfile);
  objectoutputstream oos=new objectoutputstream(fos);
  oos.writeobject(b);
  fos.close();
  oos.close();
  }
  }
  第三段代码当然是用于验证签名了.命令行中指定三个参数.密钥文件,更新验证的字符串和签名文件.
  import java.security.*;
  import java.io.*;
  public class signverify
  {
  public static void main(string[] args)
  {
  if(args.length!=3)
  {
  system.out.println("usage: java signverify keyfile string sigfile");
  system.exit(1);
  }
  signverify obj=new signverify();
  try{
  obj.verify(args[0],args[1],args[2]);
  }catch(nosuchalgorithmexception ex)
  {
  system.out.println("nosuchalgorithmexception");
  }
  catch(invalidkeyexception ex)
  {
  system.out.println("invalidkeyexception");
  }
  catch(signatureexception ex)
  {
  system.out.println("signatureexception");
  }
  catch(classnotfoundexception ex)
  {
  system.out.println("classnotfoundexception");
  }
  catch(filenotfoundexception ex)
  {
  system.out.println("filenotfoundexception");
  }
  catch(ioexception ex)
  {
  system.out.println("ioexception");
  }
  }
  public void verify(string keyfile,string str,string sigfile) throws
  nosuchalgorithmexception,invalidkeyexception,signatureexception,
  classnotfoundexception,filenotfoundexception,ioexception
  {
  fileinputstream fis=new fileinputstream(keyfile);
  objectinputstream ois=new objectinputstream(fis);
  keypair kp=(keypair)ois.readobject();
  publickey pubkey=kp.getpublic();
  privatekey prikey=kp.getprivate();
  fis.close();
  ois.close();
  fileinputstream fis1=new fileinputstream(sigfile);
  objectinputstream ois1=new objectinputstream(fis1);
  byte[] b=(byte[])ois1.readobject();
  fis1.close();
  ois1.close();
  signature sig=signature.getinstance("sha1withdsa");
  sig.initverify(pubkey);
  sig.update(str.getbytes());
  if(sig.verify(b))
  {
  system.out.println("verify ok!");
  }
  else
  {
  system.out.println("verify error!");
  }
  }
  }
  在验证过程中,密钥对,字符串和签名一个都不能错,否则无法通过验证.

扫描关注微信公众号