如何在 Java 中生成一次密钥并在 2 个不同的程序中使用该密钥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20233775/
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
How to generate secret key in Java once and use that key in 2 different programs
提问by K M Rakibul Islam
My aim is to write a Java program to encrypt a text file (cipher text
) using AES algorithm
. And then, write another program to decrypt that encrypted file (cipher text
) to get the plain text back. I want to use same key (same key, generate once, save it somewhere, and use it in both encryption and decryption program) for encryption and decryption process. If I generate key and do the encryption and decryption line by line in the same program then it works perfectly. Here is the working code snippet for that:
我的目标是编写一个 Java 程序来cipher text
使用AES algorithm
. 然后,编写另一个程序来解密该加密文件 ( cipher text
) 以获取纯文本。我想使用相同的密钥(相同的密钥,生成一次,将其保存在某处,并在加密和解密程序中使用它)进行加密和解密过程。如果我生成密钥并在同一个程序中逐行进行加密和解密,那么它就可以完美运行。这是工作的代码片段:
String strDataToEncrypt = new String();
String strCipherText = new String();
String strDecryptedText = new String();
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE,secretKey);
strDataToEncrypt = "any text input";
byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
strCipherText = new BASE64Encoder().encode(byteCipherText);
System.out.println("cipher text: " +strCipherText);
aesCipher.init(Cipher.DECRYPT_MODE,secretKey,aesCipher.getParameters());
byte[] byteDecryptedText = aesCipher.doFinal(new BASE64Decoder().decodeBuffer(strCipherText));
strDecryptedText = new String(byteDecryptedText);
System.out.println("plain text again: " +strDecryptedText);
But, I need to have two different programs (java files) for encryption and decryption. So, I have to somehow generate a key and save that somewhere. Then use the same key for both encryption and decryption program. How can I do that?
但是,我需要有两个不同的程序(java 文件)来进行加密和解密。所以,我必须以某种方式生成一个密钥并将其保存在某个地方。然后对加密和解密程序使用相同的密钥。我怎样才能做到这一点?
EDIT_1
编辑_1
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
byte[] encoded = secretKey.getEncoded();
System.out.println("key: "+encoded);// key: [B@52b2a2d8
I can get the encoded key value using the above program. But my question is how to generate the SecretKey using this value in my decryption program?
我可以使用上述程序获取编码的键值。但我的问题是如何在我的解密程序中使用这个值生成 SecretKey?
采纳答案by initramfs
Forgive me if I misunderstood your question but I believe you wish to reconstruct a SecretKey
object from a existing key encoded in a byte array.
如果我误解了您的问题,请原谅我,但我相信您希望SecretKey
从以字节数组编码的现有密钥重建对象。
This can be performed simply by using the javax.crypto.spec.SecretKeySpec
's constructor as such:
这可以简单地通过使用javax.crypto.spec.SecretKeySpec
的构造函数来执行:
byte[] encoded = //Key data
SecretKey secretKey = new SecretKeySpec(encoded, "AES");
Since SecretKeySpec
is a subclass of SecretKey
no casting is needed. Should your encryption/decrption algorithm change please make sure to change the string literal used in the constructor AES
to whatever algorithm you decided to use in the future.
因为SecretKeySpec
是SecretKey
不需要铸造的子类。如果您的加密/解密算法发生变化,请确保将构造函数中使用的字符串文字更改为AES
您将来决定使用的任何算法。
回答by Jim Garrison
Here's oneway to print out the values in a byte[]
array in hex:
这是以十六进制打印出数组中的值的一种方法byte[]
:
byte[] a = {-120, 17, 42,121};
for (byte b : a)
{
System.out.printf("%2X",b);
}
System.out.println();