java 密钥保存在哪里,哪个是Java应用程序的密钥库/信任库的默认位置?

I am writing an application that uses SSL.

Hence, I have a dummy keystore and a dummy truststore I want to supply to my customer.

Is there any default folder to put them inside my distribution?

e.g. docs, lib, bin...etc. Where is usually the keystore put on the server and where is usually truststore put on the client?

Thanks

解决方案

In Java, according to the JSSE Reference Guide, there is no default for the keystore, the default for the truststore is "jssecacerts, if it exists. Otherwise, cacerts".

A few applications use ~/.keystore as a default keystore, but this is not without problems (mainly because you might not want all the application run by the user to use that trust store).

I'd suggest using application-specific values that you bundle with your application instead, it would tend to be more applicable in general.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中生成 PKCS1 格式的 RSA 密钥对可以使用 Java Cryptography Extension (JCE) 中的 `KeyPairGenerator` 类。这里是一个示例代码: ``` import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; public class GenerateRSAKeyPair { public static void main(String[] args) { try { // 获取 KeyPairGenerator 对象 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); // 初始化 KeyPairGenerator 对象,设置密钥长度 keyPairGenerator.initialize(2048); // 生成密钥对 KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 获取公钥和私钥 PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们使用 `KeyPairGenerator.getInstance("RSA")` 方法获取了一个 `KeyPairGenerator` 对象,然后调用 `initialize` 方法设置密钥长度,最后调用 `generateKeyPair` 方法生成密钥对。生成的密钥对包含一个公钥和一个私钥,分别可以使用 `keyPair.getPublic()` 和 `keyPair.getPrivate()` 方法获取。 请注意,上面的代码中使用的是 PKCS#1 格式的 RSA 密钥,如果你想使用其他格式的密钥,可以更改 `KeyPairGenerator.getInstance` 方法的参数。例如,如果你想生成 PKCS#8 格式的密钥,可以使用 `KeyPairGenerator.getInstance("RSA", "SunRsaSign") ### 回答2: 要使用Java生成PKCS1 RSA密钥对,我们可以使用Java密钥(KeyStore)和Java加密标准(Java Cryptography Architecture,JCA)提供的工具和类来完成。 首先,我们需要使用KeyPairGenerator类生成RSA密钥对。示例代码如下: ```java import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; public class GenerateRSAKeys { public static void main(String[] args) { try { // 选择RSA算法 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); // 设置密钥长度,一般为1024、2048 keyPairGenerator.initialize(2048); // 生成密钥对 KeyPair keyPair = keyPairGenerator.generateKeyPair(); System.out.println("Public Key: " + keyPair.getPublic()); System.out.println("Private Key: " + keyPair.getPrivate()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } } ``` 上述代码中,我们通过调用KeyPairGenerator.getInstance("RSA")选择了RSA算法,然后通过调用keyPairGenerator.initialize(2048)设置密钥长度为2048位,最后通过调用keyPairGenerator.generateKeyPair()生成了RSA密钥对。 运行该代码,我们将在控制台上看到生成的公钥和私钥。请注意,这里输出的密钥格式可能不是PKCS1格式,而是Java中的默认格式。如果需要将密钥以PKCS1格式存储或导出,可以使用相关的编码和转换方法进行处理。 希望以上信息对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值