网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.reverse 方法.
.Java2源码解读:java.util.Array.
.在.NET框架中保留应用程序设置.
.Web 服务代理组件创建 JSF Web 服.
.Java初学者如何迈出AOP第一步.
.使用JSF(Java Server Faces)开.
.选择排序算法的JAVA实现.
.虚拟机监视器Xen和虚拟化技术(一.
.了解Struts1.1介绍.
.命令行参数.
.2. Building Model Components.
.Swing是MVC设计的典范.
.Java中“异常机制”深入研究.
.在应用程序中处理jar文件.
.Eclipse工具下开发Struts解决乱码.
.发布您的最新基于Eclipse的产品一.
.J2SE中的序列化之接受默认序列化.
.EJB 自管理的事务.
.JAVA中正则表达式的应用(1).
.实现Java平台的三种方式v.

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

发表日期:2008-1-5



  若要自己开发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);
  }
  }
上一篇:一个关于抛出异常的程序代码的分析 人气:1271
下一篇:如何在Microsoft.NET中自定义配置文件 人气:829
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