简体   繁体   English

生成私钥的问题

[英]Issue in generating private key

Private key generation 私钥生成

          public PrivateKey getStoredPrivateKey(String filePath) {
    PrivateKey privateKey = null;
    byte[] keydata = getKeyData(filePath);
    PKCS8EncodedKeySpec encodedPrivateKey = new PKCS8EncodedKeySpec(keydata);
    KeyFactory keyFactory = null;
    try {
        keyFactory = KeyFactory.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    try {
        System.out.println("hello");
        privateKey = keyFactory.generatePrivate(encodedPrivateKey);
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }
    return privateKey;
}

I am using it here 我在这里用

  PrivateKey privateKey = new KryptoUtil().getStoredPrivateKey(privateKeyFilePath);

but its showing error 但它显示错误

    hello
    java.security.spec.InvalidKeySpecException:                          
    java.security.InvalidKeyException: IOException : version mismatch: (supported:     00, parsed:     03
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(Unknown Source)

I am passing a (.p12) file in getStoredPrivateKey(String filePath) function. 我传递getStoredPrivateKey(字符串文件路径)函数(或.p12)文件。

why its giving error? 为什么给出错误?

P12 is is keystore type where multiple keys and certificates can be stored and a password can be used to protect them. P12是密钥库类型,可以存储多个密钥和证书,并可以使用密码来保护它们。 You can search about P12 (PKCS12) on Internet. 您可以在Internet上搜索有关P12(PKCS12)的信息。 Your file is P12 file, so most likely it is PKCS12 format file. 您的文件是P12文件,因此很可能是PKCS12格式文件。

To get private key from P12 file use below code. 要从P12文件获取私钥,请使用以下代码。 You need below things before calling this code. 在调用此代码之前,您需要具备以下条件。

filePath . filePath String path (absolute) of P12 file. P12文件的字符串路径(绝对)。

filePassword . filePassword It is a char[]. 它是一个char []。 Represents password of p12 file. 代表p12文件的密码。

keyPassword . keyPassword It is a char[]. 它是一个char []。 Represents password for private key. 代表私钥的密码。 Most likely it is same as filePassword. 最有可能与filePassword相同。

alias . 别名 A String. 一个字符串。 Represents by which alias a private key stored in P12 archive/keystore. 代表哪个别名存储在P12存档/密钥库中的私钥。

To check what is the alias of your private key you can use below command 要检查私钥的别名是什么,可以使用以下命令

keytool -list -v -keystore <yourfile>.p12 -storetype pkcs12

It will ask for password then print multiple lines. 它将要求输入密码,然后打印多行。 Look for 寻找

Entry Type: PrivatKeyEntry

There you will find the alias. 在那里您会找到别名。

Initialize these variables and then use below code to get private key. 初始化这些变量,然后使用下面的代码获取私钥。 You can also get Certificates/Public key associate with this key. 您还可以获得与此密钥关联的证书/公共密钥。 Look for API of PrivateKeyEntry 寻找PrivateKeyEntry的API

KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream(filePath), filePassword);
PrivateKeyEntry keyEntry = (PrivateKeyEntry) ks.getEntry(alias, new KeyStore.PasswordProtection(keyPassword));
PrivateKey key = privateKeyEntry.getPrivateKey();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM