【问题标题】:How to store a SecretKey within a Java KeyStore protected by a PublicKey (RSAPasswordProtection)?如何将 SecretKey 存储在受 PublicKey (RSAPasswordProtection) 保护的 Java KeyStore 中?
【发布时间】:2015-03-30 18:05:48
【问题描述】:

我想将 SecretKey 存储在受 PublicKey 保护的 Java KeyStore 中。加载受保护的 KeyEntry 时,我想

  1. 获取受保护的密钥字节数组,以便稍后使用 PrivateKey 手动解包。
  2. 在移交 PrivateKey 时让 KeyStore 处理解包。

可以将 setEntry()-Method 与已包装的字节数组一起使用。也可以通过使用 getEntry() 方法来获取包装的字节数组。要加密 SecretKey,setEntry()-Method 支持使用 ProtectionParameter。我能找到的唯一 ProtectionParameter 是 PasswordProtection 参数。

有人知道 RsaProtection for Java KeyStore 吗?还是有其他方法可以使用 PublicKey 包装 SecretKeys 并使用 PrivateKey 取回它?

【问题讨论】:

    标签: java security cryptography


    【解决方案1】:

    Java 密钥库肯定无法处理这个问题;他们主要使用对称加密来保护密钥存储。不过,可以包装和解包密钥。我已经使用 OAEP 而不是不太安全的"RSA" (PKCS#1) 加密来展示这一点:

    Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
    rsa.init(Cipher.WRAP_MODE, keyPair.getPublic());
    byte[] wrapped = rsa.wrap(aesKey);
    
    rsa.init(Cipher.UNWRAP_MODE, keyPair.getPrivate());
    SecretKey unwrappedAESKey = (SecretKey) rsa.unwrap(wrapped, "RSA", Cipher.SECRET_KEY);
    

    【讨论】:

    • 我认为您可能可以做到,但您可能必须编写自己的 SecretKey-implementing 类。但是,密钥库肯定会使用自己的密码重新加密您的密钥。
    • @EJP 可能,但它需要 JCA 周围的一个可怕的垃圾。当然,如果您可以签署自己的提供商,您可以做任何事情;)
    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2011-03-03
    • 2017-03-25
    • 2017-10-15
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多