java exportpriv 下载_ExportPriv.java

习胤运
2023-12-01

// How to export the private key from keystore?

// Does keytool not have an option to do so?

// This example use the "testkeys" file that comes with JSSE 1.0.3

// Alexey Zilber: Ported to work with Base64Coder: http://www.source-code.biz/snippets/java/2.htm

// $Id: ExportPriv.java 10 2011-09-30 17:28:32Z mark@g.foster.cc $

// $URL: https://java-exportpriv.googlecode.com/svn/trunk/ExportPriv.java $

import java.io.File;

import java.io.FileInputStream;

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.util.Scanner;

import java.util.Vector;

class ExportPriv {

public static void main(String args[]) throws Exception{

/* if (args.length < 2) {

//Yes I know this sucks (the password is visible to other users via ps

// but this was a quick-n-dirty fix to export from a keystore to pkcs12

// someday I may fix, but for now it'll have to do.

System.err.println("Usage: java ExportPriv ");

System.exit(1);

}*/

ExportPriv myep = new ExportPriv();

// System.out.println("Args: " + args[0] + " " + args[1] + " " + args[2]);

System.out.print("输入keystore文件路径:");

Scanner scan=new Scanner(System.in);

String keyStorePath=scan.nextLine();

System.out.print("输入证书别名:");

String alias=scan.nextLine();

System.out.print("证书密码:");

String pass=scan.nextLine();

scan.close();

// System.out.println(keyStorePath+",1");

// System.out.println(alias+"2");

// System.out.println(pass+"3");

//myep.doit("D:\\Program Files\\apache-tomcat-8.0.23\\bin\\.keystore", "baynet", "123abc");

System.out.println();

System.err.println("请将以下打印的文字保存为以.key结尾的文件即私钥文件");

//将输出的信息(包括begin/end)保存为一个以.key结尾的文件就是私钥文件

myep.doit(keyStorePath, alias, pass);

}

public void doit(String fileName, String aliasName, String pass) throws Exception{

KeyStore ks = KeyStore.getInstance("JKS");

char[] passPhrase = pass.toCharArray();

//BASE64Encoder myB64 = new BASE64Encoder();

File certificateFile = new File(fileName);

// System.out.println("certificateFile: " + certificateFile);

ks.load(new FileInputStream(certificateFile), passPhrase);

// System.out.println("ks: " + ks);

// for(String a : ks.aliases()) {

// System.out.println("alias: " + a);

// }

KeyPair kp = getPrivateKey(ks, aliasName, passPhrase);

// System.out.println("kp: " + kp);

PrivateKey privKey = kp.getPrivate();

char[] b64 = Base64Coder.encode(privKey.getEncoded());

System.out.println("-----BEGIN PRIVATE KEY-----");

for (String subSeq : splitArray(b64, 64)) {

System.out.println(subSeq.toCharArray());

}

System.out.println("-----END PRIVATE KEY-----");

}

// From http://javaalmanac.com/egs/java.security/GetKeyFromKs.html

public KeyPair getPrivateKey(KeyStore keystore, String alias, char[] password) {

try {

// Get private key

Key key = keystore.getKey(alias, password);

if (key instanceof PrivateKey) {

// Get certificate of public key

Certificate cert = keystore.getCertificate(alias);

// Get public key

PublicKey publicKey = cert.getPublicKey();

// Return a key pair

return new KeyPair(publicKey, (PrivateKey)key);

}

} catch (UnrecoverableKeyException e) {

} catch (NoSuchAlgorithmException e) {

} catch (KeyStoreException e) {

}

return null;

}

private Vector splitArray(char[] chry, int subarrLen) {

Vector result = new Vector();

String input = new String(chry);

int i = 0;

while (i < chry.length) {

result.add(input.substring(i, Math.min(input.length(), i + subarrLen)));

i = i + subarrLen;

}

return result;

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

 类似资料: