Cod*_*JHP 5 java properties-file key-generator
我正在尝试加密和解密密码,并且这些生成密钥到目前为止都很好.现在我需要将此密钥存储在属性文件中,但是当我添加密钥时,它看起来像这样:
#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
Run Code Online (Sandbox Code Playgroud)
所以我怀疑我的代码可能有问题?!?!
并且我的代码中有一部分=
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();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.所有答案都可以接受.
KeyGenerator#generateKey()返回类型为SecretKeyjavadocs 和 from javadocs
实现此接口的密钥返回字符串 RAW 作为其编码格式(请参阅 getFormat),并返回原始密钥字节作为 getEncoded 方法调用的结果。(getFormat 和 getEncoded 方法继承自 java.security.Key 父接口。)
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");
| 归档时间: |
|
| 查看次数: |
660 次 |
| 最近记录: |