缓存KeyStore的内容并将其转换为InputStream

发布于 2024-09-14 16:43:23 字数 455 浏览 3 评论 0原文

我正在使用 AES 来完成对称密钥加密。 我将密钥存储在受密码保护的密钥库中。

API 公开了以下内容,用于从密钥库加载密钥

keyStore.load(inputStream, keyStorePassword.toCharArray()); 

,因此每次当我想要加密或解密时,我都必须传递输入流,在我看来,这至少会影响性能,因为它每次都要重新读取内容。 /strong>

有人可以帮我制定将其存储在内存中并从那时起访问它并转换为 InputStream 的策略吗?

注意: 我确实尝试将密钥库的内容读取为字符串(UTF-8)并将其转换为InputStream并将其传递给api。但它吐出了以下异常

java.io.IOException:无效 密钥库表单

I am using AES to accomplish symmetric-key encryption.
I store the Key in a password protected KeyStore.

the api exposes the following for loading the key from keystore

keyStore.load(inputStream, keyStorePassword.toCharArray()); 

so everytime when i want to enrypt or decrypt , i have to pass the inputstream which is atleast in my opinion a performance hit as it has to read the content everytime afresh.

Could you anyone please help me out with the strategy of storing it in memory and from then on accessing it and converting to a InputStream?

Note :
i did try to read the contents of the keystore to String (UTF-8)and convert it to InputStream and passed it to the api .But it spit out following exception

java.io.IOException: Invalid
keystore form

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

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

发布评论

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

评论(2

蓝梦月影 2024-09-21 16:43:23

KeyStore 采用某种二进制格式。将其转换为 UTF-8 字符串是不行的。您可以使用使用字节缓冲区的 ByteArrayInputStream 。

但是在我看来在性能优化方面并不重要。您应该进行一些分析来检查这是否真的会影响性能。因为不应该。操作系统也会缓存,如果同时没有更改,很可能不会一遍又一遍地从磁盘读取同一文件。程序员通常非常不善于判断程序的哪些部分是性能消耗者,哪些部分不是。

另外:密码通常通过 char 数组提供是有原因的:您可以完全控制数组的内容,并且可以在不再需要时将其清除。密码在内存中应尽可能短。您无法对简单字符串进行这种控制(您不知道它们何时被垃圾收集)。

The KeyStore is in some binary format. Converting it to a UTF-8 string is no good. You could use a ByteArrayInputStream which uses a byte buffer.

But: in my opinion doesn't count when it comes to performance optimization. You should do some profiling to check whether this really impacts performance. Because it shouldn't. The operating system does cache too and most probably won't read the same file from disk over and over again if it didn't change in the meantime. Programmers usually are extremely bad at judging which parts of a program are performance hogs and which aren't.

Also: It has a reason that passwords usually are provided via char arrays: You have total control over the content of the array and can clear it once it isn't needed anymore. The password should stay in memory as short as possible. You don't have that kind of control with simple strings (you don't know when they are garbage collected).

梦罢 2024-09-21 16:43:23

感谢您的回复。
我完全赞成以下替代方案。

我将加载密钥库一次并提取 SecretKey 并分配给您正在使用的类的实例或类变量,然后在需要加密或解密时使用 SecretKey

Thanks for the responses.
I am all for the below alternative.

I would Load the keystore once and extract the SecretKey and assign to an instance or class variable of the class you are using and then use the SecretKey whenever one need to encrypt or decrypt

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