【问题标题】:Importing PEM encrypted key pair in java using bouncycastle使用 bouncycastle 在 java 中导入 PEM 加密密钥对
【发布时间】:2013-03-16 19:26:16
【问题描述】:

我正在编写一个使用 RSA 执行各种任务的程序。
我知道如何生成密钥对并将其写入文件,但我无法将加密的 (AES-256-CFB) 密钥对加载到 KeyPair 对象。

所以问题是:如何使用 BouncyCastle 库将加密的 PEM 密钥对加载/解密为 java.security.KeyPair 对象?

谢谢。

生成/导出代码:

public void generateKeyPair(int keysize, File publicKeyFile, File privateKeyFile, String passphrase) throws FileNotFoundException, IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    SecureRandom random = new SecureRandom();
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");

    generator.initialize(keysize, random);

    KeyPair pair = generator.generateKeyPair();
    Key pubKey = pair.getPublic();

    PEMWriter pubWriter = new PEMWriter(new FileWriter(publicKeyFile));
    pubWriter.writeObject(pubKey);
    pubWriter.close();
    PEMWriter privWriter = new PEMWriter(new FileWriter(privateKeyFile));
    if (passphrase == null) {
        privWriter.writeObject(pair);
    } else {
        PEMEncryptor penc = (new JcePEMEncryptorBuilder("AES-256-CFB"))
                .build(passphrase.toCharArray());

        privWriter.writeObject(pair, penc);
    }
    privWriter.close();
}

【问题讨论】:

  • 为什么你有用于 RSA 任务的 AES 密钥?
  • @MarquisofLorne 他想加密一个 RSA 私钥以将其存储在一个文件中。

标签: java import rsa bouncycastle pem


【解决方案1】:

我假设您已将 BouncyCastle 设置为安全提供程序,例如:

Security.addProvider(new BouncyCastleProvider());

您提供的代码创建了两个密钥文件,一个用于私钥,一个用于公钥。但是,公钥隐含在私钥中,因此我们只需要读取私钥文件即可重构密钥对。

那么主要的步骤是:

  • 创建一个PEMParser 来读取密钥文件。

  • 使用解密密钥所需的密码创建一个JcePEMDecryptorProvider

  • 创建JcaPEMKeyConverter,将解密后的密钥转换为KeyPair

KeyPair loadEncryptedKeyPair(File privateKeyFile, String passphrase)
      throws FileNotFoundException, IOException {
  FileReader reader = new FileReader(privateKeyFile);
  PEMParser parser = new PEMParser(reader);
  Object o = parser.readObject();

  if (o == null) {
    throw new IllegalArgumentException(
        "Failed to read PEM object from file!");
  }

  JcaPEMKeyConverter converter = new JcaPEMKeyConverter();

  if (o instanceof PEMKeyPair) {
    PEMKeyPair keyPair = (PEMKeyPair)o;
    return converter.getKeyPair(keyPair);
  }

  if (o instanceof PEMEncryptedKeyPair) {
    PEMEncryptedKeyPair encryptedKeyPair = (PEMEncryptedKeyPair)o;

    PEMDecryptorProvider decryptor =
        new JcePEMDecryptorProviderBuilder().build(passphrase.toCharArray());

    return converter.getKeyPair(encryptedKeyPair.decryptKeyPair(decryptor));
  }

  throw new IllegalArgumentException("Invalid object type: " + o.getClass());
}

示例用法:

File privKeyFile = new File("priv.pem");
String passphrase = "abc";

try {
  KeyPair keyPair = loadEncryptedKeyPair(privKeyFile, passphrase);
} catch (IOException ex) {
  System.err.println(ex);
}

参考:用于键解析的 BouncyCastle 单元测试 (link)。

【讨论】:

    猜你喜欢
    • 2013-03-17
    • 2011-01-09
    • 2017-03-17
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多