RC4 decryption not working properly. Encryption is totally working and I have described my problem in the para below

518 Views Asked by At

I am currently working on combining 3 cryptography algorithms namely AES,DES,RC4. I have successfully done encrytion and stored the key and cipher text in a file but as in my code of RC4 cipher is an integer array it needed to be converted in Stringto store it in a file but now when I am trying to decrypt I am not able to get cipher text back to integer array when i am retrieving it from the file I am providing the RC4 encryption and decryption code please help in improving the code.

ENCRYPTION:

     plaintext3=newStr[d];// This is the text that needs to be encrypted
        
        int temp=0;
        String ptext;
        String key2;
        int s[]=new int[256];
        int k[]=new int[256];
        
        ptext=plaintext3;
        int n2=16;  
        String AlphaNumericString2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                    + "0123456789"
                                    + "abcdefghijklmnopqrstuvxyz";
        StringBuilder sb = new StringBuilder(n2); 
        for (int i = 0; i < n2; i++) 
        { 
            int index = (int)(AlphaNumericString2.length()* Math.random()); 
            sb.append(AlphaNumericString2.charAt(index)); 
        }   
        key2= sb.toString(); 


        char ptextc[]=ptext.toCharArray();
        char keyc[]=key2.toCharArray();
        int cipher2[]=new int[ptext.length()];
        

        int ptexti[]=new int[ptext.length()];
        int keyi[]=new int[key2.length()];
        for(int i=0;i<ptext.length();i++)
        {
        ptexti[i]=(int)ptextc[i];
        }
        for(int i=0;i<key2.length();i++)
        {
        keyi[i]=(int)keyc[i];
        }
        for(int i=0;i<255;i++)
        {
        s[i]=i;
        k[i]=keyi[i%key2.length()];
        }
        int j=0;
        for(int i=0;i<255;i++)
        {
        j=(j+s[i]+k[i])%256;
        temp=s[i];
        s[i]=s[j];
        s[j]=temp;
        }
        int i=0;
        j=0;
        int z=0;
        for(int l=0;l<ptext.length();l++)
        {
        i=(l+1)%256;
        j=(j+s[i])%256;
        temp=s[i];
        s[i]=s[j];
        s[j]=temp;
        z=s[(s[i]+s[j])%256];
        cipher2[l]=z^ptexti[l];
        
        }
        System.out.println("cipher is : "+cipher2);
        String s1 = Arrays.toString(cipher2);
        System.out.println(s1);
        
        
        
        
         try { 
                BufferedWriter out = new BufferedWriter(new FileWriter("E:\\CipherFile.txt",true));
                out.write(s1);
                out.close();
                 }finally {
                     
                 }
    
        
        System.out.print("\n\nENCRYPTED:\t\t");
        System.out.println(cipher2);
        
        System.out.print("\n\nKey:\t\t"+key2);

DECRYPTION:

int temp=0;
        
        char[] ch = InputForRC4Decryption.toCharArray();//input for RC4 decryption is the cipher text that is retrieved from file
        
        int s[]=new int[256];
        int k[]=new int[256];
        
         int decrypt2[]=new int[1000000];
         char keyc[]=KeyForRC4Decryption.toCharArray();

    int keyi[]=new int[KeyForRC4Decryption.length()];
    
    
    for(int e=0;e<KeyForRC4Decryption.length();e++)
    {
    keyi[e]=(int)keyc[e];
    }
    
    for(int g=0;g<255;g++)
    {
    s[g]=g;
    k[g]=keyi[g%KeyForRC4Decryption.length()];
    }
    
    int h=0;
    for(int y=0;y<255;y++)
    {
    h=(h+s[y]+k[y])%256;
    temp=s[y];
    s[i]=s[h];
    s[h]=temp;
    }
    
    int m=0;
     h=0;
    int z=0;
    for(int l=0;l<InputForRC4Decryption.length();l++)
    {
    m=(l+1)%256;
    h=(h+s[m])%256;
    temp=s[m];
    s[m]=s[h];
    s[h]=temp;
    z=s[(s[m]+s[h])%256];
    decrypt2[l]=z^ch[l];
    }


    
    
        
        System.out.print("\n\nDECRYPTED:\t\t");
        System.out.println(decrypt2);
        System.out.print("\n\nKey:\t\t"+KeyForRC4Decryption); 

