N'essayez pas de stocker l'objet IvParameterSpec. Ce n'est pas sérialisable, car il n'est pas destiné à être stocké. L'IV est la partie importante. Stockez ceci et créez un nouveau IvSpec à partir du IV. J'ai changé le code d'exemple de here pour le cryptage AES pour stocker le IV et utiliser le IV chargé pour déchiffrer le texte chiffré afin que vous puissiez voir un flux de travail possible.
Veuillez noter qu'il s'agit d'un exemple minimal. Dans un vrai usecase vous stocker et charger la clé aussi bien et la gestion des exceptions devrait également être reconsidéré :-D
public class Test {
public static void main(String[] args) throws Exception {
String message = "This string contains a secret message.";
// generate a key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
byte[] key = keygen.generateKey().getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
byte[] iv = { 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8 };
IvParameterSpec ivspec = new IvParameterSpec(iv);
// initialize the cipher for encrypt mode
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
// encrypt the message
byte[] encrypted = cipher.doFinal(message.getBytes());
System.out.println("Ciphertext: " + hexEncode(encrypted) + "\n");
// Write IV
FileOutputStream fs = new FileOutputStream(new File("paramFile"));
BufferedOutputStream bos = new BufferedOutputStream(fs);
bos.write(iv);
bos.close();
// Read IV
byte[] fileData = new byte[16];
DataInputStream dis = null;
dis = new DataInputStream(new FileInputStream(new File("paramFile")));
dis.readFully(fileData);
if (dis != null) {
dis.close();
}
// reinitialize the cipher for decryption
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(fileData));
// decrypt the message
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println("Plaintext: " + new String(decrypted) + "\n");
}
[...]
}
iv est un tableau d'octets 16bit – user2756703
Je pense que, cette réponse à écrire un tableau d'octets dans un fichier doit aider toi. http://stackoverflow.com/questions/4350084/byte-to-file-in-java – LostAvatar
non, ce n'est pas possible. paramspec1 n'est pas un tableau d'octets – user2756703