5

パスワードを暗号化および復号化しようとしていますが、これらの生成キーについてはこれまでのところ問題ありません。このキーをプロパティファイルに保存する必要がありますが、キーを追加すると次のようになります。

#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();
    }

}

助けてくれてありがとう.すべての答えは受け入れられる.

4

1 に答える 1

3

KeyGenerator#generateKey()SecretKeyjavadocsのおよびからの戻り値の型があります

このインターフェースを実装するキーは、文字列 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");

于 2016-11-01T07:02:55.500 に答える