Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将GeneratedKey添加到config.properties文件?_Java_Properties File_Key Generator - Fatal编程技术网

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

Java 如何将GeneratedKey添加到config.properties文件?,java,properties-file,key-generator,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 所以我怀疑我的密码可能出了什么问题 我的代码中有一部分= private byte[] 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();
    }

}
谢谢您的帮助。所有答案都可以接受。
密钥生成器#generateKey()
的返回类型为和来自javadocs

实现此接口的键返回字符串RAW作为其 编码格式(请参见getFormat),并返回原始密钥字节作为 getEncoded方法调用的结果。(getFormat和getEncoded 方法继承自java.security.Key父接口。)

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


SecretKey originalKey=新的SecretKey规范(decodekey,0,decodekey.length,“AES”)

你在使用eclipse吗?是的,eclipse Neon你能试着用这行代码把
字节[]
转换成
字符串吗<代码>添加属性(“键”,新字符串(keybyte,StandardCharsets.UTF_8))感谢您的重播。对于corse,现在我将尝试并通知我们结果:''11月1日星期二08:56:39 EET 2016 KEY=\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000'
。'11月1日星期二09:19:13 EET 2016 KEY=Ja017HB2g8XPrzRwqj4uQw\=