javaencryptionstream-cipher

Decryption only yields one correct line after encrypting line by line using RC4 algorithm


I have to encrypt a file line by line using the RC4 algorithm.

Encrypting the whole file and decrypting the whole file yields the original which is fine.

When I attempt to read the file one line at a time,encrypt it and then write the encrypted line to file, decryption of the resulting file yields just one correct line which is the first line of the original file.

I have tried to read the file and feed it to rc4 routine using a byte array whose size is a multiple of the key length but the results were the same. Here is my attempt:

try
{
    BufferedReader br = new BufferedReader((new FileReader(fileToEncrypt)));                    
    FileOutputStream fos = new  FileOutputStream("C:\\Users\\nikaselo\\Documents\\Encryption\\encrypted.csv", true);
    File file = new File("C:\\Users\\nikaselo\\Documents\\Encryption\\encrypted.csv");
 // encrypt
    while ((line = br.readLine()) != null) 
    {
        byte [] encrypt = fed.RC4(line.getBytes(), pwd);

        if (encrypt != null) dos.write(encrypt);
            fos.flush();
    }

    fos.close();

// test decrypt
    FileInputStream fis = null;
    fis = new FileInputStream(file);
    byte[] input = new byte[512];
    int bytesRead;
    while ((bytesRead = fis.read(input)) != -1)
    {
        byte [] de= fed.RC4(input, pwd);
        String result = new String(de);
        System.out.println(result);
    }
}   
catch (Exception ex) 
{                                
    ex.printStackTrace();
}

and here is my RC4 function

public  byte []  RC4 (byte [] Str, String Pwd) throws Exception
{
    int[] Sbox = new int [256] ;
    int A, B,c,Tmp;;

    byte [] Key = {};
    byte [] ByteArray = {};

    //KEY
    if ((Pwd.length() == 0 || Str.length == 0)) 
    {
        byte [] arr = {};
        return arr;
    }
    if(Pwd.length() > 256) 
    {
        Key  = Pwd.substring(0, 256).getBytes();
    }
    else 
    {
        Key = Pwd.getBytes();
    }
    //String
    for( A = 0 ; A <= 255; A++ ) 
    {
        Sbox[A] = A;    
    }
    A = B = c= 0;
    for  (A = 0; A <= 255; A++) 
    {
        B = (B + Sbox[A] + Key[A % Pwd.length()]) % 256;    
        Tmp = Sbox[A];
        Sbox[A] = Sbox[B];
        Sbox[B] = Tmp;
    }

    A = B = c= 0;
    ByteArray = Str;
    for (A = 0; A <= Str.length -1 ; A++)
    {   
        B = (B + 1) % 256;
        c = (c + Sbox[B]) % 256;
        Tmp =  Sbox[B];
        Sbox[B] = Sbox[c];
        Sbox[c] = Tmp;
        ByteArray[A] = (byte) (ByteArray[A] ^ (Sbox[(Sbox[B] + Sbox[c]) % 256]));           
    }

    return ByteArray;
}

Running this gives me one clean line and the rest is just unreadable.


Solution

  • You are encrypting line by line, but you are trying to decrypt in 512 bytes blocks.

    Your options, as I see it are:

    1. Encrypt and decrypt in fixed sized blocks
    2. Pad each line out to 512 bytes (and split lines that are longer than 512 bytes)
    3. Introduce a delimiter. This will be tricky because potentially any delimiter could appear in the cipher text, so you should base64 encode each encrypted line and separate them with line feeds.

    Probably 1 is the easiest (and the one used in real encryption), but if you have to do it line by line, I would go with 3 even though this introduces a vulnerability, but it's RC4 which is no longer considered secure anyway.