Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

Java
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

Java
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));
    }
}


What I have tried:

tried to read the user input :(
Posted
Updated 16-Mar-18 8:02am

1 solution

BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Text that you want to encrypt:");
String plainText = userInput.readLine();
System.out.println("Enter the key:");
String key = userInput.readLine();
byte[] encrypted = MARS.encrypt(plainText.getBytes(), key.getBytes())
System.out.println("Plain text: " + plainText);
System.out.println("Encrypted Text: " + new String(encrypted));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900