java - 从 DER 格式的 String base64 编码创建 PrivateKey 和 PublicKey

标签 java encryption private-key der

我的私钥和公钥在 base64 的字符串中,使用 ANS1 DER 编码。我尝试创建 java PrivateKeyPublicKey 的实例:

byte [] llave2 = DatatypeConverter.parseBase64Binary(key);
PKCS8Key pkcs8 = new PKCS8Key( llave2, password.toCharArray()); //line 2
llave2 = pkcs8.getDecryptedBytes();                             //line 3
certificado = DatatypeConverter.parseBase64Binary(cer);

KeyFactory kf = KeyFactory.getInstance("RSA");  
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(llave2);
PrivateKey privateKey = kf.generatePrivate(ks);
X509EncodedKeySpec x = new X509EncodedKeySpec(certificado);
PublicKey publicKey = kf.generatePublic(x);

我在 PublicKey publicKey = kf.generatePublic(x) 中收到以下错误。

    java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException:     IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)
    at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(Unknown Source)
    at java.security.KeyFactory.generatePublic(Unknown Source)
    at vital.cancelaciones.GeneraXMLCancelacion.main(GeneraXMLCancelacion.java:118)
Caused by: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)
    at sun.security.x509.X509Key.decode(Unknown Source)
    at sun.security.x509.X509Key.decode(Unknown Source)
    at sun.security.rsa.RSAPublicKeyImpl.<init>(Unknown Source)
    at sun.security.rsa.RSAKeyFactory.generatePublic(Unknown Source)
    ... 3 more

我想我应该像第 2 行和第 3 行中的私钥一样对公钥做一些类似的事情。因为证书也被加密了。有什么建议吗?

最佳答案

为了测试您的方案,我使用 openssl 创建了一个 RSA 私钥。

openssl genrsa -out private.pem 1024

然后我将此 key 转换为 PKCS#8 DER 格式。

openssl pkcs8 -topk8 -inform PEM -in private.pem -outform DER -out private.der -nocrypt

openssl 的手册将 PKCS#8 和 DER 都称为格式,所以就我而言,发生了以下情况:

  • pkcs8 告诉 openssl 我想使用 PKCS#8 格式的私钥。
  • -topk8 告诉它我要用 -in 指定的私钥在 PKCS#8 中(否则它会假设它是)。
  • -inform-in 指定我想将 (PEM) 私钥转换为 PKCS#8(没有 -topk8 它'将尝试将 PKCS#8 格式的 key 转换为标准 key 格式。
  • -outform-out 告诉它我想要一个 DER 格式的 key 作为输出。
  • -nocrypt 告诉它我不想加密 key 。

然后,使用我的 RSA key (标准格式)我创建了一个证书。

openssl req -new -x509 -keyform PEM -key private.pem -outform DER -out public.der

证书包含与我的私钥对应的公钥。

完成所有这些之后,我已经使用 Base64 对私钥和证书进行了编码。

base64 private.der > private.der.b64
base64 public.der > public.der.b64

生成了以下文件。

private.pem      # standard
private.der      # pkcs8/DER
private.der.b64 
public.der       # x509/DER
public.der.b64   
public static void main(String[] args) throws IOException, GeneralSecurityException {
  // get a handle on the base64 encoded key and certificate
  File privateKeyFile = new File("private.der.b64");
  File publicKeyFile = new File("public.der.b64");

  // pull them into arrays
  byte[] privateKeyBytes = toByteArray(privateKeyFile);
  byte[] publicKeyBytes = toByteArray(publicKeyFile);

  // decode them
  privateKeyBytes = toDecodedBase64ByteArray(privateKeyBytes);
  publicKeyBytes = toDecodedBase64ByteArray(publicKeyBytes);

  // get the private key
  KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  KeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
  PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

  // get the public key
  CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
  Certificate certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(publicKeyBytes));
  PublicKey publicKey = certificate.getPublicKey();
}

private static byte[] toByteArray(File file) throws IOException {
  // java 7's try-with-resources statement
  try (FileInputStream in = new FileInputStream(file);
      FileChannel channel = in.getChannel()) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    channel.transferTo(0, channel.size(), Channels.newChannel(out));
    return out.toByteArray();
  }
}

private static byte[] toDecodedBase64ByteArray(byte[] base64EncodedByteArray) {
  return DatatypeConverter.parseBase64Binary(
      new String(base64EncodedByteArray, Charset.forName("UTF-8")));
}

主要问题是您有证书而不是公钥。证书包含公钥,但不能用 X509EncodedKeySpec(...) 加载,这就是必须使用 CertificateFactory 的原因。

(顺便说一句 here 是一篇关于 openssl 和 Java 密码学用法的很棒的文章/教程。我的部分信息来自那里。)

关于java - 从 DER 格式的 String base64 编码创建 PrivateKey 和 PublicKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8454677/

相关文章:

bash - 使用 ssh 手动加载私钥

linux - ssh 使用私钥无需密码

java - 警告框不重复,输入检查不工作

java - If-else 语句中的 While 循环 (Java)

java - 如何在 Spring Boot 中实现刷新 token

iphone - 如何解密加密的 Apple iTunes iPhone 备份?

java - 在 Swing 中使用 JProgressBar 进行操作

android - iPhone 和 Android 的通用加密过程

c - 在微 Controller 上用 C 语言加密/解密小消息

ssl - 如何识别我的 SSL 公钥证书?