1. Download our Official Android App: Forums for Android!

Xor file encrypt/decrypt

Discussion in 'Android Development' started by ArthurGray, Sep 6, 2017.

  1. ArthurGray

    ArthurGray Newbie
    Thread Starter
    Rank:
     #820
    Points:
    15
    Posts:
    13
    Joined:
    Aug 31, 2017

    Aug 31, 2017
    13
    0
    15
    Hello guys, I'm back with a simple question.
    I could use some help understanding where's the problem with this piece of code.
    Basically it's a method that uses a byte[] password to cycle through a file and xor encrypt it, in a (useless) effort to write bug-free code, I wrote the easiest implementation possible: read a byte, encrypt it, write the result, repeat until no byte is available.
    Code (Text):
    1. private void xorFile(String filename, String dest)
    2. {
    3.     try {
    4.         FileInputStream is = new FileInputStream(filename);
    5.         FileOutputStream os = new FileOutputStream(dest);
    6.         int b = is.read();
    7.         int index = 0;
    8.         while(b != -1)
    9.         {
    10.             int c = ((byte)b ^ password[index % password.length]);
    11.             index++;
    12.             os.write(c);
    13.             b = is.read();
    14.         }
    15.         os.flush();
    16.         os.close();
    17.         is.close();
    18.     }
    19.     catch( Exception e )
    20.     {}
    21. }
    22.  
    I don't really understand, the output file ends up being even larger than the input!
    I checked both files with a binary viewer, the output file is perfectly fine (xor is working) except for the additional bytes, which consist in a large amount of zeros plus some random bytes at the end.. I don't get where do they come from!
     

    Advertisement

    #1 ArthurGray, Sep 6, 2017
    Last edited: Sep 6, 2017
  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,843
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,843
    11,441
    1,988
    Male
    Software developer
    South West of England
    Well this line looks dodgy:

    Code (Text):
    1.  
    2. int c = ((byte)b ^ password[index % password.length]);
    3.  
    You declare variable 'c' to be of type int. In Java, an int is 32 bits (4 bytes). This means that the result of your expression, which yields only 1 byte of data, will be padded out to 4 bytes, with 3 bytes of zeroes.

    Declare variable c as a byte, and you'll get the result you expect.

    In fact you don't even need variable 'c', because you could write

    Code (Text):
    1.  
    2. os.write((byte)b ^ password[index % password.length]);
    3.  
     
  3. ArthurGray

    ArthurGray Newbie
    Thread Starter
    Rank:
     #820
    Points:
    15
    Posts:
    13
    Joined:
    Aug 31, 2017

    Aug 31, 2017
    13
    0
    15
    From the documentation:
    also is.read returns an integer. The reason is that it needs more than 8 bit to represent the case in which no byte has been read, -1.
    I tried it anyway with same result.
    I'll add some details: I encrypt a 72KB file obtaining a 76KB where the first 72 are exactly what I would expect, the last 4 are just a pure manifestation of the demon itself.
    I also tried to actually catch exceptions but nothing. No exceptions or error logs in logcat.
     
    #3 ArthurGray, Sep 7, 2017
    Last edited: Sep 7, 2017
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,843
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,843
    11,441
    1,988
    Male
    Software developer
    South West of England
    What happens if you take out the encryption line. Simply read and write the same file contents. Do you get the right result?
     
  5. ArthurGray

    ArthurGray Newbie
    Thread Starter
    Rank:
     #820
    Points:
    15
    Posts:
    13
    Joined:
    Aug 31, 2017

    Aug 31, 2017
    13
    0
    15
    I am very sorry, I wasn't aware a subsequent method would actually edit my file appending some data.. this is what you get for programming late at night.
    Thank you!
    I upgraded the script, this should be faster. I'll leave it here since I didn't find a xor file encryptor before writing this, so that perhaps this topic will be of use to someone else (you might consider turning index to long if dealing with large files)
    Code (Java):
    1. void xorFile(String filename, String dest, byte[] password) throws IOException
    2. {
    3.         FileInputStream is = new FileInputStream(filename);
    4.         FileOutputStream os = new FileOutputStream(dest);
    5.  
    6.         byte[] data = new byte[1024*4]; //4 KB buffer
    7.         int read = is.read(data), index = 0;
    8.         while( read != -1 ) {
    9.             for( int k=0; k<read; k++ ) {
    10.                 data[k] ^= password[index % password.length];
    11.                 index++;
    12.             }
    13.             os.write(data,0,read);
    14.             read = is.read(data);
    15.         }
    16.  
    17.         os.flush();
    18.         os.close();
    19.         is.close();
    20. }
     

Share This Page

Loading...