InvalidKeySpecException:algid 解析错误,不是序列

时间:2023-01-15
本文介绍了InvalidKeySpecException:algid 解析错误,不是序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在编写一个程序,它接受来自控制台的输入 - zip 文件的名称、要制作的 zip 文件的名称,其中包含从第一个 zip 生成的(de/en)加密文件和一个包含公钥.解密时出现异常:

线程main"中的异常 java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence在 sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)在 java.security.KeyFactory.generatePrivate(KeyFactory.java:372)在 com.Main.makePrivateKey(Main.java:148)在 com.Main.decrypt(Main.java:40)在 com.Main.main(Main.java:182)引起:java.security.InvalidKeyException:IOException:algid解析错误,不是序列在 sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:351)在 sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356)在 sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:91)在 sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75)在 sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316)在 sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213)... 4 更多

不知道为什么,因为我在 makePrivateKey 方法中使用了正确的规范.我正在测试的键是(一个单独的文件中的一个键的一行):

公钥:

<预> <代码> MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCE3pA746UfpC8sFk8ZJp0yupyJqj5jy6cjdxUYoP7mCm7c0mqQDeCcDNBYW2eSozCioPrH/9L + CDQEPLYakoem + jFnUKDH5 + PRU/0PJTJJF8Xh/ZT9eJlvsYBr1/qSfICf6RTs7kzwq9IuSZBw7/tfNEF9i0A8FVox6HOopXod1QIDAQAB

私钥:

 <代码> MIICXQIBAAKBgQCE3pA746UfpC8sFk8ZJp0yupyJqj5jy6cjdxUYoP7mCm7c0mqQDeCcDNBYW2eSozCioPrH/9L + CDQEPLYakoem + jFnUKDH5 + PRU/0PJTJJF8Xh/ZT9eJlvsYBr1/qSfICf6RTs7kzwq9IuSZBw7/tfNEF9i0A8FVox6HOopXod1QIDAQABAoGANOFrYBqK5lvu1koOswDWQZFZqcSSzh8IZyoGwGWa7S0r0EECXlDXmuPSq8e9IfRG8ALHrH + ZlrbnFOSgyVSWHfpj3aH + qknoSX5TW2rMQHih8865xuqheMQ + RTZ7 + BRDqNsYkzxB/Z8mqzpoJQSYf + H7nWxdDCgAJVYZzxl3DmUCQQD32iEjnwiwUjii8slcmvCEZl + z84DWNdvJOg6Z38sI4AvrfpKc1WAcDg1rNZCKrRgokh54wpLt08cpFcrD04c3AkEAiTzDmc0bdgfg5wj6xHFZpYlBwiGm/bjOR2PS57P0GNU5PsDllRbFqIuzArITutO5lvZZImzuYz7Lf + cQ73pxUwJBAOdEwmdaneDo17A0m2 + TO3/nhqWDMVSwLMU3RyiNigZeCMFU + bkd4PBMrHi9IoJDwacZsRU9eZwxYEUV8H2Jg0ECQEEkOqRSm2pXKwX/WSjNtQPCNxhy6NUeV6vDUmTxIjh3XYjP/ynZeVEbnoj1BjB0N2/U11Jj6nPpZqb7gyppMEkCQQCoGdVYDipU+hMMnvxa0zOIyQc/a+HE0lESqn+2ZPafYi9Z1RldRMvUXhP8U7s+OuhRwprdw2ivvOFrnWyz9lL2

该程序的代码如下.欢迎任何帮助:)

