import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.HashMap;
import javax.crypto.Cipher;
public class RSAUtils {
/**
* 生成公钥和私钥
* @throws NoSuchAlgorithmException
*
*/
public static HashMap<String, Object> getKeys() throws NoSuchAlgorithmException{
HashMap<String, Object> map = new HashMap<String, Object>();
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
map.put("public", publicKey);
map.put("private", privateKey);
return map;
}
/**
* 使用模和指数生成RSA公钥
* 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
* /None/NoPadding】
*
* @param modulus
* 模
* @param exponent
* 指数
* @return
*/
public static RSAPublicKey getPublicKey(String modulus, String exponent) {
try {
BigInteger b1 = new BigInteger(modulus);
BigInteger b2 = new BigInteger(exponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 使用模和指数生成RSA私钥
* 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
* /None/NoPadding】
*
* @param modulus
* 模
* @param exponent
* 指数
* @return
*/
public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {
try {
BigInteger b1 = new BigInteger(modulus);
BigInteger b2 = new BigInteger(exponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 公钥加密
*
* @param data
* @param publicKey
* @return
* @throws Exception
*/
public static String encryptByPublicKey(String data, RSAPublicKey publicKey)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 模长
int key_len = publicKey.getModulus().bitLe
数据结构:JAVA 实现 RSA 算法 数据结构 实现一对密钥对整个项目所有加密解密文件都适用的方法,采用先生成一对密钥.保存到 xml 文件中,以后获得私匙和公钥只需要从...
RSA算法的Java实现田文春韦岗 (华南理工大学电子与信息学院,广州510641)E—mall:wenchnntian@2Icn.con 擒耍网络服务的丰富,尤其是电子商务的发展,需要强健的密码...
用java编程实现RSA算法_IT/计算机_专业资料。用java编程实现RSA算法网络与通信 !""#$%% 大众科技 !& 用 !"#" 编程实现 $%& 算法 !肖卫雄 !佛山职业技术...
用Java2实现RSA算法_专业资料。RSA算法是被应用得最多的公开密钥算法之一。本文从RSA算法的特点、加密算法、解密算法、密钥的产生等方面介绍RSA算法,并用Java2...
用Java语言编程实现RSA算法_IT/计算机_专业资料。用Java语言编程实现RSA算法2008年4月第4期(总第1 13期) 广西轻工业GUANGXIJOURNALOFLIGHTINDUSTRY 计算机与信息...
RSA密码算法及其Java实现_专业资料。RSA密码算法是目前公认的,在理论和实际应用中最为成熟和完善的一种非对称密钥体制。该文对其原理进行了介绍,并证明了该算法的...
RSA加密算法java编程实现_工学_高等教育_教育专区 暂无评价0人阅读0次下载举报文档 RSA加密算法java编程实现_工学_高等教育_教育专区。RSA加密算法java编程实现...
javacard实现RSA算法_计算机软件及应用_IT/计算机_专业资料。javacard测试RSA算法的源码例子今日推荐 88份文档 2014年全国注册造价工程师...
用JAVA语言实现RSA公钥密码算法_专业资料。本文阐述了公开密钥密码体制RSA算法的原理及实现技术。并在此基础上,给出了JAVA语言实现的RSA算法源代码。...
热门文档