2017-01-29 5 views
0

暗号化されたStringとその解読キーのマップを含む.serファイルを作成しています(これは最善の方法ではありませんが、プロジェクト)私は次にシリアル化を使用して暗号化します:暗号化された.serファイルを復号化してStreamCorruptedExceptionをスローする

private void encryptKeysFile() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, IOException{ 
    SecretKey key64 = new SecretKeySpec(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, "Blowfish"); 
    Cipher cipher = Cipher.getInstance("Blowfish"); 
    cipher.init(Cipher.ENCRYPT_MODE, key64); 
    File keysFile = new File(System.getProperty("src"),fileName); 
    SealedObject sealedObject = new SealedObject(keysFile, cipher); 
    CipherOutputStream cipherOutputStream = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)), cipher); 
    ObjectOutputStream outputStream = new ObjectOutputStream(cipherOutputStream); 
    outputStream.writeObject(sealedObject); 
    outputStream.close(); 
} 

オブジェクトは、ドライブ上のファイルに書き戻されます。 I読者のファイルとそれを復号化する別の方法:これらのメソッドがエラーを実行している

private File dencryptKeysFile() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, IOException, ClassNotFoundException, BadPaddingException{ 
    SecretKey key64 = new SecretKeySpec(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, "Blowfish"); 
    Cipher cipher = Cipher.getInstance("Blowfish"); 
    cipher.init(Cipher.DECRYPT_MODE, key64); 
    CipherInputStream cipherInputStream = new CipherInputStream(new BufferedInputStream(new FileInputStream(fileName)),cipher); 
    ObjectInputStream inputStream = new ObjectInputStream(cipherInputStream); 
    SealedObject sealedObject = (SealedObject)inputStream.readObject(); 
    inputStream.close(); 

    File keysFile =(File)sealedObject.getObject(cipher); 
    this.keysFile = keysFile; 
    return keysFile; 
} 

ザ・:

java.io.StreamCorruptedException: invalid stream header: E0F0DDB8 
at java.io.ObjectInputStream.readStreamHeader(Unknown Source) 
at java.io.ObjectInputStream.<init>(Unknown Source) 
at mainClasses.Encrypter.dencryptKeysFile(Encrypter.java:180) 
at mainClasses.Encrypter.main(Encrypter.java:214) 

をファイルにスローされますが読み込まれていません。 Encrypter.java:180 = ObjectInputStream inputStream = new ObjectInputStream(cipherInputStream);

+0

@SteelToeいいえ、そうではありません。これは、異なるストリームヘッダー値と全く異なるコードを参照します。この質問とは関係ありません。 – EJP

答えて

1

あなたは、密閉した後、暗号化後のシール状態でCipherで起こったことと同じCipher、で暗号化し、その後、あなたが解読し、別のCipherで開封されているが、この時間はもちろんCipherは初期状態にあり、封止後の状態ではなく、封止後にしか達成できないが、これは決して起こり得ない。

ここではベルトとブレースを使用しています。シーリングと暗号ストリームの両方の暗号化は必要ありません。どちらか一方を使用してください。

+0

封印したファイルを開くことはできますか? – user2370794

+0

「封印されたファイル」という意味はわかりませんが、どのファイルも開くことができます。密封された*オブジェクト*は暗号化されています。 – EJP

+0

したがって、密封されたオブジェクトの作成後に暗号化メソッドが終了し、CipherOutputStreamがすでに暗号化されたオブジェクトを暗号化しようとしています。 – user2370794

関連する問題