关于java中rsa的问题
帮我注释一下这个网址的代码,因为看不太懂,,,谢谢,,衷心表示感谢了http://blog.csdn.net/yangcp1/article/details/534806...
帮我注释一下这个网址的代码,因为看不太懂,,,谢谢,,衷心表示感谢了http://blog.csdn.net/yangcp1/article/details/5348066
展开
展开全部
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class RSATest {
public static void main(String[] args) {
try {
RSATest encrypt = new RSATest();
String encryptText = "encryptText";
//产生一对RSA密钥对
KeyPair keyPair = encrypt.generateKey();
//密钥对中的私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
//密钥对中的公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
//加密数据(encryptTest)
byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes());
//解密数据(e)
byte[] de = encrypt.decrypt(privateKey, e);
//打印输出加密的数据(e)
System.out.println(toHexString(e));
//打印输出解密的数据(de)
System.out.println(toHexString(de));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成长度为1024的密钥对
* @Date 2015-11-3,上午11:35:00
* @author hw
* @throws NoSuchAlgorithmException
* @return KeyPair
*/
public KeyPair generateKey() throws NoSuchAlgorithmException {
//初始化密码对生成器("RSA",密钥算法标识)
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
//初始化密码对生成器("1024", 密钥长度, 还支持2048,4096长度,第二个参数是个随机数)
keyPairGen.initialize(1024, new SecureRandom());
//使用密码对生成器,产生一对密钥
KeyPair keyPair = keyPairGen.generateKeyPair();
return keyPair;
}
/**
* 保存密钥对
* @Date 2015-11-3,上午11:37:11
* @author hw
* @param keyPair 密钥对
* @param publicKeyFile 公钥保存文件
* @param privateKeyFile 私钥保存文件
* @throws ConfigurationException
* @return void
*/
public void saveKey(KeyPair keyPair, String publicKeyFile,
String privateKeyFile) throws ConfigurationException {
PublicKey pubkey = keyPair.getPublic();
PrivateKey prikey = keyPair.getPrivate();
//保存公钥
PropertiesConfiguration publicConfig = new PropertiesConfiguration(
publicKeyFile);
publicConfig.setProperty("PULIICKEY", toHexString(pubkey.getEncoded()));
publicConfig.save();
//保存私钥
PropertiesConfiguration privateConfig = new PropertiesConfiguration(
privateKeyFile);
privateConfig.setProperty("PRIVATEKEY",
toHexString(prikey.getEncoded()));
privateConfig.save();
}
/**
* 加载密钥
* @Date 2015-11-3,上午11:39:06
* @author hw
* @param filename 密钥文件
* @param type 密钥类型:0:公钥 1:私钥
* @throws ConfigurationException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @return Key
*/
public Key loadKey(String filename, int type)
throws ConfigurationException, NoSuchAlgorithmException,
InvalidKeySpecException {
//读取密钥文件
PropertiesConfiguration config = new PropertiesConfiguration(filename);
//构造密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance("RSA",
new BouncyCastleProvider());
if (type == 0) {
//读取公钥数据
String privateKeyValue = config.getString("PULIICKEY");
//字符串格式公钥转公钥对象
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
toBytes(privateKeyValue));
PrivateKey privateKey = keyFactory.generatePrivate(priPKCS8);
return privateKey;
} else {
//读取私钥数据
String privateKeyValue = config.getString("PRIVATEKEY");
//字符串格式私钥转私钥对象
X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(
toBytes(privateKeyValue));
PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);
return publicKey;
}
}
/**
* 加密数据
* @Date 2015-11-3,上午11:41:24
* @author hw
* @param publicKey 公钥
* @param data 要加密数据
* @return byte[]
*/
protected byte[] encrypt(RSAPublicKey publicKey, byte[] data) {
if (publicKey != null) {
try {
//密钥算法
Cipher cipher = Cipher.getInstance("RSA",
new BouncyCastleProvider());
//设置为:加密模式
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//加密源数据,并返回加密后数据
return cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 解密数据
* @Date 2015-11-3,上午11:42:32
* @author hw
* @param privateKey 私钥
* @param raw 加密数据
* @return byte[]
*/
protected byte[] decrypt(RSAPrivateKey privateKey, byte[] raw) {
if (privateKey != null) {
try {
//密钥算法
Cipher cipher = Cipher.getInstance("RSA",
new BouncyCastleProvider());
//设置为:解密模式
cipher.init(Cipher.DECRYPT_MODE, privateKey);
//解密加密数据,并返回源数据
return cipher.doFinal(raw);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* byte[] 转 16进制字符串
* @Date 2015-11-3,上午11:43:40
* @author hw
* @param b
* @return String
*/
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]);
sb.append(HEXCHAR[b[i] & 0x0f]);
}
return sb.toString();
}
/**
* 16进制字符串转byte[]
* @Date 2015-11-3,上午11:44:08
* @author hw
* @param s
* @return byte[]
*/
public static final byte[] toBytes(String s) {
byte[] bytes;
bytes = new byte[s.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
16);
}
return bytes;
}
/** 16进制数 */
private static char[] HEXCHAR = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class RSATest {
public static void main(String[] args) {
try {
RSATest encrypt = new RSATest();
String encryptText = "encryptText";
//产生一对RSA密钥对
KeyPair keyPair = encrypt.generateKey();
//密钥对中的私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
//密钥对中的公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
//加密数据(encryptTest)
byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes());
//解密数据(e)
byte[] de = encrypt.decrypt(privateKey, e);
//打印输出加密的数据(e)
System.out.println(toHexString(e));
//打印输出解密的数据(de)
System.out.println(toHexString(de));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成长度为1024的密钥对
* @Date 2015-11-3,上午11:35:00
* @author hw
* @throws NoSuchAlgorithmException
* @return KeyPair
*/
public KeyPair generateKey() throws NoSuchAlgorithmException {
//初始化密码对生成器("RSA",密钥算法标识)
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
//初始化密码对生成器("1024", 密钥长度, 还支持2048,4096长度,第二个参数是个随机数)
keyPairGen.initialize(1024, new SecureRandom());
//使用密码对生成器,产生一对密钥
KeyPair keyPair = keyPairGen.generateKeyPair();
return keyPair;
}
/**
* 保存密钥对
* @Date 2015-11-3,上午11:37:11
* @author hw
* @param keyPair 密钥对
* @param publicKeyFile 公钥保存文件
* @param privateKeyFile 私钥保存文件
* @throws ConfigurationException
* @return void
*/
public void saveKey(KeyPair keyPair, String publicKeyFile,
String privateKeyFile) throws ConfigurationException {
PublicKey pubkey = keyPair.getPublic();
PrivateKey prikey = keyPair.getPrivate();
//保存公钥
PropertiesConfiguration publicConfig = new PropertiesConfiguration(
publicKeyFile);
publicConfig.setProperty("PULIICKEY", toHexString(pubkey.getEncoded()));
publicConfig.save();
//保存私钥
PropertiesConfiguration privateConfig = new PropertiesConfiguration(
privateKeyFile);
privateConfig.setProperty("PRIVATEKEY",
toHexString(prikey.getEncoded()));
privateConfig.save();
}
/**
* 加载密钥
* @Date 2015-11-3,上午11:39:06
* @author hw
* @param filename 密钥文件
* @param type 密钥类型:0:公钥 1:私钥
* @throws ConfigurationException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @return Key
*/
public Key loadKey(String filename, int type)
throws ConfigurationException, NoSuchAlgorithmException,
InvalidKeySpecException {
//读取密钥文件
PropertiesConfiguration config = new PropertiesConfiguration(filename);
//构造密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance("RSA",
new BouncyCastleProvider());
if (type == 0) {
//读取公钥数据
String privateKeyValue = config.getString("PULIICKEY");
//字符串格式公钥转公钥对象
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
toBytes(privateKeyValue));
PrivateKey privateKey = keyFactory.generatePrivate(priPKCS8);
return privateKey;
} else {
//读取私钥数据
String privateKeyValue = config.getString("PRIVATEKEY");
//字符串格式私钥转私钥对象
X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(
toBytes(privateKeyValue));
PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);
return publicKey;
}
}
/**
* 加密数据
* @Date 2015-11-3,上午11:41:24
* @author hw
* @param publicKey 公钥
* @param data 要加密数据
* @return byte[]
*/
protected byte[] encrypt(RSAPublicKey publicKey, byte[] data) {
if (publicKey != null) {
try {
//密钥算法
Cipher cipher = Cipher.getInstance("RSA",
new BouncyCastleProvider());
//设置为:加密模式
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//加密源数据,并返回加密后数据
return cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 解密数据
* @Date 2015-11-3,上午11:42:32
* @author hw
* @param privateKey 私钥
* @param raw 加密数据
* @return byte[]
*/
protected byte[] decrypt(RSAPrivateKey privateKey, byte[] raw) {
if (privateKey != null) {
try {
//密钥算法
Cipher cipher = Cipher.getInstance("RSA",
new BouncyCastleProvider());
//设置为:解密模式
cipher.init(Cipher.DECRYPT_MODE, privateKey);
//解密加密数据,并返回源数据
return cipher.doFinal(raw);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* byte[] 转 16进制字符串
* @Date 2015-11-3,上午11:43:40
* @author hw
* @param b
* @return String
*/
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]);
sb.append(HEXCHAR[b[i] & 0x0f]);
}
return sb.toString();
}
/**
* 16进制字符串转byte[]
* @Date 2015-11-3,上午11:44:08
* @author hw
* @param s
* @return byte[]
*/
public static final byte[] toBytes(String s) {
byte[] bytes;
bytes = new byte[s.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
16);
}
return bytes;
}
/** 16进制数 */
private static char[] HEXCHAR = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询