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

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

無需 JCE 用底層 API 實現(xiàn)開發(fā) RSA

2019-11-18 12:05:38
字體:
供稿:網(wǎng)友

  若要自己開發(fā)RSA的話那都是用底層API實現(xiàn)的,自然是無需JCE。但有一個問題須說明,那就是你所提到的JDK1.1.8,是否可以正確執(zhí)行我不敢確定,因為我手邊沒有1.1.8的文檔,而我使用的所有API均來自1.2.2.至于1.1.8中是否都一樣,我不知道,但想必沒什么問題。還有一個問題:由于RSA的實現(xiàn)均是純粹的數(shù)學(xué)原理,故其算法當(dāng)然也都是針對數(shù)字的。至于文本或二進(jìn)制代碼當(dāng)然也可以,比如轉(zhuǎn)換為字節(jié)數(shù)組或純二進(jìn)制等,具體使用什么方法最好最快我還沒找到。所以這就留給你自己解決好了。不過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);
  }
  }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 咸丰县| 上杭县| 奇台县| 安化县| 龙岩市| 宝应县| 永寿县| 涿州市| 沙洋县| 濉溪县| 图们市| 杭锦旗| 仙桃市| 陇西县| 始兴县| 江都市| 子洲县| 墨竹工卡县| 桐柏县| 伊宁县| 且末县| 汉沽区| 西盟| 华宁县| 孝感市| 巫山县| 五台县| 岐山县| 平舆县| 岢岚县| 南溪县| 锦州市| 昌邑市| 黎川县| 临沂市| 明星| 建阳市| 沈阳市| 太和县| 高邮市| 衡东县|