小编Ven*_*hav的帖子

从PEM BASE64获取RSA私钥编码的私钥文件

我有一个私钥文件(PEM BASE64编码).我想用它来解密一些其他数据.使用Java我试图读取文件并解码其中的BASE64编码数据...这是我试过的代码片段....

import java.io.*;
import java.nio.ByteBuffer;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import com.ibm.crypto.fips.provider.RSAPrivateKey;
import com.ibm.misc.BASE64Decoder;

public class GetPrivateKey {
    public static RSAPrivateKey get() throws Exception {
        File privateKeyFile = new File("privatekey.key");
        byte[] encodedKey = new byte[(int) privateKeyFile.length()];
        new FileInputStream(privateKeyFile).read(encodedKey);
        ByteBuffer keyBytes = new BASE64Decoder().decodeBufferToByteBuffer(encodedKey.toString());
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes.array());
        KeyFactory kf = KeyFactory.getInstance("RSA", "IBMJCEFIPS");
        RSAPrivateKey pk = (RSAPrivateKey) kf.generatePrivate(privateKeySpec);
        return pk;
    }

    public static void main(String[] args) throws Exception {
        PrivateKey privKey = FormatMePlease.get();
        System.out.println(privKey.toString());
    }

}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

Exception in thread …
Run Code Online (Sandbox Code Playgroud)

java cryptography certificate pkcs#8 x509

28
推荐指数
6
解决办法
12万
查看次数

标签 统计

certificate ×1

cryptography ×1

java ×1

pkcs#8 ×1

x509 ×1