包com;导入 java.io.BufferedReader;导入java.io.File;导入 java.io.FileInputStream;导入 java.io.FileOutputStream;导入 java.io.FileReader;导入 java.io.IOException;导入 java.io.InputStream;导入 java.io.OutputStream;导入 java.security.GeneralSecurityException;导入 java.security.KeyFactory;导入 java.security.PrivateKey;导入 java.security.PublicKey;导入 java.security.Security;导入 java.security.spec.PKCS8EncodedKeySpec;导入 java.security.spec.X509EncodedKeySpec;导入 java.util.Base64;导入 java.util.Enumeration;导入 java.util.Scanner;导入 java.util.zip.ZipEntry;导入 java.util.zip.ZipFile;导入 java.util.zip.ZipOutputStream;导入 javax.crypto.Cipher;公共类主要{公共最终静态 int BUFFER_SIZE = 117;公共静态无效解密(字符串 originalZipFileName,字符串 newZipFileName,字符串 privateKeyFileName)抛出异常 {字节[] 缓冲区 = 新字节[BUFFER_SIZE];ZipFile originalZipFile = new ZipFile(originalZipFileName);ZipOutputStream newZipFile = new ZipOutputStream(new FileOutputStream(newZipFileName));枚举zipEntries = originalZipFile.entries();字符串 privateKey = getKeyString(privateKeyFileName);PrivateKey key = makePrivateKey(privateKey);密码密码 = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, key);文件 file = new File("temp.txt");而(zipEntries.hasMoreElements()){ZipEntry 条目 = zipEntries.nextElement();ZipEntry 副本 = 新 ZipEntry(entry.getName());newZipFile.putNextEntry(复制);整数读取;InputStream inputEntry = originalZipFile.getInputStream(entry);OutputStream outputFile = new FileOutputStream(file);而((读取= inputEntry.read(缓冲区))!= -1){outputFile.write(cipher.doFinal(buffer), 0, read);}InputStream inputTempFile = new FileInputStream(file);而((读取= inputTempFile.read(缓冲区))!= -1){newZipFile.write(缓冲区,0,读取);}newZipFile.closeEntry();inputEntry.close();inputTempFile.close();输出文件.close();文件.删除();}newZipFile.close();}公共静态无效加密(字符串 originalZipFileName,字符串 newZipFileName,字符串 publicKeyFileName)抛出异常 {字节[] 缓冲区 = 新字节[BUFFER_SIZE];ZipFile originalZipFile = new ZipFile(originalZipFileName);ZipOutputStream newZipFile = new ZipOutputStream(new FileOutputStream(newZipFileName));枚举zipEntries = originalZipFile.entries();字符串 publicKey = getKeyString(publicKeyFileName);PublicKey 密钥 = makePublicKey(publicKey);密码密码 = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE,密钥);文件 file = new File("temp.txt");而(zipEntries.hasMoreElements()){ZipEntry 条目 = zipEntries.nextElement();ZipEntry 副本 = 新 ZipEntry(entry.getName());newZipFile.putNextEntry(复制);整数读取;InputStream inputEntry = originalZipFile.getInputStream(entry);OutputStream outputFile = new FileOutputStream(file);而((读取= inputEntry.read(缓冲区))!= -1){outputFile.write(cipher.doFinal(buffer), 0, read);}InputStream inputTempFile = new FileInputStream(file);而((读取= inputTempFile.read(缓冲区))!= -1){newZipFile.write(缓冲区,0,读取);}newZipFile.closeEntry();inputEntry.close();inputTempFile.close();输出文件.close();文件.删除();}newZipFile.close();}公共静态字符串 getKeyString(字符串文件名){字符串键 = 新字符串();尝试 {BufferedReader buf = new BufferedReader(new FileReader(fileName));键 = buf.readLine();} 捕捉(IOException e){e.printStackTrace();}返回 key.trim();}公共静态 PublicKey makePublicKey(字符串存储)抛出 GeneralSecurityException {byte[] data = Base64.getDecoder().decode(stored);X509EncodedKeySpec 规范 = 新 X509EncodedKeySpec(data);KeyFactory 事实 = KeyFactory.getInstance("RSA");返回 fact.generatePublic(spec);}公共静态 PrivateKey makePrivateKey(字符串存储)抛出 GeneralSecurityException {byte[] data = Base64.getDecoder().decode(stored);PKCS8EncodedKeySpec 规范 = 新 PKCS8EncodedKeySpec(data);KeyFactory 事实 = KeyFactory.getInstance("RSA");返回 fact.generatePrivate(spec);}公共静态 void main(String[] args) 抛出异常 {扫描仪扫描 = 新的扫描仪(System.in);System.out.println("输入操作类型:");字符串线 = scan.nextLine();如果(line.equals(加密")){System.out.println("输入原始ZIP文件名:");字符串 originalZipFileName = scan.nextLine();System.out.println("请输入新 ZIP 文件的名称:");字符串 newZipFileName = scan.nextLine();System.out.println("请输入包含公钥的文件名:");字符串 publicKeyFileName = scan.nextLine();加密(originalZipFileName,newZipFileName,publicKeyFileName);}如果(line.equals(解密")){System.out.println("输入原始ZIP文件名:");字符串 originalZipFileName = scan.nextLine();System.out.println("请输入新 ZIP 文件的名称:");字符串 newZipFileName = scan.nextLine();System.out.println("请输入包含私钥的文件名:");String privateKeyFileName = scan.nextLine();解密(originalZipFileName,newZipFileName,privateKeyFileName);}}}

