Click here to Skip to main content
15,887,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I’m trying to develop program that the user will enter a text in textbox (plaintext) then the user have to select a key buttons (key_128) (key_192) (key_256) then it will click on button (encrypt_data) to encrypt the text that the user entered. Then it will show the output in another textbox (encrypt)
This is the source code I’m working on

Java
public class MARS extends javax.swing.JFrame {

    static Cipher cipher;
	
	private static int[] K;
        

	private static final int [] s_box ={
// s_box values here
	};
	
	private static int rotl(int val, int pas) {
		return (val << pas) | (val >>> (32 - pas));
	}
	private static int rotr(int val, int pas) {
		return (val >>> pas) | (val << (32-pas));
	}
	
	
	private static int[] expandKey(byte[] key){
		int n = key.length/4;
		int[] tmp = new int[40];
		int[] data = new int[n];
		
		for(int i =0;i<data.length;i++)
			data[i] = 0;
		
		int off = 0;
		for(int i=0;i<data.length;i++){
			data[i] = 	((key[off++]&0xff))|
						((key[off++]&0xff) << 8) |
						((key[off++]&0xff) << 16) |
						((key[off++]&0xff) << 24);
		}
		
		int[] T = new int[15];
		for(int i=0;i<T.length;i++){
			if(i<data.length) T[i] = data[i];
			else if(i == data.length) T[i] = n;
			else T[i] = 0;
		}
		
		for(int j=0;j<4;j++){
			for(int i=0;i<T.length;i++)
				T[i] = T[i] ^ (rotl(T[Math.abs(i-7 %15)] ^ T[Math.abs(i-2 %15)],3) ^ (4*i+j));
			for(int c=0;c<4;c++)
				for(int i=0;i<T.length;i++)
					T[i] = T[i] + rotl(s_box[(int)(T[Math.abs(i-1%15)] & 0x000001ff)],9);
			for(int i = 0;i<=9;i++) tmp[10*j+i] = T[4*i%15];
		}
		
		int[] B = {0xa4a8d57b, 0x5b5d193b, 0xc8a8309b, 0x73f9a978};
		int j,w,m,r,p;
		for(int i = 5;i<=35;i++){
			j = tmp[i] & 0x00000003;
			w = tmp[i] | 0x00000003;
			m = generateMask(w);
			r = tmp[i-1] & 0x0000001f;
			p = rotl(B[j],r);
			tmp[i] = w ^ (p & m); 
		}
		
		return tmp;
	}
	
	private static int generateMask(int x){
		int m;
		
		m = (~x ^ (x>>>1)) & 0x7fffffff;
		m &= (m >> 1) & (m >> 2); 
		m &= (m >> 3) & (m >> 6); 
		    
		if(m == 0) 
			return 0;

		m <<= 1; m |= (m << 1); m |= (m << 2); m |= (m << 4);

		m |= (m << 1) & ~x & 0x80000000;

		return m & 0xfffffffc;

	}
	
	
	public static byte[] encryptBloc(byte[] in){
		byte[] tmp =  new byte[in.length];
		int aux;

		
		int[] data = new int[in.length/4];
		for(int i =0;i<data.length;i++)
			data[i] = 0;
		int off = 0;
		for(int i=0;i<data.length;i++){
			data[i] = 	((in[off++]&0xff))|
						((in[off++]&0xff) << 8) |
						((in[off++]&0xff) << 16) |
						((in[off++]&0xff) << 24);
		}
	
		int A = data[0],B = data[1],C = data[2],D = data[3];
		A = A + K[0];
		B = B + K[1];
		C = C + K[2];
		D = D + K[3];
		
		//forward mixing
		for(int i = 0;i<=7;i++){
			B = B ^ s_box[A & 0xff];
			B = B + s_box[(rotr(A,8) & 0xff) + 256]; 
            C = C + s_box[rotr(A,16) & 0xff]; 
            D = D ^ s_box[(rotr(A,24) & 0xff) + 256]; 
            
            A = rotr(A,24);
            
            if(i == 1 || i == 5) A = A + B;
            if(i == 0 || i == 4) A = A + D;
            
            aux = A;
			A = B;
			B = C;
			C = D;
			D = aux;
		}
		int R,L,M;
		int[] eout;
		//cryptographic core
		for(int i = 0;i<=15;i++){
			
			eout = E_func(A, K[2*i+4], K[2*i+5]);
			
			A = rotl(A,13);
			C = C + eout[1];
			
			if(i<8) {
				B = B + eout[0];
				D = D ^ eout[2];
			}
			else{
				D = D + eout[0];
				B = B ^ eout[2];
			}
			
			aux = A;
			A = B;
			B = C;
			C = D;
			D = aux;
		}
		//backward mixing
		for(int i = 0;i<=7;i++){
			
			if(i == 3 || i == 7) A = A - B;
			if(i == 2 || i == 6) A = A - D;
			
			B = B ^ s_box[(A & 0xff) + 256];
			C = C - s_box[rotr(A,24) & 0xff];
			D = D - s_box[(rotr(A,16) & 0xff) + 256];
			D = D ^ s_box[rotr(A,8) & 0xff];
			
			A = rotl(A,24);
			
			aux = A;
			A = B;
			B = C;
			C = D;
			D = aux;

		}
		A = A - K[36];
		B = B - K[37];
		C = C - K[38];
		D = D - K[39];
		
		data[0] = A;data[1] = B;data[2] = C;data[3] = D;
		
		for(int i = 0;i<tmp.length;i++){
			tmp[i] = (byte)((data[i/4] >>> (i%4)*8) & 0xff);
		}
		
		return tmp;	
	}
	
