麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

無需JCE用底層API實現開發RSA

2019-11-18 13:33:33
字體:
來源:轉載
供稿:網友

  若要自己開發RSA的話那都是用底層API實現的,自然是無需JCE。但有一個問題須說明,那就是你所提到的JDK1.1.8,是否可以正確執行我不敢確定,因為我手邊沒有1.1.8的文檔,而我使用的所有API均來自1.2.2.至于1.1.8中是否都一樣,我不知道,但想必沒什么問題。還有一個問題:由于RSA的實現均是純粹的數學原理,故其算法當然也都是針對數字的。至于文本或二進制代碼當然也可以,比如轉換為字節數組或純二進制等,具體使用什么方法最好最快我還沒找到。所以這就留給你自己解決好了。不過RSA主要是理解算法,搞明白了這些其余不在話下。
  這是第一個代碼RSAGenerator,用于生成RSA中的p,q,n,m,e,d并把n,e,d寫入磁盤中的RSAKey.ser文件。
  import java.security.*;
  import java.math.*;
  import java.io.*;
  class RSAInfo implements Serializable
  {
  BigInteger e;
  BigInteger d;
  BigInteger n;
  }
  public class RSAGenerator
  {
  RSAInfo info=new RSAInfo();
  public static void main(String[] args)
  {
  RSAGenerator obj=new RSAGenerator();
  try{
  obj.getParameter();
  obj.writeState();
  }catch(NoSUChAlgorithmException ex)
  {
  System.out.  }
  catch(IOException ex)
  {
  System.out.println("IOException");
  }
  }
  public void getParameter() throws NoSuchAlgorithmException
  {
  int bitlength=100;
  int certainty=50;
  SecureRandom sRandom=SecureRandom.getInstance("SHA1PRNG");
  BigInteger one=new BigInteger("1");
  BigInteger p=new BigInteger(bitlength,certainty,sRandom);
  BigInteger q=new BigInteger(bitlength,certainty,sRandom);
  BigInteger n=p.mult
iply(q);
  BigInteger m=p.suBTract(one).multiply((q.subtract(one)));
  int len=m.bitLength();
  BigInteger e;
  while(true)
  {
  e=new BigInteger(len,sRandom);
  if(m.gcd(e).equals(one))break;
  }
  BigInteger d=e.modInverse(m);
  info.e=e;
  info.d=d;
  info.n=n;
  }
  public void writeState() throws IOException
  {
  FileOutputStream fos=new FileOutputStream("RSAKey.ser");
  ObjectOutputStream oos=new ObjectOutputStream(fos);
  oos.writeObject(info);
  oos.flush();
  fos.close();
  oos.close();
  }
  }
  這是第二個代碼RSAEncrypt,用于加密。
  import java.security.*;
  import java.math.*;
  import java.io.*;
  public class RSAEncrypt
  {
  public static void main(String[] args)
  {
  if(args.length!=1)
  {
  System.out.println("Usage: java RSAEncrypt Number");
  System.exit(1);
  }
  RSAEncrypt obj=new RSAEncrypt();
  try{
  obj.encrypt(args[0]);
  }catch(ClassNotFoundException ex)
  {
  System.out.println("ClassNotFoundException");
  }
  catch(FileNotFoundException ex)
  {
  System.out.println("FileNotFoundException");
  }
  catch(IOException ex)
  {
  System.out.println("IOException");
  }
  }
  public void encrypt(String num) throws ClassNotFoundException,
  FileNotFoundException,IOException
  {
  FileInputStream fis=new FileInputStream("RSAKey.ser");
  ObjectInputStream ois=new ObjectInputStream(fis);
  RSAInfo info=(RSAInfo)ois.readObject();
  BigInteger plainText=new BigInteger(num);
  BigInteger cipherText=plainText.modPow(info.e,info.n);
  System.out.println("Cipher text is:");
  System.out.println(cipherText);
  fis.close();
  ois.close();
  }
  }
  
  這是第三個代碼RSADecrypt,用于從RSAKey.ser中取出e.d.n解密。
  mport java.security.*;
  import java.math.*;
  import java.io.*;
  public class RSADecrypt
  {
  public static void main(String[] args)
  {
  if(args.length!=1)
  {
  System.out.println("Usage: java RSADecrypt Number");
  System.exit(1);
  }
  RSADecrypt obj=new RSADecrypt();
  try{
  obj.decrypt(args[0]);
  }catch(ClassNotFoundException ex)
  {
  System.out.println("ClassNotFoundException");
  }
  catch(FileNotFoundException ex)
  {
  System.out.println("FileNotFoundException");
  }
  catch(IOException ex)
  {
  System.out.println("IOException");
  }
  }
  public void decrypt(String num) throws ClassNotFoundException,
  FileNotFoundException,IOException
  {
    FileInputStream fis=new FileInputStream("RSAKey.ser");
  ObjectInputStream ois=new ObjectInputStream(fis);
  RSAInfo info=(RSAInfo)ois.readObject();
  BigInteger cipherText=new BigInteger(num);
  BigInteger plainText=cipherText.modPow(info.d,info.n);
  System.out.println("Plain text is:");
  System.out.println(plainText);
  }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一区播放 | 毛片视频观看 | 在线无码| 国产美女爽到喷白浆的 | 国产免费传媒av片在线 | 亚洲国产精久久久久久久 | av之家在线观看 | 宅男噜噜噜66国产免费观看 | 蜜桃成品人免费视频 | 免费观看国产精品视频 | 激情宗合网 | 国产福利视频 | 亚洲免费视频一区 | 免费在线观看毛片 | 精品国产91久久久久久久妲己 | 欧美黑人伦理 | 成人免费福利视频 | 久久精品亚洲精品国产欧美kt∨ | 久色亚洲| 黄色高清免费 | www.99xxxx.com| 久久人人做 | 日本黄色大片免费 | 成人免费福利视频 | 国产高清成人久久 | chengrenzaixian| 精品一区二区电影 | 看免费的毛片 | 日韩午夜片 | 一区www| 97porn| 黄色免费小网站 | 免费久久久久 | 久久国产夫妻视频 | 成人在线免费视频观看 | 欧美va亚洲 | 欧美自拍 | 91精品国啪老师啪 | 最新黄色av | 久久99精品久久久久久久久久久久 | 一级性生活免费视频 |