java 读取和写入 SecretKey 到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10536930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading and Writing a SecretKey to a file
提问by RyanSoper
I'm looking to be able to save a DES SecretKey to a file, so you can import and use it later on. I've looked at a few routes, but they all seem a bit long winded, is there no conscise simple way of doing this?
我希望能够将 DES SecretKey 保存到文件中,以便您以后可以导入和使用它。我看了几条路线,但都显得有点啰嗦,有没有简洁的方法可以做到这一点?
采纳答案by Sanjeev
Not clear on a few things: what you consider "long winded", how you're generating the key, and what format you have the key in. If you don't have the key you can use keytool (a utility which comes with jdk) to generate it and put it into a file called a keystore. You can also use the KeyGenerator class and the KeyStore class to programmatically do the same thing (both are documented pretty well and the KeyStore javadochas an example which does exactly what you seem to want.)
不清楚一些事情:您认为“冗长”是什么,您如何生成密钥,以及您拥有密钥的格式。如果您没有密钥,您可以使用 keytool(随附的实用程序) jdk) 生成它并将其放入一个名为 keystore 的文件中。您还可以使用 KeyGenerator 类和 KeyStore 类以编程方式执行相同的操作(两者都有很好的文档记录,并且KeyStore javadoc有一个示例,它完全符合您的要求。)
If this is long winded, you can generate the key and put it into a text file as clear text. Then you can just access it using a BufferedReader. This is less secure because anyone with access to this file will know your key, but it's a little easier.
如果这是冗长的,您可以生成密钥并将其作为明文放入文本文件中。然后您可以使用 BufferedReader 访问它。这不太安全,因为任何有权访问此文件的人都会知道您的密钥,但它更容易一些。
回答by Bruno
The most "natural" way to save a SecretKey
into a file in Java is to use a KeyStore
: that's exactly what it's for (although you can save certificates too, of course).
SecretKey
在 Java 中将 a保存到文件中的最“自然”方法是使用aKeyStore
: 这正是它的用途(当然,您也可以保存证书)。
The example in the documentationshows how to save a private key and a secret key. It will also encrypt it with a protection parameter (password here).
回答by Mike Samuel
javax.crypto.SecretKey extends java.security.Key
javax.crypto.SecretKey extends java.security.Key
java.security.Key extends java.io.Serializable
java.security.Key extends java.io.Serializable
So if you trust the file-system to which you're storing keys, you can write out a key using java.io.ObjectOutputStream
and read it in using java.io.ObjectInputStream
.
因此,如果您信任存储密钥的文件系统,则可以使用 using 写出密钥java.io.ObjectOutputStream
并在其中读取它java.io.ObjectInputStream
。
ObjectOutputStream oout = new ObjectOutputStream(outputStream);
try {
oout.writeObject(myKey);
} finally {
oout.close();
}
and the input looks similar
并且输入看起来相似
Key key;
ObjectInputStream oin = new ObjectInputStream(inputStream);
try {
key = (Key) oin.readObject();
} finally {
oin.close();
}
To generate a key, you need a KeyFactory
or SecretKeyFactory
要生成密钥,您需要一个KeyFactory
或SecretKeyFactory
SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
and then to generate a secret
然后生成一个秘密
factory.generateSecret(keySpec);
given a key spec that is appropriate to the algorithm. See the KeySpec
javadocfor pointers to algorithm specific KeySpec
implementations.
给定适合算法的关键规范。有关算法特定实现的指针,请参阅KeySpec
javadocKeySpec
。
For DES, see DESKeySpec
though you might want something more modern.
对于 DES,看看DESKeySpec
你可能想要更现代的东西。