rsa加密与解密算法
来源:互联网 发布:英语美文软件下载 编辑:程序博客网 时间:2024/05/15 00:05
实验名称:RSA算法
实验类型: 设计性实验
适用对象: 信息安全
一、实验目的
学习RSA算法,通过编程实现RSA算法,掌握RSA产生公钥和私钥的方法,加深对RSA加密体制的了解,为深入学习密码学奠定基础。
二、实验要求
分析RSA算法的功能需求,详细设计实现RSA算法的数据结构和流程,给出测试用例和测试步骤,得出测试和结论。RSA算法的实现程序必须提供加密和解密两个接口:int encrypt()和int decrypt()。当加密或者解密成功时返回CRYPT_OK,失败时返回CRYPT_ERROR。
三、实验原理
详见书本中关于RSA算法的描述。下图可作为参考
四、实验所需仪器、设备、材料(试剂)
运行Windows或Linux操作系统的PC机,具有gcc(Linux)、java、VC(Windows)等C或java语言编译环境。
java中实现rsa算法
代码为:
package rsa;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Random;
public class rsa {
private static final BigInteger temp = null;
private BigInteger p;
private BigInteger q;
private BigInteger n;
private BigInteger t;
private BigInteger e;
private BigInteger d;
BigInteger s=new BigInteger("1");
public void getpq() throws IOException{
Random rand=new Random();
System.out.println("请输入随机数的位数");
BufferedReader stdi=new BufferedReader(new InputStreamReader(System.in));
String st=stdi.readLine();
int length=Integer.parseInt(st);
System.out.println(length);
this.p= temp.probablePrime(length,rand);
this.q=temp.probablePrime(length,rand);
System.out.println("大素数p的值为:"+p);
System.out.println("大素数q的值为:"+q);
this.n=p.multiply(q);
this.t=(p.subtract(s)).multiply(q.subtract(s));
}
public void geted() throws IOException{
System.out.println("请输入随机数e,作为加密密钥,要求0<e<t,gcd(e,t)=1");
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String str=stdin.readLine();
this.e=new BigInteger(str);
System.out.println("加密密钥为:"+e);
(this.d)=e.modInverse(t);
System.out.println("解密密钥为:"+d);
}
public void jiami() throws IOException{
System.out.println("请输入明文:");
BufferedReader stdi=new BufferedReader(new InputStreamReader(System.in));
String str=stdi.readLine();
BigInteger P=new BigInteger(str);
BigInteger C=P.modPow(e, n);
System.out.println("得到的密文是:"+C);
}
public void jiemi() throws IOException{
System.out.println("请输入密文:");
BufferedReader stdi=new BufferedReader(new InputStreamReader(System.in));
String str=stdi.readLine();
BigInteger C=new BigInteger(str);
BigInteger P=C.modPow(d, n);
System.out.println("得到的名文是:"+P);
}
public static void main(String[] args) throws IOException {
rsa rs=new rsa();
rs.getpq();
rs.geted();
rs.jiami();
rs.jiemi();
}
}
原文:web开发网
- rsa加密与解密算法
- rsa加密与解密算法
- RSA算法原理与加密解密
- RSA加密与解密的算法
- 加密解密---------->RSA算法
- RSA加密解密算法
- RSA加密解密算法
- RSA算法----加密解密
- RSA加密与解密
- RSA加密与解密
- Rsa 加密与解密
- RSA加密与解密
- RSA加密与解密
- RSA加密与解密
- RSA加密与解密
- Android-RSA算法加密解密
- RSA加密解密算法Java
- C#实现字符串RSA加密与解密算法
- Java交换两个变量的值的三种方法
- PHP常见错误汇总
- 1048 Inverso
- 超好看的table样式格行换色加单击事件
- 在windows下安装ubuntu 12.04的方法
- rsa加密与解密算法
- 天易26----java导出excel表格(支持wps和office excel)
- c语言变参函数设计
- 小知识点
- shell操作数据库:高级查询
- 通过分析执行时间优化SQL语句
- 关于BM算法
- 給Joomla網站創建Google+1
- 使用UIAutomation进行 iOS 自动化测试