[英]How to get AES key from java code so that i can save it in a text file?
I have tried below code to get AES key from java code.我尝试使用以下代码从 java 代码中获取 AES 密钥。 And the output is also captured after the code.而output也是在代码之后被抓到的。
import javax.crypto.KeyGenerator;
public class MyClass {
public static void main(String args[]) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256);
System.out.println("key algo: " + keyGenerator.generateKey().getAlgorithm());
System.out.println("key format: " + keyGenerator.generateKey().getFormat());
System.out.println("key generated: " + keyGenerator.generateKey()); // this seems to be equivallent to keyGenerator.generateKey().toString()
System.out.println("key (getEncoded): " + keyGenerator.generateKey().getEncoded());
}
}
Output: I see below output for above code. Output:我看到下面的 output 上面的代码。
key algo: AES
key format: RAW
key generated: javax.crypto.spec.SecretKeySpec@fffe8a11
key (getEncoded): [B@26653222
I am not sure if keyGenerator.generateKey().getEncoded()
result is what i need to use as a key that can be saved in a text file and used between application.我不确定keyGenerator.generateKey().getEncoded()
结果是否是我需要用作可以保存在文本文件中并在应用程序之间使用的密钥。
Can someone Answer.有人可以回答。 What I want is to save the AES key in a text file and send it over through email or some secure means.我想要的是将 AES 密钥保存在文本文件中,并通过 email 或某些安全方式发送。 So the other application can use this key to decrypt the messages.所以其他应用程序可以使用这个密钥来解密消息。
you can encode the bytes to Base64 string like this:您可以将字节编码为 Base64 字符串,如下所示:
SecretKey key = keyGenerator.generateKey();
Base64.getEncoder().encodeToString(key.getEncoded())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.