解决方案

您的私钥不是 PKCS#8 格式.您必须向其添加关键算法信息以使其成为 PKCS#8,然后它才能工作.

您可能应该更改生成密钥的方式,以使它们采用 PKCS#8 格式.但由于我对此一无所知,我将只展示如何使用 BouncyCastle 库在您的应用程序中更新密钥:

@Test公共 void testKey() 抛出异常 {串privKeyStr =MIICXQIBAAKBgQCE3pA746UfpC8sFk8ZJp0yupyJqj5jy6cjdxUYoP7mCm7c0mqQDeCcDNBYW2eSozCioPrH/9L + CDQEPLYakoem + jFnUKDH5 + PRU/0PJTJJF8Xh/ZT9eJlvsYBr1/qSfICf6RTs7kzwq9IuSZBw7/tfNEF9i0A8FVox6HOopXod1QIDAQABAoGANOFrYBqK5lvu1koOswDWQZFZqcSSzh8IZyoGwGWa7S0r0EECXlDXmuPSq8e9IfRG8ALHrH + ZlrbnFOSgyVSWHfpj3aH + qknoSX5TW2rMQHih8865xuqheMQ + RTZ7 + BRDqNsYkzxB/Z8mqzpoJQSYf + H7nWxdDCgAJVYZzxl3DmUCQQD32iEjnwiwUjii8slcmvCEZl + z84DWNdvJOg6Z38sI4AvrfpKc1WAcDg1rNZCKrRgokh54wpLt08cpFcrD04c3AkEAiTzDmc0bdgfg5wj6xHFZpYlBwiGm/bjOR2PS57P0GNU5PsDllRbFqIuzArITutO5lvZZImzuYz7Lf + cQ73pxUwJBAOdEwmdaneDo17A0m2 + TO3/nhqWDMVSwLMU3RyiNigZeCMFU + bkd4PBMrHi9IoJDwacZsRU9eZwxYEUV8H2Jg0ECQEEkOqRSm2pXKwX/WSjNtQPCNxhy6NUeV6vDUmTxIjh3XYjP/ynZeVEbnoj1BjB0N2/U11Jj6nPpZqb7gyppMEkCQQCoGdVYDipU+hMMnvxa0zOIyQc/a+HE0lESqn+2ZPafYi9Z1RldRMvUXhP8U7s+OuhRwprdw2ivvOFrnWyz9lL2";byte[] data = Base64.getDecoder().decode(privKeyStr);/* 添加 PKCS#8 格式 */ASN1EncodableVector v = new ASN1EncodableVector();v.add(new ASN1Integer(0));ASN1EncodableVector v2 = new ASN1EncodableVector();v2.add(new ASN1ObjectIdentifier(PKCSObjectIdentifiers.rsaEncryption.getId()));v2.add(DERNull.INSTANCE);v.add(新 DERSequence(v2));v.add(新DEROCtetString(数据));ASN1Sequence seq = new DERSequence(v);byte[] privKey = seq.getEncoded("DER");PKCS8EncodedKeySpec 规范 = 新 PKCS8EncodedKeySpec(privKey);KeyFactory 事实 = KeyFactory.getInstance("RSA");PrivateKey key = fact.generatePrivate(spec);Assert.assertNotNull("生成私钥失败", key);}

I'm writing a program which takes as input from the console - the name of a zip file, name of a zip file to be made containig the (de/en)crypted files generated from the first zip and a file containing the public key. I get the exception when decrypting:

Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
at com.Main.makePrivateKey(Main.java:148)
at com.Main.decrypt(Main.java:40)
at com.Main.main(Main.java:182)
Caused by: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:351)
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356)
at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:91)
at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75)
at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316)
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213)
... 4 more

