国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

在Jini,RMI和Applet中如何實(shí)現(xiàn)代碼簽名

2019-11-18 11:56:53
字體:
供稿:網(wǎng)友

  第一段代碼:生成公開/私有密鑰對并在命令行中指定文件,把密鑰對寫入該文件.
  import java.security.*;
  import java.io.*;
  public class KeyPairGen
  {
  public static void main(String[] args)
  {
  if(args.length!=1)
  {
  System.out.  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();
  }
  }
  第二段代碼,命令行中指定存放密鑰的文件,用于簽名的字符串(這里使用字符串只是為了簡單,其實(shí)在真正實(shí)際使用中應(yīng)該換成用
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();
  }
  }
  第三段代碼當(dāng)然是用于驗(yàn)證簽名了.命令行中指定三個參數(shù).密鑰文件,更新驗(yàn)證的字符串和簽名文件.
  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!");
  }
  }
  }
  在驗(yàn)證過程中,密鑰對,字符串和簽名一個都不能錯,否則無法通過驗(yàn)證.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿瓦提县| 平谷区| 江达县| 大足县| 乐亭县| 丹江口市| 宁城县| 南安市| 建德市| 抚顺县| 筠连县| 江都市| 安阳县| 永善县| 洪洞县| 霍邱县| 象州县| 湟中县| 讷河市| 淄博市| 周宁县| 阿尔山市| 灵璧县| 长子县| 保亭| 林西县| 贵德县| 黄冈市| 远安县| 扎鲁特旗| 甘孜| 濮阳市| 奎屯市| 田林县| 宣恩县| 南汇区| 阜阳市| 湟中县| 民权县| 达拉特旗| 贵溪市|