The following code is the code from where i took encryption and decryption processes

    import java.io.*;
import java.util.*;
class Main
{
public static void main(String args[])throws IOException
{
int temp=0;
String ptext;
String key;
int s[]=new int[256];
int k[]=new int[256];
Scanner sc=new Scanner(System.in);
System.out.print("\nENTER PLAIN TEXT\t");
ptext=sc.nextLine();

 
int n=16;  
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                            + "0123456789"
                            + "abcdefghijklmnopqrstuvxyz";
StringBuilder sb = new StringBuilder(n); 
for (int i = 0; i < n; i++) 
{ 
    int index = (int)(AlphaNumericString.length()* Math.random()); 
    sb.append(AlphaNumericString.charAt(index)); 
}   
key= sb.toString(); 


char ptextc[]=ptext.toCharArray();
char keyc[]=key.toCharArray();
int cipher[]=new int[ptext.length()];
int decrypt[]=new int[ptext.length()];

int ptexti[]=new int[ptext.length()];
int keyi[]=new int[key.length()];
for(int i=0;i<ptext.length();i++)
{
ptexti[i]=(int)ptextc[i];
}
for(int i=0;i<key.length();i++)
{
keyi[i]=(int)keyc[i];
}
for(int i=0;i<255;i++)
{
s[i]=i;
k[i]=keyi[i%key.length()];
}
int j=0;
for(int i=0;i<255;i++)
{
j=(j+s[i]+k[i])%256;
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
int i=0;
j=0;
int z=0;
for(int l=0;l<ptext.length();l++)
{
i=(l+1)%256;
j=(j+s[i])%256;
temp=s[i];
s[i]=s[j];
s[j]=temp;
z=s[(s[i]+s[j])%256];
cipher[l]=z^ptexti[l];
decrypt[l]=z^cipher[l];
}
System.out.print("\n\nENCRYPTED:\t\t");
display(cipher);
System.out.print("\n\nDECRYPTED:\t\t");
display(decrypt);
}

static void display(int disp[])
{
char convert[]=new char[disp.length];
for(int l=0;l<disp.length;l++)
{
convert[l]=(char)disp[l];
System.out.print(convert[l]);
}
}
}```
1

There are 1 best solutions below

0
On

Security warning: As the RC4 algorithm is broken it should not be used any longer.

The below code is just a short example of how to do it with Java's own implementation (I'm running Java OpenJDK11 but it should run on Java 8 as well).

Output:

RC4 encryption

### Security warning - the RC4 algorithm is broken - DO NOT USE IT ###

RC4 key length: 16
decrypted: The quick brown fox jumps over the lazy dog

code:

import javax.crypto.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class RC4Main {
    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        System.out.println("RC4 encryption");
        System.out.println("\n### Security warning - the RC4 algorithm is broken - DO NOT USE IT ###\n");
        byte[] plainBytes = "The quick brown fox jumps over the lazy dog".getBytes(StandardCharsets.UTF_8);
        // generate a rc4 key
        KeyGenerator rc4KeyGenerator = KeyGenerator.getInstance("RC4");
        SecretKey key = rc4KeyGenerator.generateKey();
        System.out.println("RC4 key length: " + key.getEncoded().length);
        // encrypt
        Cipher cipherEnc = Cipher.getInstance("RC4");  // Transformation of the algorithm
        cipherEnc.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherBytes = cipherEnc.doFinal(plainBytes);
        // decrypt
        Cipher cipherDec = Cipher.getInstance("RC4");  // Transformation of the algorithm
        cipherDec.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptBytes = cipherDec.doFinal(cipherBytes);
        System.out.println("decrypted: " + new String(decryptBytes));
    }
}