Can't figure out why because the I use the rigth spec in the makePrivateKey method. The keys I'm testing with are(one line for a key each in a separate file):

Public key:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCE3pA746UfpC8sFk8ZJp0yupyJqj5jy6cjdxUYoP7mCm7c0mqQDeCcDNBYW2eSozCioPrH/9L+CDQEPLYakoem+jFnUKDH5+pru/0PJTJJF8Xh/ZT9eJlvsYBr1/qSfICf6RTs7kzwq9IuSZBw7/tfNEF9i0A8FVox6HOopXod1QIDAQAB

Private key:

MIICXQIBAAKBgQCE3pA746UfpC8sFk8ZJp0yupyJqj5jy6cjdxUYoP7mCm7c0mqQDeCcDNBYW2eSozCioPrH/9L+CDQEPLYakoem+jFnUKDH5+pru/0PJTJJF8Xh/ZT9eJlvsYBr1/qSfICf6RTs7kzwq9IuSZBw7/tfNEF9i0A8FVox6HOopXod1QIDAQABAoGANOFrYBqK5lvu1koOswDWQZFZqcSSzh8IZyoGwGWa7S0r0EECXlDXmuPSq8e9IfRG8ALHrH+ZlrbnFOSgyVSWHfpj3aH+qknoSX5TW2rMQHih8865xuqheMQ+RTZ7+BRDqNsYkzxB/Z8mqzpoJQSYf+H7nWxdDCgAJVYZzxl3DmUCQQD32iEjnwiwUjii8slcmvCEZl+z84DWNdvJOg6Z38sI4AvrfpKc1WAcDg1rNZCKrRgokh54wpLt08cpFcrD04c3AkEAiTzDmc0bdgfg5wj6xHFZpYlBwiGm/bjOR2PS57P0GNU5PsDllRbFqIuzArITutO5lvZZImzuYz7Lf+cQ73pxUwJBAOdEwmdaneDo17A0m2+to3/nhqWDMVSwLMU3RyiNigZeCMFU+bkd4PBMrHi9IoJDwacZsRU9eZwxYEUV8H2Jg0ECQEEkOqRSm2pXKwX/WSjNtQPCNxhy6NUeV6vDUmTxIjh3XYjP/ynZeVEbnoj1BjB0N2/U11Jj6nPpZqb7gyppMEkCQQCoGdVYDipU+hMMnvxa0zOIyQc/a+HE0lESqn+2ZPafYi9Z1RldRMvUXhP8U7s+OuhRwprdw2ivvOFrnWyz9lL2

The code for the program is bellow . Any help is wellcomed :)

package com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import javax.crypto.Cipher;

public class Main {

public final static int BUFFER_SIZE = 117;

public static void decrypt(String originalZipFileName, String newZipFileName, String privateKeyFileName) throws Exception {
    byte[] buffer = new byte[BUFFER_SIZE];  

    ZipFile originalZipFile = new ZipFile(originalZipFileName); 
    ZipOutputStream newZipFile = new ZipOutputStream(new FileOutputStream(newZipFileName));

    Enumeration<? extends ZipEntry> zipEntries = originalZipFile.entries();

    String privateKey = getKeyString(privateKeyFileName);
    PrivateKey key = makePrivateKey(privateKey);

    Cipher cipher = Cipher.getInstance("RSA");

    cipher.init(Cipher.DECRYPT_MODE, key);
    File file = new File("temp.txt");

    while(zipEntries.hasMoreElements()){

        ZipEntry entry = zipEntries.nextElement();          

        ZipEntry copy = new ZipEntry(entry.getName());      
        newZipFile.putNextEntry(copy);

        int read;           
        InputStream inputEntry = originalZipFile.getInputStream(entry);
        OutputStream outputFile = new FileOutputStream(file);

        while((read = inputEntry.read(buffer)) != -1){              
            outputFile.write(cipher.doFinal(buffer), 0, read);
        }

        InputStream inputTempFile = new FileInputStream(file);

        while((read = inputTempFile.read(buffer)) != -1){
            newZipFile.write(buffer, 0, read);
        }

        newZipFile.closeEntry();
        inputEntry.close();
        inputTempFile.close();
        outputFile.close();
        file.delete();
    }
    newZipFile.close();
}

public static void encrypt(String originalZipFileName, String newZipFileName, String publicKeyFileName) throws Exception{

    byte[] buffer = new byte[BUFFER_SIZE];  

    ZipFile originalZipFile = new ZipFile(originalZipFileName); 
    ZipOutputStream newZipFile = new ZipOutputStream(new FileOutputStream(newZipFileName));

    Enumeration<? extends ZipEntry> zipEntries = originalZipFile.entries();

    String publicKey = getKeyString(publicKeyFileName);
    PublicKey key = makePublicKey(publicKey);

    Cipher cipher = Cipher.getInstance("RSA");

    cipher.init(Cipher.ENCRYPT_MODE, key);
    File file = new File("temp.txt");

    while(zipEntries.hasMoreElements()){

        ZipEntry entry = zipEntries.nextElement();          

        ZipEntry copy = new ZipEntry(entry.getName());      
        newZipFile.putNextEntry(copy);

        int read;           
        InputStream inputEntry = originalZipFile.getInputStream(entry);
        OutputStream outputFile = new FileOutputStream(file);

        while((read = inputEntry.read(buffer)) != -1){              
            outputFile.write(cipher.doFinal(buffer), 0, read);
        }

        InputStream inputTempFile = new FileInputStream(file);

        while((read = inputTempFile.read(buffer)) != -1){
            newZipFile.write(buffer, 0, read);
        }

        newZipFile.closeEntry();
        inputEntry.close();
        inputTempFile.close();
        outputFile.close();
        file.delete();
    }
    newZipFile.close();
}   

public static String getKeyString(String fileName){

    String key = new String();
    try {
        BufferedReader buf = new BufferedReader(new FileReader(fileName));
        key = buf.readLine();       
    } catch ( IOException e) {
        e.printStackTrace();
    }   

    return key.trim();
}

public static PublicKey makePublicKey(String stored) throws GeneralSecurityException {
    byte[] data = Base64.getDecoder().decode(stored);
    X509EncodedKeySpec spec = new  X509EncodedKeySpec(data);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    return fact.generatePublic(spec);
}

public static PrivateKey makePrivateKey(String stored) throws GeneralSecurityException {
    byte[] data = Base64.getDecoder().decode(stored);
    PKCS8EncodedKeySpec spec = new  PKCS8EncodedKeySpec(data);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    return fact.generatePrivate(spec);
}

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

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter type of operation:");
    String line = scan.nextLine();

    if(line.equals("encrypt")){
        System.out.println("Enter name of original ZIP file:");
        String originalZipFileName = scan.nextLine();

        System.out.println("Enter name of new ZIP file:");
        String newZipFileName = scan.nextLine();

        System.out.println("Enter name of file containg public key:");
        String publicKeyFileName = scan.nextLine();

        encrypt(originalZipFileName, newZipFileName, publicKeyFileName);        
    }
    if(line.equals("decrypt")){
        System.out.println("Enter name of original ZIP file:");
        String originalZipFileName = scan.nextLine();

        System.out.println("Enter name of new ZIP file:");
        String newZipFileName = scan.nextLine();

        System.out.println("Enter name of file containg private key:");
        String privateKeyFileName = scan.nextLine();

        decrypt(originalZipFileName, newZipFileName, privateKeyFileName);       
    }       

}

}

解决方案

Your private key is not in PKCS#8 format. You have to add key algorithm information to it to make it PKCS#8 and then it will work.

Probably you should alter the way you generate keys to have them in PKCS#8 format. But since I know nothing on that I will just show how to update key to it in your application using BouncyCastle library:

@Test
public void testKey() throws Exception {
    String privKeyStr = "MIICXQIBAAKBgQCE3pA746UfpC8sFk8ZJp0yupyJqj5jy6cjdxUYoP7mCm7c0mqQDeCcDNBYW2eSozCioPrH/9L+CDQEPLYakoem+jFnUKDH5+pru/0PJTJJF8Xh/ZT9eJlvsYBr1/qSfICf6RTs7kzwq9IuSZBw7/tfNEF9i0A8FVox6HOopXod1QIDAQABAoGANOFrYBqK5lvu1koOswDWQZFZqcSSzh8IZyoGwGWa7S0r0EECXlDXmuPSq8e9IfRG8ALHrH+ZlrbnFOSgyVSWHfpj3aH+qknoSX5TW2rMQHih8865xuqheMQ+RTZ7+BRDqNsYkzxB/Z8mqzpoJQSYf+H7nWxdDCgAJVYZzxl3DmUCQQD32iEjnwiwUjii8slcmvCEZl+z84DWNdvJOg6Z38sI4AvrfpKc1WAcDg1rNZCKrRgokh54wpLt08cpFcrD04c3AkEAiTzDmc0bdgfg5wj6xHFZpYlBwiGm/bjOR2PS57P0GNU5PsDllRbFqIuzArITutO5lvZZImzuYz7Lf+cQ73pxUwJBAOdEwmdaneDo17A0m2+to3/nhqWDMVSwLMU3RyiNigZeCMFU+bkd4PBMrHi9IoJDwacZsRU9eZwxYEUV8H2Jg0ECQEEkOqRSm2pXKwX/WSjNtQPCNxhy6NUeV6vDUmTxIjh3XYjP/ynZeVEbnoj1BjB0N2/U11Jj6nPpZqb7gyppMEkCQQCoGdVYDipU+hMMnvxa0zOIyQc/a+HE0lESqn+2ZPafYi9Z1RldRMvUXhP8U7s+OuhRwprdw2ivvOFrnWyz9lL2";
    byte[] data = Base64.getDecoder().decode(privKeyStr);

    /* Add PKCS#8 formatting */
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(0));
    ASN1EncodableVector v2 = new ASN1EncodableVector();
    v2.add(new ASN1ObjectIdentifier(PKCSObjectIdentifiers.rsaEncryption.getId()));
    v2.add(DERNull.INSTANCE);
    v.add(new DERSequence(v2));
    v.add(new DEROctetString(data));
    ASN1Sequence seq = new DERSequence(v);
    byte[] privKey = seq.getEncoded("DER");

    PKCS8EncodedKeySpec spec = new  PKCS8EncodedKeySpec(privKey);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PrivateKey key = fact.generatePrivate(spec);
    Assert.assertNotNull("Failed to generate the private key", key);
}

这篇关于InvalidKeySpecException:algid 解析错误,不是序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:AES 加密 IV 下一篇:在 JAVA 中使用 PKCS #7 对数据进行签名

相关文章

最新文章