1

コードに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を呼び出すことはできません。そこにあるパスワードを使用できないためです。パスワードを引数として渡すことはできません。これが問題の根本です。

4

1 に答える 1

2

SealedObjectJava には、シリアル化されたインスタンスを暗号化するために呼び出される機能があります。たぶん、これはあなたが達成しようとしていることに対してよりうまくいくでしょう。あなたがしていることと SealedObject がしていることの主な違いは、最初の逆シリアル化ではなく、2 番目のフェーズで復号化を行うことだと思います。

于 2013-11-06T02:10:56.477 に答える