保护文件中的密钥

发布于 2025-01-03 05:45:14 字数 328 浏览 4 评论 0原文

我从以下代码生成私钥:

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA","BC");
    kpg.initialize(512);
    KeyPair          pair = kpg.genKeyPair();

我想将私钥存储在文件中,并且我想为文件设置密码来保护它。是否可以在JAVA中以编程方式?

我见过 openssl 命令生成密钥并对其进行加密,放入一个文件并保留该文件的密码。但我不知道该文件存储在哪里? 无论如何,我想通过java程序输入密码......

I generated private key from the following code:

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA","BC");
    kpg.initialize(512);
    KeyPair          pair = kpg.genKeyPair();

I want to store private key in file and i want to put password for the file to protect it.Is it possible programatically in JAVA??

I have seen openssl commands where it generates key and encrypts it,puts in a file and keeps password for that file.But i have no idea where the file is storing??
Anyway i want to put password by a javaprogram...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿挽旧人 2025-01-10 05:45:14

执行此操作的标准方法是将您的密钥存储在密钥库中。密钥库有多种不同的格式,Java 往往默认使用 JKS(Java 密钥库)。这些文件可以保存证书和私钥,并且通常受密码保护,用于派生密钥来加密内容。

如何以编程方式执行此操作在 Keystore 类的 javadoc 中有详细记录,您还可以在其中找到可能需要使用的相关类的链接。这是链接:http://docs.oracle。 com/javase/6/docs/api/java/security/KeyStore.html

The standard way of doing this is to store your key within a keystore. There are a number of different formats of keystores, and Java tends to default to JKS (Java Key Store). These files can hold certificates and private keys, and are typically protected with a password, used to derive a key to encrypt the contents.

How to do this programmaticlly is documetned well in the javadoc for the Keystore class, where you can also find links to related classes you may need to use. Here is the link: http://docs.oracle.com/javase/6/docs/api/java/security/KeyStore.html

白馒头 2025-01-10 05:45:14

有很多潜在的解决方案。但是,重新使用现有标准会为您提供最好的服务。

例如,您应该能够将密钥对保存在受密码保护的 PKCS#12 文件中 - 即使不包含 X.509 证书。这将保护您的密钥对,并且如果您需要在另一个系统或其他语言上重用它,它将更加经得起未来的考验。

There are a lot of potential solutions. However you'll best served by re-using an existing standard.

E.g. you should be able to save your key-pair inside a password-protected PKCS#12 file - even if if does not include X.509 certificate(s). That will protect your key-pair and will be more future-proof if you ever need to reuse it on another system or from another language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文