<一>基础
RSA算法非常简单,概述如下:
找两素数p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一个数e,要求满足e<t并且e与t互素(就是最大公因数为1)
取d*e%t==1
(其中,公钥的exponent即RSA算法中的e, e通常是3,17和65537
X.509建议使用65537,PEM建议使用3,PKCS#1建议使用3或65537,一般来说,都是选择3。)
这样最终得到三个数: n d e
设消息为数M (M <n)
设c=(M**d)%n就得到了加密后的消息c
设m=(c**e)%n则 m == M,从而完成对c的解密。
注:**表示次方,上面两式中的d和e可以互换。
在对称加密中:
n e两个数构成公钥,可以告诉别人;
n d两个数构成私钥,d自己保留,不让任何人知道。
给别人发送的信息使用d加密,只要别人能用e解开就证明信息是由你发送的
别人给你发送信息时使用e加密,这样只有拥有d的你能够对其解密。
rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解
从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法
求得d。
以上原理详见维基百科:http://zh.wikipedia.org/wiki/RSA%E5%8A%A0%E5%AF%86%E6%BC%94%E7%AE%97%E6%B3%95#.E5.85.AC.E9.92.A5.E5.92.8C.E7.A7.81.E9.92.A5.E7.9A.84.E4.BA.A7.E7.94.9F
<二>实践
package com.yajun.rsa;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.SecureRandom;
/**
* RSA 算法原理实现
*
* @author Administrator
*/
public class RSA implements Serializable {
private static final long serialVersionUID = 7935341891569534021L;
private final static BigInteger one = BigInteger.ONE;
private final static SecureRandom random = new SecureRandom();
private BigInteger privateKey = null;
private BigInteger publicKey = null;
private BigInteger modulus = null;
private BigInteger p1 = null;
private BigInteger p2 = null;
//Ces variables doivent être initialisé pour l'encrytage de données.
private BigInteger modulusE;
private BigInteger publicKeyE;
private int N;
private BigInteger phi0;
public RSA(int N) {
this.N = N;
// generate an N-bit (roughly) public and private key
//clés privé
//p1
p1 = BigInteger.probablePrime(N / 2, random);
//p2
p2 = BigInteger.probablePrime(N / 2, random);
//t
phi0 = (p1.subtract(one)).multiply(p2.subtract(one));
//n
modulus = p1.multiply(p2);
//d
setPrivateKey();
//e
publicKey = privateKey.modInverse(phi0);
modulusE = modulus;
publicKeyE = publicKey;
}
public BigInteger getModulus() {
return modulus;
}
public BigInteger getPublicKey() {
return publicKey;
}
public void setPublicKey(BigInteger p, BigInteger n) {
publicKeyE = p;
modulusE = n;
}
/**
* 这部分没怎么看懂
*/
private void setPrivateKey() {
do {
privateKey = BigInteger.probablePrime(N / 2, random);
} while (privateKey.gcd(phi0).intValue() != 1 || privateKey.compareTo(modulus) != -1
|| privateKey.compareTo(p1.max(p2)) == -1);
}
/**
* 加密
*
* @param message
* @return
*/
public BigInteger encrypt(BigInteger message) {
BigInteger rep = null;
String str_message = new String(message.toByteArray());
if (message != null) {
if (str_message.length() <= (N / 8)) {
if (publicKeyE != null && modulusE != null
&& message.toByteArray().length < Integer.MAX_VALUE) {
rep = message.modPow(publicKeyE, modulusE);
}
}
}
return rep;
}
/**
* 加密,公式比较容易看懂
*
* @param message
* @param publicKeyP
* @param modulusP
* @return
*/
public BigInteger encrypt(BigInteger message, BigInteger publicKeyP, BigInteger modulusP) {
BigInteger rep = null;
String str_message = new String(message.toByteArray());
if (str_message.length() <= (N / 8)) {
if (publicKeyP != null && modulusP != null
&& message.toByteArray().length < Integer.MAX_VALUE) {
rep = message.modPow(publicKeyP, modulusP);
}
}
return rep;
}
/**
* 解密,公式也比较容易看懂
*
* @param encrypted
* @return
*/
public BigInteger decrypt(BigInteger encrypted) {
return encrypted.modPow(privateKey, modulus);
}
public String toString() {
String s = "";
s += "public = " + publicKey + "\n";
s += "modulus = " + modulus;
return s;
}
public static void main(String[] args) {
BigInteger message = BigInteger.valueOf(88);
RSA rsa = new RSA(512);
BigInteger enInteger = rsa.encrypt(message);
BigInteger deInteger = rsa.decrypt(enInteger);
System.out.println(enInteger);
System.out.println(deInteger);
}
}
分享到:
相关推荐
RSA算法原理; RSA算法原理; RSA算法原理; RSA算法原理; RSA算法原理;
RSA算法原理.doc
主要对网络中数据传输rsa加密的原理介绍,包括数学理论和代码开发应用
阐述了RSA的原理-RSA密钥生成原理 RSA安全性依据
RSA算法原理及应用示例.doc
rsa算法原理[参考].pdf
RSA算法原理与C语言实现 运行环境:WINDOWS下VC6.0及以上编程工具 运行方式:(1)WINDOWS下VC6.0及以上编程工具编译链接运行 (2)工程文件夹下Debug下的*.exe
RSA算法原理与实现_密码学源代码_C语言程序_C++程序源代码
关于RSA加密算法的原理。详细介绍了什么是RSA加密算法,及其加密过程。
RSA算法是世界上第一个既能用于数据加密也能用于数字签名的非对称性加密算法。它易于理解和操作,所以流行甚广。算法的名字以发明者的名字命名,他们是:Ron Rivest,Adi Shamir 和Leonard Adleman。虽然RSA的安全性...
RSA算法的纯Python实现,...RSA算法原理基于两个大质数的乘积很难因式分解,几种算法的优劣主要体现在质数判断、快速乘模运算、快速幂模运算等。如需实际应用建议使用大能们的实现:https://pypi.python.org/pypi/rsa/
RSA密码系统是较早提出的一种公开钥密码系统。1978年,美国麻自理工学院(MIT)的Rivest,Shamir和Adleman在题为“获得数字签名和公开钥...RSA是建立在“大整数的素因子分解是困难问题”基础上的,是一种分组密码体制。
C语言实现RSA 加密过程。中间包括原理和实现过程,有助学学习密码学的同志们交流学习。
项目需要编写的MFC测试小程序(编译环境VS2015) 1、实现RSA算法密匙n、e、d 的自动生成; 2、根据当前的密匙对输入数字进行加解密... 5、RSA算法原理可参考https://blog.csdn.net/wm_1991/article/details/51954565
RSA密码系统是较早提出的一种公开钥密码系统。1978年,美国麻自理工学院(MIT)的Rivest,Shamir和Adleman在题为“获得数字签名和公开钥...RSA是建立在“大整数的素因子分解是困难问题”基础上的,是一种分组密码体制。
RSA素数算法详细讲解,RSA加密原理与实现。
RSA算法由原理到实现
一篇详细介绍RSA算法的文章,从数论讲到算法实现,还有各种针对RSA的攻击方式,可以说讲的很详细
RSA-AlgorithmRSA算法演示程序,仅供了解RSA算法实现原理RSA算法原理找出两个"很大"的质数:P & QN = P * QM = (P - 1) * (Q - 1)找出整数E,E与M互质,即除了1之外,没有其他公约数找出整数D,使得E*D除以M余1,即 ...
密码学 RSA 算法 c语言源码 大数运算 实现原理 很不错的 运行过