Split a byte array at a delimiter

前端 未结 6 1997
遥遥无期
遥遥无期 2021-01-02 15:49

I\'m having a bit of an issue and, the other questions here didn\'t help me much.

I am a security student and I am trying to write a crypter for a project. For those

6条回答
  •  迷失自我
    2021-01-02 16:10

    Your approach has a number of flaws - you're reading an entire Byte[] into memory, but decryption is a streamable process, so you're needlessly wasting memory. Secondly you cannot "split" an array (or a string, for that matter) in the CLR. When you split a CLR String it creates copies, which wastes memory.

    Try this:

    public static void Main(String[] args) {
    
        using(FileStream fs = new FileStream( @"path\to\fileName.exe", FileMode.Read)) {
    
            BinaryReader rdr = new BinaryReader( fs );
            SeekToEndOfDelimiter( rdr );
    
            // Use an implementation of RC4 decryption that accepts Streams as arguments, then pass fs directly as an argument:
            using(FileStream output = new FileStream( @"path\to\out.exe", FileMode.Write)) {
                // Providing the key arguments is an exercise for the reader
                MyRc4Implementation.DecryptStream( fs, output, key );
            }
        }
    
    }
    
    private static void SeekToEndOfDelimiter(BinaryReader rdr) {
        // Implementing this code is an exercise left up to the reader.
        // But just iterate through each byte (assuming ASCII-compatible encoding) until you encounter the end of the delimiter
    }
    

    There, no messy byte[] arrays :)

提交回复
热议问题