Java无需 JCE 用底层 API 实现开发 RSA

分类: J2SE   出处:iocblog整理  更新时间:2009-10-20   添加到收藏  


  若要自己开发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.println("nosuchalgorithmexception");
  }
  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.multiply(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();[iocblog.net 来源]
  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);
  }
  }[iocblog.net 来源]


Tag: RSA ,JCE



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。