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

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

無需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);
  }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孝义市| 克拉玛依市| 靖宇县| 云林县| 南乐县| 绥化市| 闸北区| 克东县| 枞阳县| 南充市| 莱阳市| 沭阳县| 云南省| 博湖县| 神木县| 攀枝花市| 阳西县| 洪湖市| 林州市| 塘沽区| 盐边县| 阜平县| 鹰潭市| 尼玛县| 灌南县| 长海县| 福州市| 长葛市| 泌阳县| 灵璧县| 太仆寺旗| 广饶县| 平泉县| 万荣县| 保德县| 枝江市| 江安县| 东城区| 集贤县| 金乡县| 海晏县|