gpt4 book ai didi

java - 如何将 GeneratedKey 添加到 config.properties 文件?

转载 作者:搜寻专家 更新时间:2023-10-31 20:17:41 24 4
gpt4 key购买 nike

我正在尝试加密和解密密码,到目前为止,这些生成的 key 一直很好。现在我需要将这个 key 存储在属性文件中,但是当我添加 key 时,它看起来像这样:

#Tue Nov 01 08:22:52 EET 2016
KEY=\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000

所以我怀疑我的代码可能有问题?!?!

还有一部分是我的代码=

private byte[] key = new byte[16];

public void addProperties(String x, String z) {
Properties properties = new Properties();
String propertiesFileName = "config.properties";
try {
OutputStream out = new FileOutputStream(propertiesFileName);
properties.setProperty(x, z);
properties.store(out, null);
} catch (IOException e) {
e.printStackTrace();
}
}

public void generateKey() {
KeyGenerator keygen;
SecretKey secretKey;
byte[] keybyte = new byte[64];
try {
keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
secretKey = keygen.generateKey();
keybyte = secretKey.getEncoded();
key = keybyte;

//THIS METHOD ADDING PROP TO PROPERTIES FILE
addProperties("KEY", new String(key));

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

}

感谢您的帮助。所有答案均可接受。

最佳答案

KeyGenerator#generateKey() 的返回类型为 SecretKey来自 javadocs

Keys that implement this interface return the string RAW as their encoding format (see getFormat), and return the raw key bytes as the result of a getEncoded method call. (The getFormat and getEncoded methods are inherited from the java.security.Key parent interface.)

So you need to convert them and there is already asked question on this

String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());

SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");

关于java - 如何将 GeneratedKey 添加到 config.properties 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40355287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com