コードにKeyChain
クラスがあり、これを使用してディスクに保存し、暗号化された資格情報のリストを取得できます。
の構築中にKeyChain
、AES 暗号を初期化します。
オブジェクトをシリアル化するには、まず資格情報リストをバッファにシリアル化し、次にそのバッファを暗号化して元の .xml に入れOutputObjectStream
ます。
それを逆シリアル化するために、バッファに読み込み、復号化し、資格情報を逆シリアル化しようとしObjectInputStream
ましたが、それを行うには、最初に暗号を構築する必要があります。逆シリアル化はコンストラクターを呼び出さないため、それはできません。どうすればこれを好転させることができますか?
キーチェーン:
private void readObject(ObjectInputStream is) throws IOException {
byte[] buffer = new byte[512000];
int readBytes = is.read(buffer);
byte[] encryptedBytes = new byte[readBytes];
System.arraycopy(buffer, 0, encryptedBytes, 0, readBytes);
// Here it crashes and burns because i can't decrypt yet, the ciphers haven't been setup
byte[] decryptedBytes = decryptBytes(encryptedBytes);
ByteInputStream stream = new ByteInputStream(decryptedBytes, readBytes);
ObjectInputStream unsafeInputStream = new ObjectInputStream(stream);
try {
Keys = (List<Key>)unsafeInputStream.readObject();
} catch (ClassNotFoundException ex) {
// Fail miserably
}
}
private void writeObject(ObjectOutputStream os) throws IOException {
ByteOutputStream streamBytes = new ByteOutputStream();
ObjectOutputStream unsafeOutputStream = new ObjectOutputStream(streamBytes);
unsafeOutputStream.writeObject(Keys);
unsafeOutputStream.flush();
byte[] decryptedBytes = streamBytes.getBytes();
byte[] encryptedBytes = encryptBytes(decryptedBytes);
os.write(encryptedBytes);
os.flush();
Arrays.fill(decryptedBytes, (byte)0);
Arrays.fill(encryptedBytes, (byte)0);
}
落とし穴:initCryptograhy(char[] password)
readObjectを呼び出すことはできません。そこにあるパスワードを使用できないためです。パスワードを引数として渡すことはできません。これが問題の根本です。