	private static int[] E_func(int in,int k1,int k2){
		int[] tmp = new int[3];
		int M,L,R;
		M = in + k1;
		R = rotl(in,13) * k2;
		L = s_box[M & 0x000001ff];
		R = rotl(R,5);
		M = rotl(M,R & 0x0000001f);
		L = L ^ R;
		R = rotl(R,5);
		L = L ^ R;
		L = rotl(L,R & 0x0000001f);
		
		tmp[0] = L;
		tmp[1] = M;
		tmp[2] = R;
		
		return tmp;
	}
	
	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 code I wrote in (encrypt_data) button but it gives error :(

Java
byte[] plain = plaintext.getText().getBytes();
        if (key_128.isSelected())
       {
         byte[] K = getText().getBytes();
       }

       else if (key_192.isSelected())
       {
         byte[] K = getText().getBytes();
       }
     else if (key_256.isSelected()){
        byte[] K = getText().getBytes();
     }
       byte[] encrypted = encrypt(plain, K);
       String encrypted = encrypt.setText();


And I need to set values in key buttons (key_128) (key_192) (key_256). How I can do that in java? Each key will have binary value

What I have tried:

i tried seting values in encrypt_data button but didn't work
Posted
Updated 22-Mar-18 8:15am
Comments
Richard MacCutchan 21-Mar-18 13:15pm    
What error? But it would be much easier to throw away all that spaghetti code, and use the Java encryption classes: java encryption - Google Search[^].
Member 13725790 22-Mar-18 9:50am    
@Richard MacCutchan. Sorry for my late reply but the problem is in getText() that in if else statements and the last line showing the result in textbox encrypt
Richard MacCutchan 22-Mar-18 10:15am    
Looking at your code, I don't think you can call getText() in that way. That call should be linked to a JTextField object.
Member 13725790 22-Mar-18 12:05pm    
I just changed the whole code to what you recommended. But i’m not sure how to get the output in Jtextbox or give the K a integer value so once the user click that key it directly calculate like calculator 😥 how i can do that? I tried searching but nothing worked
Richard MacCutchan 22-Mar-18 12:22pm    
Sorry, I don't know either; your code is not easy to understand. I suggest you go to the Java documentation (Overview (Java Platform SE 8 )[^]) for specific questions, and The Java™ Tutorials[^] for general code samples and tutorials.

At a guess, you're getting a compiler error because you've declared K inside the if / else if blocks, but you're trying to use it outside of those blocks.

Move the declaration above the first if statement. You'll also need to handle the case where none of the three options is selected - you know it will never happen, but the compiler doesn't, and will complain about it.
byte[] plain = plaintext.getText().getBytes();
byte[] K;
if (key_128.isSelected())
{
    K = getText().getBytes();
}
else if (key_192.isSelected())
{
    K = getText().getBytes();
}
else if (key_256.isSelected())
{
    K = getText().getBytes();
}
else
{
    throw new IllegalStateException();
}

Now you just need to work out why you have the if statements at all, given that every branch does exactly the same thing!
 
Share this answer
 
Comments
Member 13725790 22-Mar-18 9:53am    
You’re right i’m for The null option i just added but the problem same still. And about what you mentioned i tried that but later ill need the system to caculate the encryption time so im thinking it will b right from the user click the encryption button till the results shows. But ill try as u say hopefully it works. Thank you for helping me out
this is the solution i came up with but there is error in the second line. i'm trying to solve and i posted question if anyone could help. thank you!

Java
byte[] plain = plaintext.getText().getBytes();
byte[] K = new BinaryCodec().toByteArray("0011000100110011100000001010");
byte[] encrypted = encrypt(plain, K);
String a = new String(encrypted);
encryptedtext.setText(a);
 
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