请问怎样生成RSA密钥对并且以文件形式保存?
谢谢!
3 个解决方案
#1
cryptoAPI里面有现成的算法,write到文件里面就可以了
#2
package test.com.ebao.life.DES;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
/**
* <p>
* Title: RSA非对称型加密的公钥和私钥
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2003
* </p>
* <p>
* Company:
* </p>
*
* @author not attributable
* @version 1.0
*/
public class GenerateKeyRSA {
private KeyPairGenerator kpg = null;
private KeyPair kp = null;
private PublicKey public_key = null;
private PrivateKey private_key = null;
private FileOutputStream public_file_out = null;
private ObjectOutputStream public_object_out = null;
private FileOutputStream private_file_out = null;
private ObjectOutputStream private_object_out = null;
/**
* 构造函数
*
* @param in
* 指定密匙长度(取值范围:512~2048)
* @throws NoSuchAlgorithmException
* 异常
*/
public GenerateKeyRSA(int in)
throws NoSuchAlgorithmException, FileNotFoundException, IOException {
kpg = KeyPairGenerator.getInstance("RSA"); // 创建‘密匙对’生成器
kpg.initialize(in); // 指定密匙长度(取值范围:512~2048)
kp = kpg.genKeyPair(); // 生成‘密匙对’,其中包含着一个公匙和一个私匙的信息
public_key = kp.getPublic(); // 获得公匙
private_key = kp.getPrivate(); // 获得私匙
}
/**
* 获得加密密钥
* @return
*/
public PrivateKey getPrivateKey(){
return this.private_key;
}
/**
* 获得加密公钥
* @return
*/
public PublicKey getPublicKey(){
return this.public_key;
}
/**
* 获得密钥对
* @return
*/
public KeyPair getKeyPair(){
return this.kp;
}
/**
* 保存私匙
* @param address
* @throws Exception
*/
public void savePrivateKey(String address) throws IOException{
try {
private_file_out = new FileOutputStream(address + "/private_key.dat");
private_object_out = new ObjectOutputStream(private_file_out);
private_object_out.writeObject(public_key);
} catch (IOException e) {
throw e;
}
}
/**
* 保存公匙
* @param address
* @throws Exception
*/
public void savePublicKey(String address) throws IOException{
try {
public_file_out = new FileOutputStream(address + "/public_key.dat");
public_object_out = new ObjectOutputStream(public_file_out);
public_object_out.writeObject(public_key);
} catch (IOException e) {
throw e;
}
}
public static void main(String[] args) {
try {
System.out.println("私匙和公匙保存到C盘下的文件中.");
GenerateKeyRSA app=new GenerateKeyRSA(1024);
app.savePublicKey("c:/");
app.savePrivateKey("c:/");
} catch (IOException ex) {
} catch (NoSuchAlgorithmException ex) {
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
/**
* <p>
* Title: RSA非对称型加密的公钥和私钥
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2003
* </p>
* <p>
* Company:
* </p>
*
* @author not attributable
* @version 1.0
*/
public class GenerateKeyRSA {
private KeyPairGenerator kpg = null;
private KeyPair kp = null;
private PublicKey public_key = null;
private PrivateKey private_key = null;
private FileOutputStream public_file_out = null;
private ObjectOutputStream public_object_out = null;
private FileOutputStream private_file_out = null;
private ObjectOutputStream private_object_out = null;
/**
* 构造函数
*
* @param in
* 指定密匙长度(取值范围:512~2048)
* @throws NoSuchAlgorithmException
* 异常
*/
public GenerateKeyRSA(int in)
throws NoSuchAlgorithmException, FileNotFoundException, IOException {
kpg = KeyPairGenerator.getInstance("RSA"); // 创建‘密匙对’生成器
kpg.initialize(in); // 指定密匙长度(取值范围:512~2048)
kp = kpg.genKeyPair(); // 生成‘密匙对’,其中包含着一个公匙和一个私匙的信息
public_key = kp.getPublic(); // 获得公匙
private_key = kp.getPrivate(); // 获得私匙
}
/**
* 获得加密密钥
* @return
*/
public PrivateKey getPrivateKey(){
return this.private_key;
}
/**
* 获得加密公钥
* @return
*/
public PublicKey getPublicKey(){
return this.public_key;
}
/**
* 获得密钥对
* @return
*/
public KeyPair getKeyPair(){
return this.kp;
}
/**
* 保存私匙
* @param address
* @throws Exception
*/
public void savePrivateKey(String address) throws IOException{
try {
private_file_out = new FileOutputStream(address + "/private_key.dat");
private_object_out = new ObjectOutputStream(private_file_out);
private_object_out.writeObject(public_key);
} catch (IOException e) {
throw e;
}
}
/**
* 保存公匙
* @param address
* @throws Exception
*/
public void savePublicKey(String address) throws IOException{
try {
public_file_out = new FileOutputStream(address + "/public_key.dat");
public_object_out = new ObjectOutputStream(public_file_out);
public_object_out.writeObject(public_key);
} catch (IOException e) {
throw e;
}
}
public static void main(String[] args) {
try {
System.out.println("私匙和公匙保存到C盘下的文件中.");
GenerateKeyRSA app=new GenerateKeyRSA(1024);
app.savePublicKey("c:/");
app.savePrivateKey("c:/");
} catch (IOException ex) {
} catch (NoSuchAlgorithmException ex) {
}
}
}
#3
通用的做法是写到KeyStore里面,JKS的格式,可以通过
keytool -list -v -keystore来list里面的keyentry
你看看KeyFactory以及SecureX的CertStore类
keytool -list -v -keystore来list里面的keyentry
你看看KeyFactory以及SecureX的CertStore类
#1
cryptoAPI里面有现成的算法,write到文件里面就可以了
#2
package test.com.ebao.life.DES;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
/**
* <p>
* Title: RSA非对称型加密的公钥和私钥
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2003
* </p>
* <p>
* Company:
* </p>
*
* @author not attributable
* @version 1.0
*/
public class GenerateKeyRSA {
private KeyPairGenerator kpg = null;
private KeyPair kp = null;
private PublicKey public_key = null;
private PrivateKey private_key = null;
private FileOutputStream public_file_out = null;
private ObjectOutputStream public_object_out = null;
private FileOutputStream private_file_out = null;
private ObjectOutputStream private_object_out = null;
/**
* 构造函数
*
* @param in
* 指定密匙长度(取值范围:512~2048)
* @throws NoSuchAlgorithmException
* 异常
*/
public GenerateKeyRSA(int in)
throws NoSuchAlgorithmException, FileNotFoundException, IOException {
kpg = KeyPairGenerator.getInstance("RSA"); // 创建‘密匙对’生成器
kpg.initialize(in); // 指定密匙长度(取值范围:512~2048)
kp = kpg.genKeyPair(); // 生成‘密匙对’,其中包含着一个公匙和一个私匙的信息
public_key = kp.getPublic(); // 获得公匙
private_key = kp.getPrivate(); // 获得私匙
}
/**
* 获得加密密钥
* @return
*/
public PrivateKey getPrivateKey(){
return this.private_key;
}
/**
* 获得加密公钥
* @return
*/
public PublicKey getPublicKey(){
return this.public_key;
}
/**
* 获得密钥对
* @return
*/
public KeyPair getKeyPair(){
return this.kp;
}
/**
* 保存私匙
* @param address
* @throws Exception
*/
public void savePrivateKey(String address) throws IOException{
try {
private_file_out = new FileOutputStream(address + "/private_key.dat");
private_object_out = new ObjectOutputStream(private_file_out);
private_object_out.writeObject(public_key);
} catch (IOException e) {
throw e;
}
}
/**
* 保存公匙
* @param address
* @throws Exception
*/
public void savePublicKey(String address) throws IOException{
try {
public_file_out = new FileOutputStream(address + "/public_key.dat");
public_object_out = new ObjectOutputStream(public_file_out);
public_object_out.writeObject(public_key);
} catch (IOException e) {
throw e;
}
}
public static void main(String[] args) {
try {
System.out.println("私匙和公匙保存到C盘下的文件中.");
GenerateKeyRSA app=new GenerateKeyRSA(1024);
app.savePublicKey("c:/");
app.savePrivateKey("c:/");
} catch (IOException ex) {
} catch (NoSuchAlgorithmException ex) {
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
/**
* <p>
* Title: RSA非对称型加密的公钥和私钥
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2003
* </p>
* <p>
* Company:
* </p>
*
* @author not attributable
* @version 1.0
*/
public class GenerateKeyRSA {
private KeyPairGenerator kpg = null;
private KeyPair kp = null;
private PublicKey public_key = null;
private PrivateKey private_key = null;
private FileOutputStream public_file_out = null;
private ObjectOutputStream public_object_out = null;
private FileOutputStream private_file_out = null;
private ObjectOutputStream private_object_out = null;
/**
* 构造函数
*
* @param in
* 指定密匙长度(取值范围:512~2048)
* @throws NoSuchAlgorithmException
* 异常
*/
public GenerateKeyRSA(int in)
throws NoSuchAlgorithmException, FileNotFoundException, IOException {
kpg = KeyPairGenerator.getInstance("RSA"); // 创建‘密匙对’生成器
kpg.initialize(in); // 指定密匙长度(取值范围:512~2048)
kp = kpg.genKeyPair(); // 生成‘密匙对’,其中包含着一个公匙和一个私匙的信息
public_key = kp.getPublic(); // 获得公匙
private_key = kp.getPrivate(); // 获得私匙
}
/**
* 获得加密密钥
* @return
*/
public PrivateKey getPrivateKey(){
return this.private_key;
}
/**
* 获得加密公钥
* @return
*/
public PublicKey getPublicKey(){
return this.public_key;
}
/**
* 获得密钥对
* @return
*/
public KeyPair getKeyPair(){
return this.kp;
}
/**
* 保存私匙
* @param address
* @throws Exception
*/
public void savePrivateKey(String address) throws IOException{
try {
private_file_out = new FileOutputStream(address + "/private_key.dat");
private_object_out = new ObjectOutputStream(private_file_out);
private_object_out.writeObject(public_key);
} catch (IOException e) {
throw e;
}
}
/**
* 保存公匙
* @param address
* @throws Exception
*/
public void savePublicKey(String address) throws IOException{
try {
public_file_out = new FileOutputStream(address + "/public_key.dat");
public_object_out = new ObjectOutputStream(public_file_out);
public_object_out.writeObject(public_key);
} catch (IOException e) {
throw e;
}
}
public static void main(String[] args) {
try {
System.out.println("私匙和公匙保存到C盘下的文件中.");
GenerateKeyRSA app=new GenerateKeyRSA(1024);
app.savePublicKey("c:/");
app.savePrivateKey("c:/");
} catch (IOException ex) {
} catch (NoSuchAlgorithmException ex) {
}
}
}
#3
通用的做法是写到KeyStore里面,JKS的格式,可以通过
keytool -list -v -keystore来list里面的keyentry
你看看KeyFactory以及SecureX的CertStore类
keytool -list -v -keystore来list里面的keyentry
你看看KeyFactory以及SecureX的CertStore类