读取Cipher生成的文件时StreamCorruptedExecution

时间:2014-12-11 17:16:08

标签: java android encryption objectinputstream

当我想要读取用CipherOutputStream编写的文件时,我得到一个StreamCorruptedExecution。该文件创建得很好。 这是代码,崩溃发生在最后一行:

            file = new File("/sdcard/test.txt");
            SecretKey key64 = new SecretKeySpec( new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, "Blowfish" );
            Cipher cipher = Cipher.getInstance( "Blowfish" );

            //Code to write your object to file

            cipher.init( Cipher.ENCRYPT_MODE, key64 );

            SealedObject sealedObject = new SealedObject( (Serializable) "TEST", cipher);
            CipherOutputStream cipherOutputStream = new CipherOutputStream( new BufferedOutputStream( new FileOutputStream( file ) ), cipher );
            ObjectOutputStream outputStream = new ObjectOutputStream( cipherOutputStream );
            outputStream.writeObject( sealedObject );
            outputStream.flush();
            outputStream.close();

            //now try to read it again

            CipherInputStream cipherInputStream = new CipherInputStream( new BufferedInputStream( new FileInputStream( file ) ), cipher );

            ObjectInputStream inputStream = new ObjectInputStream( cipherInputStream ); //<== this line crashes with StreamCorruptedExecution

1 个答案:

答案 0 :(得分:1)

您忘记了initDECRYPT_MODE