6月 122015
 

生成keystore文件

keytool -genkey -alias test -keyalg RSA -keystore /key.store

RSA是一个既能用于数据加密也能用于数字签名的算法。
DSA(Digital Signature Algorithm,数字签名算法,用作数字签名标准的一部分),它是另一种公开密钥算法,它不能用作加密,只用作数字签名。DSA使用公开密钥,为接受者验证数据的完整性和数据发送者的身份。

提取证书的方法:
keytool -export -alias 别名 -keystore 文件名 -file 证书名称

但是我们无法通过KEYTOOL工具来提取私钥的..我们只能通过java的KeyStore类getEntry() 或者getKey()来提取私钥.

1. 导出私钥参考下面例子中的export方法

2. 怎么来验证提取的私钥是否正确呢 (因为公钥私钥必须成对出现,我们可以通过证书提取去公钥,然后用公钥加密,使用刚刚获得的私钥解密) ,参考代码中check方法

代码如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

import javax.crypto.Cipher;

import sun.misc.BASE64Encoder;

public class ExportPrivateKey {
private File keystoreFile;
private String keyStoreType;
private char[] password;
private String alias;
private File exportedFile;

public static KeyPair getPrivateKey(KeyStore keystore, String alias,
char[] password) {
try {
Key key = keystore.getKey(alias, password);
if (key instanceof PrivateKey) {
Certificate cert = keystore.getCertificate(alias);
PublicKey publicKey = cert.getPublicKey();
return new KeyPair(publicKey, (PrivateKey) key);
}
} catch (UnrecoverableKeyException e) {
} catch (NoSuchAlgorithmException e) {
} catch (KeyStoreException e) {
}
return null;
}

public void export() throws Exception {
KeyStore keystore = KeyStore.getInstance(keyStoreType);
BASE64Encoder encoder = new BASE64Encoder();
keystore.load(new FileInputStream(keystoreFile), password);
KeyPair keyPair = getPrivateKey(keystore, alias, password);
PrivateKey privateKey = keyPair.getPrivate();
String encoded = encoder.encode(privateKey.getEncoded());
FileWriter fw = new FileWriter(exportedFile);
fw.write(“—–BEGIN RSA PRIVATE KEY—–n”);
fw.write(encoded);
fw.write(“n”);
fw.write(“—–END RSA PRIVATE KEY—–“);
fw.close();
}

public void check(){
//通过证书,获取公钥
 CertificateFactory cf;
try {
cf = CertificateFactory.getInstance(“X.509”);
FileInputStream in = new FileInputStream(“/home/ouu/keystore/server.cer”);
//生成一个证书对象并使用从输入流 inStream 中读取的数据对它进行初始化。
Certificate c = cf.generateCertificate(in);
PublicKey publicKey = c.getPublicKey();

       KeyStore keystore = KeyStore.getInstance(keyStoreType);
BASE64Encoder encoder = new BASE64Encoder();
keystore.load(new FileInputStream(keystoreFile), password);
KeyPair keyPair = getPrivateKey(keystore, alias, password);
PrivateKey privateKey = keyPair.getPrivate();

//通过下面这段代码提取的私钥是否正确
String before = “asdf”;
       byte[] plainText = before.getBytes(“UTF-8”);
       Cipher cipher = Cipher.getInstance(“RSA/ECB/PKCS1Padding”);
       cipher.init(Cipher.ENCRYPT_MODE, publicKey);
       // 用公钥进行加密,返回一个字节流
       byte[] cipherText = cipher.doFinal(plainText);
       cipher.init(Cipher.DECRYPT_MODE, privateKey);
       // 用私钥进行解密,返回一个字节流
       byte[] newPlainText = cipher.doFinal(cipherText);
       System.out.println(new String(newPlainText, “UTF-8”)); 
       
       

       

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String args[]) throws Exception {
ExportPrivateKey export = new ExportPrivateKey();
export.keystoreFile = new File(
“/home/ouu/keystore/server.keystore”);
export.keyStoreType = “JKS”;
export.password = “123456”.toCharArray();
export.alias = “server”;
export.exportedFile = new File(“server.key”);
export.export(); //导出私钥匙
export.check();  //检测私钥正确性
}
}







Sorry, the comment form is closed at this time.