Search code examples
javaalgorithmencryption

The output gives the same result


Im trying to run this code but it gives the same output: What I’m trying to do is that ask the user to enter text to be encrypted. Then enter the key he wants to use to encrypt either 128, 192, or 256. But when I test it whenever I enter anything it gives the same output :( this is the method i'm trying to call

package test;

public class MARS {
    public static byte[] encrypt(byte[] in,byte[] key){
        K = expandKey(key);
        int lenght=0;
        byte[] padding = new byte[1];
        int i;
        lenght = 16 - in.length % 16;
        padding = new byte[lenght];
        padding[0] = (byte) 0x80;

        for (i = 1; i < lenght; i++)
            padding[i] = 0;

        byte[] tmp = new byte[in.length + lenght];
        byte[] bloc = new byte[16];

        int count = 0;

        for (i = 0; i < in.length + lenght; i++) {
            if (i > 0 && i % 16 == 0) {
                bloc = encryptBloc(bloc);
                System.arraycopy(bloc, 0, tmp, i - 16, bloc.length);
            }
            if (i < in.length)
                bloc[i % 16] = in[i];
            else{
                bloc[i % 16] = padding[count % 16];
                count++;
            }
        }
        if(bloc.length == 16){
            bloc = encryptBloc(bloc);
            System.arraycopy(bloc, 0, tmp, i - 16, bloc.length);
        }

        return tmp;
    }
}

this is the class of calling method

package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Call {
    public static void main(String[] args) throws Exception {
        byte[] array = "going to encrypt".getBytes();
        byte[] key = "the key you want to use".getBytes();
        byte[] encryptBloc = MARS.encrypt(array,key);
        BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
        String plainText = userInput.readLine();
        System.out.println("Enter Text that you want to Encrypt: " + plainText);

        BufferedReader ke = new BufferedReader(new InputStreamReader(System.in));
        String kee = userInput.readLine();
        System.out.println("Enter The Key you want to use to encrypt this message: " + new String(key));
        System.out.println("plain Text: "+ plainText);
        System.out.println("Encrypted Text: " + new String(encryptBloc));
    }
}

Solution

  • Your code just flat out makes very little sense. You're getting the bytes of string literals as the plaintext and the key, and you're (essentially) ignoring the user input. You're also creating multiple input streams that you're not using, and prompting for user input at weird times.

    Here's what it should look like:

    // Create input reader
    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    
    // Prompt, then wait and get user input for the plaintext 
    System.out.println("Enter Text that you want to encrypt:");
    String plainText = userInput.readLine();
    
    // Prompt, then wait and get user input for the key
    System.out.println("Enter the key:");
    String key = userInput.readLine();
    
    // Actually encrypt it
    byte[] encrypted = MARS.encrypt(plainText.getBytes(), key.getBytes());
    
    // Print encrypted and unencrypted
    System.out.println("Plain text: " + plainText);
    System.out.println("Encrypted Text: " + new String(encrypted));