Ho fatto programmazione web da diversi anni e da allora non ho fatto nessuna programmazione per applicazioni desktop, e ho dimenticato tante cose. Per favore sii paziente se questo è troppo semplice.Java: come scrivere file binari?
Ora ho questa situazione:
Sto cercando di memorizzare alcune parole con hash in un file. Penso che dovrei usare file binari per questo (correggimi se sbaglio). Ma non ho idea di come dovrei scrivere le parole nel file. Ho provato molti modi, ma quando rileggo il file e provo a decifrare le parole, ottengo BadPaddingException
.
Qualcuno ha qualche idea su come scrivere le parole in un file?
PS: Io uso questo codice per codificare/decodificare le parole (l'ho presa da un altro thread StackOverflow, con alcune modifiche):
public static byte[] encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, 20));
return pbeCipher.doFinal(property.getBytes("UTF-8"));
}
public static String decrypt(byte[] property) throws GeneralSecurityException, IOException {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, 20));
return new String(pbeCipher.doFinal(property));
}
Hai provato a fare 'System.out.println (decriptare (cifrare ("Hello world"))); '? – bezmax
Sì, e funziona! –
Quindi si prega di incollare anche il codice I/O. – bezmax