简体   繁体   English

在读取时修改InputStream读取的字节,而不是更晚

[英]Modify the bytes read by an InputStream while reading, not later

I need to make a simple sort of encryption so that the normal end user can't access some files easily. 我需要进行简单的加密,以便普通最终用户无法轻松访问某些文件。

The files that are read by an FileInputStream are html files, pngs, jpegs and different simple text files (javascript, xml, ...) FileInputStream读取的文件是html文件,pngs,jpegs和不同的简单文本文件(javascript,xml,...)

What I currently do is this: 我目前做的是这样的:

public static byte[] toEncryptedByteArray(InputStream input) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    copy(input, output, true);
    return output.toByteArray();
}

public synchronized static final int copy(final InputStream input, final OutputStream output, final boolean modify) throws IOException {
    if (input == null || output == null) {
        throw new IOException("One stream is null!");
    }
    byte[] mBuffer = new byte[DEFAULT_BUFFER_SIZE];
    int count = 0;
    int n;
    while ((n = input.read(mBuffer)) != -1) {
        if (modify) {
            for ( int i = 0 ; i < n ; i++ ) {
                mBuffer[i] = (byte) ~mBuffer[i]; // byte manipulation
            }
        }
        output.write(mBuffer, 0, n);
        output.flush();
        count += n;
    }
    mBuffer = null;
    return count;
}

The memory footprint is huge as I have the complete byte array in my memory (we talk about bitmaps with >2mb in memory). 内存占用量很大,因为我的内存中有完整的字节数组(我们在内存中讨论位图> 2mb的位图)。

I thought I could simple extend the FileInputStream class and do the byte manipulation while reading the content of the file. 我以为我可以简单地扩展FileInputStream类,并在读取文件内容时进行字节操作。 That would reduce the memory usage as I could use Bitmap.decodeStream(inputstream) instead of creating a byte array to get the bitmap from... but here I am stuck totally. 这将减少内存使用量,因为我可以使用Bitmap.decodeStream(inputstream)而不是创建一个字节数组来获取位图......但在这里我完全陷入困境。 The read() and the readBytes(...) methods are native, so I can't override them. read()readBytes(...)方法是原生的,所以我不能覆盖它们。

Please spread the light in my darkness... 请在我的黑暗中传播光明......

Streams are designed to wrap. Streams旨在包装。 That's why you frequently see lines like: 这就是为什么你经常看到如下行:

InputStream is=new BufferedInputStream(new FileInputStream(file));

So, create a DecryptingInputStream that does your decryption, wrapping around another InputStream . 因此,创建一个DecryptingInputStream ,包裹另一个InputStream

All that being said, this won't be tough to crack, as your decryption keys and algorithm will be easily determinable by anyone who decompiles your app. 总而言之,这不会很难破解,因为您的解密密钥和算法很容易被反编译您的应用程序的任何人确定。

FileInputStream is not your problem. FileInputStream不是你的问题。 The read() function will not read in more than DEFAULT_BUFFER_SIZE on each call. 每次调用时,read()函数的读取次数不会超过DEFAULT_BUFFER_SIZE。 It is your use of ByteArrayOutputStream which is using up a lot of memory as your are writing to it. 你正在使用ByteArrayOutputStream,它正在用尽大量内存。

If you want to reduce the amount of memory required then I suggest you write directly to a FileOutputStream instead of a ByteArrayOutputStream. 如果您想减少所需的内存量,我建议您直接写入FileOutputStream而不是ByteArrayOutputStream。 So encrypt and write to disk as you read DEFAULT_BUFFER_SIZE from the InputStream. 因此,当您从InputStream读取DEFAULT_BUFFER_SIZE时,加密并写入磁盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM