RSA非对称加密和解密方法
来源:互联网 发布:大数据变革哪些方面 编辑:程序博客网 时间:2024/06/05 00:50
RSA公匙和密匙生成方法
package org.markerking;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
public abstract class RSAKey
...{
/** *//**
* 生成2048位的RSA公匙和私匙
*/
public static void generator(
String privateKeyFileName) throws Exception
...{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.generateKeyPair();
PublicKey puk = kp.getPublic();
PrivateKey prk = kp.getPrivate();
FileOutputStream fos1 = new FileOutputStream("publicRSAKey.dat");
FileOutputStream fos2 = new FileOutputStream(privateKeyFileName);
ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
oos1.writeObject(puk);
oos2.writeObject(prk);
oos1.close();
oos2.close();
fos1.close();
fos2.close();
System.out.println("公匙生成成功!公匙文件为publicRSAKey.dat");
System.out.println("私匙生成成功!私匙文件为" + privateKeyFileName);
}
/** *//**
* 获取已经生成的公匙
*
* @return RSAPublicKey
*/
protected static RSAPublicKey getPublicKey(String publicKeyFileName)
throws Exception
...{
FileInputStream fis = new FileInputStream(publicKeyFileName);
ObjectInputStream ois = new ObjectInputStream(fis);
RSAPublicKey rsapuk = (RSAPublicKey) ois.readObject();
return rsapuk;
}
/** *//**
* 获取已经生成的私匙
*
* @return RSAPrivateKey
*/
protected static RSAPrivateKey getPrivateKey(String privateKeyFileName)
throws Exception
...{
FileInputStream fis = new FileInputStream(privateKeyFileName);
ObjectInputStream ois = new ObjectInputStream(fis);
RSAPrivateKey psaprk = (RSAPrivateKey) ois.readObject();
return psaprk;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
public abstract class RSAKey
...{
/** *//**
* 生成2048位的RSA公匙和私匙
*/
public static void generator(
String privateKeyFileName) throws Exception
...{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.generateKeyPair();
PublicKey puk = kp.getPublic();
PrivateKey prk = kp.getPrivate();
FileOutputStream fos1 = new FileOutputStream("publicRSAKey.dat");
FileOutputStream fos2 = new FileOutputStream(privateKeyFileName);
ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
oos1.writeObject(puk);
oos2.writeObject(prk);
oos1.close();
oos2.close();
fos1.close();
fos2.close();
System.out.println("公匙生成成功!公匙文件为publicRSAKey.dat");
System.out.println("私匙生成成功!私匙文件为" + privateKeyFileName);
}
/** *//**
* 获取已经生成的公匙
*
* @return RSAPublicKey
*/
protected static RSAPublicKey getPublicKey(String publicKeyFileName)
throws Exception
...{
FileInputStream fis = new FileInputStream(publicKeyFileName);
ObjectInputStream ois = new ObjectInputStream(fis);
RSAPublicKey rsapuk = (RSAPublicKey) ois.readObject();
return rsapuk;
}
/** *//**
* 获取已经生成的私匙
*
* @return RSAPrivateKey
*/
protected static RSAPrivateKey getPrivateKey(String privateKeyFileName)
throws Exception
...{
FileInputStream fis = new FileInputStream(privateKeyFileName);
ObjectInputStream ois = new ObjectInputStream(fis);
RSAPrivateKey psaprk = (RSAPrivateKey) ois.readObject();
return psaprk;
}
}
调用方法
package org.markerking;
import java.math.BigInteger;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
/** *//**
* RSA非对称式加密与解密
*
* @author MarkerKing
* @version v1.0
* @time 2007-2-23 21:19
*/
public class RSAUtil
...{
/** *//**
* 加密方法
*
* @param String
* 需要加密的数据
* @return String 加密后的数据
*/
public String S2RSA(String str) throws Exception
...{
// 获得公匙
RSAPublicKey rsapuk = RSAKey.getPublicKey("publicRSAKey.dat");
// 获得公匙参数e,n
BigInteger e = rsapuk.getPublicExponent();
BigInteger n = rsapuk.getModulus();
// 转换明文m
byte[] btext = str.getBytes("GB2312");
BigInteger m = new BigInteger(btext);
// 计算密文
BigInteger c = m.modPow(e, n);
// 返回
return c.toString();
}
/** *//**
* 解密方法
*
* @param String
* 私匙文件
* @param String
* 加密后的字符串
* @return String 解密后的字符串
*/
public String RSA2S(String fileName, String rsaStr) throws Exception
...{
// 获取私匙
RSAPrivateKey rsaprk = RSAKey.getPrivateKey(fileName);
// 获取私匙参数d,n
BigInteger d = rsaprk.getPrivateExponent();
BigInteger n = rsaprk.getModulus();
// 获取密文
BigInteger c = new BigInteger(rsaStr);
// 解密计算
BigInteger m = c.modPow(d, n);
// 转换成String
byte[] ctext = m.toByteArray();
return new String(ctext);
}
}
import java.math.BigInteger;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
/** *//**
* RSA非对称式加密与解密
*
* @author MarkerKing
* @version v1.0
* @time 2007-2-23 21:19
*/
public class RSAUtil
...{
/** *//**
* 加密方法
*
* @param String
* 需要加密的数据
* @return String 加密后的数据
*/
public String S2RSA(String str) throws Exception
...{
// 获得公匙
RSAPublicKey rsapuk = RSAKey.getPublicKey("publicRSAKey.dat");
// 获得公匙参数e,n
BigInteger e = rsapuk.getPublicExponent();
BigInteger n = rsapuk.getModulus();
// 转换明文m
byte[] btext = str.getBytes("GB2312");
BigInteger m = new BigInteger(btext);
// 计算密文
BigInteger c = m.modPow(e, n);
// 返回
return c.toString();
}
/** *//**
* 解密方法
*
* @param String
* 私匙文件
* @param String
* 加密后的字符串
* @return String 解密后的字符串
*/
public String RSA2S(String fileName, String rsaStr) throws Exception
...{
// 获取私匙
RSAPrivateKey rsaprk = RSAKey.getPrivateKey(fileName);
// 获取私匙参数d,n
BigInteger d = rsaprk.getPrivateExponent();
BigInteger n = rsaprk.getModulus();
// 获取密文
BigInteger c = new BigInteger(rsaStr);
// 解密计算
BigInteger m = c.modPow(d, n);
// 转换成String
byte[] ctext = m.toByteArray();
return new String(ctext);
}
}
- RSA非对称加密和解密方法
- RSA非对称加密解密
- RSA非对称加密解密
- 非对称RSA加密解密
- RSA加密、解密、签名(非对称加密)
- RSA 非对称加密与解密
- RSA非对称加密解密例子
- Android RSA非对称加密解密算法
- Android: AndroidKeyStore 非对称RSA加密解密
- Java对称与非对称加密解密,AES与RSA
- Java对称与非对称加密解密,AES与RSA
- Java对称与非对称加密解密,AES与RSA
- php rsa加密解密实例(非对称加密)
- android RSA加密 解密 非对称可逆加密
- RSA 非对称加密
- RSA非对称加密
- RSA非对称加密
- RSA非对称加密
- 各种数据库JDBC驱动下载网址
- DESedeKey
- 仅通过崩溃地址找出源代码的出错行
- 深入理解静态链表(,物理上是一个结构体数组,逻辑上是两个链表,)执行一下,,,
- shell编程快速入门
- RSA非对称加密和解密方法
- 用C#编写ActiveX控件
- 关于微软的OBA架构理解(欢迎大家批评指正^_^)
- 五月的开发心情历程 2007.5.11 ,Eddy
- oracle用语句实现desc功能
- 证明题: 票子=孩子
- C++ 虚继承与普通继承的内存模型对比
- 整理加密解密部分方法
- javascript由于编码不同造成乱码的问题