网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  无需jce用底层api实现开发rsa     
  文章作者:未知  文章来源:水木森林  
  查看:105次  录入:管理员--2007-11-17  
 
  若要自己开发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();
  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);
  }
  }
 
 
上一篇: 建立anonymousftpserver    下一篇: 利用javaswing实现游戏开发
  相关文档
使用jsf(java server faces)开发(四) 11-17
j2se综合:两种java容器类list和set分析 02-29
java 中的异常处理从概念到实例 11-17
java的一些基本概念 11-17
rmi规范--第九章 11-17
浅论java访问com/activex 11-17
java进阶:一个简单thread缓冲池的实现 03-20
小技巧:在.net下编写中文代码程序 11-17
基于spring框架的websphere应用开发 11-16
java中的函数动态调用 11-17
sun力推jds数据库计划开拓数据库疆土 11-17
抢先体验“野马”j2se6.0 11-16
如何将form保存到图片中! 11-17
浮点型(floating-point types) 11-17
java伴侣 11-17
你应该知道的10件关于java 6的事情 11-17
sun 认证考试是否合适于你? 11-17
高级:使用异步servlet扩展ajax应用程序 01-09
java fun and games: java grab包的技术提示 11-17
jar文件高级应用指南 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息