如何在OS密钥库中存储程序密码? [英] How can I store a program password in the OS keystore?

查看:80
本文介绍了如何在OS密钥库中存储程序密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Mac上开发Java应用程序.我知道有Apple提供的密钥库,我想在该密钥库中安全地存储密码.

I am currently developing a Java application on a Mac. I know that there is the keystore from Apple and I want to securely store a password within that keystore.

根据Apple开发人员的说法,我可以使用keyStore = KeyStore.getInstance("KeychainStore", "Apple");

According to Apple developers I can get the keystore with keyStore = KeyStore.getInstance("KeychainStore", "Apple");

现在我的问题是:如何存储密码以及如何重新找回密码? 我已经阅读了很多有关密钥库的信息,但我不知道实现的外观.

Now my question is: How can I store the password and how can I get the password back again? I have read a lot about keystores but I do not know, how an implementation would look like.

如何从Windows/Linux获取内置密钥库?

And how can I get the built-in keystore from Windows / Linux?

推荐答案

java.security.KeyStore创建的,用于处理密码.它是加密密钥和证书的存储设施.尽管您可以尝试使用它来存储密码(因为它可以存储私钥),但我建议您这样做,因为KeyStore的API繁琐且并非针对您的用例而设计. https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html

java.security.KeyStore was not created to deal with passwords. It is a storage facility for cryptographic keys and certificates. While you can try to use it to store passwords since it can for example store private keys, I would advise you against that, because KeyStore's API is cumbersome and was not designed for your use case. https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html

您需要的是macOS中的钥匙串和其他操作中的类似工具系统.由于您还询问Windows和Linux,因此您可能对 Java Keyring 库感兴趣.它将密码存储在:

What you need instead is Keychain in macOS and similar tools in other operating systems. Since you are also asking about Windows and Linux, you might be interested in the Java Keyring library. It stores passwords in:

    在Mac OS上的
  1. 钥匙串
  2. 在Windows上
  3. 凭据管理器
  4. GNOME上的DBus秘密服务
  1. Keychain on macOS
  2. Credential Manager on Windows
  3. DBus Secret Service on GNOME

这里是使用方法:

public static void main(String[] args) throws Exception {
    Keyring keyring = Keyring.create();
    String serviceName = "test-app";
    String accountName = "test-account";
    keyring.setPassword(serviceName, accountName, "test-password");
    String password = keyring.getPassword(serviceName, accountName);
    System.out.println(password);
}

Gradle

implementation 'com.github.javakeyring:java-keyring:1.0.1'

行家

<dependency>
  <groupId>com.github.javakeyring</groupId>
  <artifactId>java-keyring</artifactId>
  <version>1.0.1</version>
</dependency>

如果要支持GNOME以外的桌面环境,则可能必须提出自己的解决方案或搜索其他库,但这应该可以帮助您入门.

If you want to support desktop environments other than GNOME you would probably have to come up with your own solution or search for a different library, but this should get you started.

这篇关于如何在OS密钥库中存储程序密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