RSA算法简介及简单java实现类

来源:互联网 发布:得力3970拷贝数据 编辑:程序博客网 时间:2024/05/16 18:58

RSA公钥加密算法是1977年由罗纳德·李维斯特(RonRivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的。当时他们三人都在麻省理工学院工作。RSA就是他们三人姓氏开头字母拼在一起组成的。

RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击,已被ISO推荐为公钥数据加密标准。

今天只有短的RSA钥匙才可能被强力方式解破。到2008年为止,世界上还没有任何可靠的攻击RSA算法的方式。只要其钥匙的长度足够长,用RSA加密的信息实际上是不能被解破的。但在分布式计算和量子计算机理论日趋成熟的今天,RSA加密安全性受到了挑战。
RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。

RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。

RSA的算法涉及三个参数,ne1e2

其中,n是两个大质数pq的积,n的二进制表示时所占用的位数,就是所谓的密钥长度。

e1e2是一对相关的值,e1可以任意取,但要求e1(p-1*(q-1互质;再选择e2,要求(e2*e1mod((p-1*(q-1))=1

ne1,(ne2)就是密钥对。其中(ne1)为公钥(ne2)为私钥。[1]

RSA加解密的算法完全相同,设A为明文,B为密文,则:A=B^e2 mod nB=A^e1 mod n;(公钥加密体制中,一般用公钥加密,私钥解密)

e1e2可以互换使用,即:

A=B^e1 mod nB=A^e2 mod n;


以下是根据网络收集,自己整理的RSA算法。

import java.math.BigInteger;import java.security.KeyFactory;import java.security.PrivateKey;import java.security.PublicKey;import java.security.spec.RSAPrivateKeySpec;import java.security.spec.RSAPublicKeySpec;import javax.crypto.Cipher;import Decoder.BASE64Decoder;import Decoder.BASE64Encoder;/** * Rsa加密解密工具类,需加入sun.misc.BASE64Decoder.jar * 加入sun.misc.BASE64Decoder.jar 是为了让加密结果更好传输,也可以不加 *  *  * @author 枫jk * */public class RsaKey {String modulus;Cipher cipher;public RsaKey(String modulus) {this.modulus = modulus;try {//若用Cipher.getInstance("RSA");在android端加密,在服务端解密会报错。//原因网友解释为:android系统的RSA实现是“RSA/ECB/NoPadding”,而标准JDK实现是“RSA/None/PKCS1Padding”,这造成了在android机上加密后无法在服务器上解密的原因//所以指定一种形式的RSA就可以了。cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 获取加密公匙 * @param modulus * @param publicExponent * @return * @throws Exception */public PublicKey getPublicKey(String modulus, String publicExponent)throws Exception {BigInteger m = new BigInteger(modulus);BigInteger e = new BigInteger(publicExponent);RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PublicKey publicKey = keyFactory.generatePublic(keySpec);return publicKey;}/** * 获取加密私匙 * @param modulus * @param privateExponent * @return * @throws Exception */public PrivateKey getPrivateKey(String modulus, String privateExponent)throws Exception {BigInteger m = new BigInteger(modulus);BigInteger e = new BigInteger(privateExponent);RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PrivateKey privateKey = keyFactory.generatePrivate(keySpec);return privateKey;}/** * 得到加密串 * @param text * @param publicExponent * @return */public String getEnRsa(String text,String publicExponent) {String str = null;try {PublicKey publicKey = getPublicKey(modulus, publicExponent);cipher.init(Cipher.ENCRYPT_MODE, publicKey);byte[] enBytes = cipher.doFinal(text.getBytes());str = (new BASE64Encoder()).encode(enBytes);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return str;}/** * 解密得到原文 * @param text * @param privateExponet * @return */public String getDeRsa(String text,String privateExponet) {String str = null;try {PrivateKey privateKey = getPrivateKey(modulus, privateExponet);cipher.init(Cipher.DECRYPT_MODE, privateKey);byte[] enBytes = (new BASE64Decoder()).decodeBuffer(text);byte[] deBytes = cipher.doFinal(enBytes);str = new String(deBytes);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return str;}}



有兴趣的可以去下载源码,跟jar包。http://download.csdn.net/detail/han123456o/6686901