Results 1 to 3 of 3
  1. #1
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default AES Implementation issues.

    Hey guys! So I know this isn't technically an applet, it's a class, but I can't think of anywhere to put this. Basically, I've written my own implementation of the Rijndael or AES cipher. Now, As far as Im aware, the four major transformations are completely fine, so I'd rather not check them for the time being, until we have to. What worries me is my key expansion algorithm. I just think there's something wrong. Here is the code:

    Java Code:
    private String[][] keyExpansion()
    	{
    		// Expands the Encryption key according to the Rijndael Key Schedule.
    		
    		String[][] output = convertToTwoD(hexVal(encKey));
    		// Store the output of the encryption Key.
    		int pointer = 0;
    		
    		if(output != null)
    		{
    			String[] rotWord = {output[3][pointer], output[3][pointer], output[3][pointer], output[3][pointer]};
    			// Ensure output isn't empty and extract 'rotword'.
    		
    			rotWord = cycle(rotWord, 1);
    
    			rotWord = subBytes(rotWord);
    			// Take the rot word, and cycle it once, then subBytes() it.
    			
    			String[][] expandedKeys = new String[45][4];
    			// Initialise the expandedKeys array.
    			for(int c = 0; c < 4;  c++)
    			{
    				for(int d = 0; d < 4; d++)
    				{
    					expandedKeys[c][d] = output[c][d];
    					// Place the original encryption key in the expanded keys.
    				}
    			}
    			int i = 0;
    					
    			
    			for(int counter = 4; counter < 45; counter ++)
    			{
    				for(int j=0; j < 4; j++)
    				{
    					// Generate the rest of the keys.
    					
    					
    					int temp2 = Integer.parseInt(expandedKeys[counter - 4][j],16);
    					
    					int temp3 = Integer.parseInt(rotWord[j], 16);
    					
    					if(j == 0)
    					{
    						expandedKeys[counter][j] =  Integer.toHexString(temp2 ^ temp3 ^ rCon[i]);
    					}
    					else
    					{
    						expandedKeys[counter][j] =  Integer.toHexString(temp2 ^ temp3 ^ 0);
    					}
    					// i is the pointer for the rCon table.
    					i ++;
    				}
    			}
    			
    			return expandedKeys;
    		}
    		else
    		{
    			// If the conversion to a 2d array returns a null.
    			return null;
    		}
    	}
    It's String[][] because it's a multidimensional array of hexidecimal characters. I know it's memory heavy but it was the best way for me to get clean access to thedifferent values.

    Right, so basically, I throw in some plaintext: "Hello my name is christopher and I am testing my AES Implementation."

    That gets split up into blocks of 4x4 multidimensional arrays.
    I've already provided a key, which is a multidimensional array of integers.
    Now I think what's happening here, is the keys are being computed wrong. Here's the output from a test:

    Plaintext : Hello my name is christopher and I am testing my AES Implementation.
    Ciphertext : @6RRapoz7k6X"c ׅyӐż)[r$y?vaNUvh׶A1 Sb# S8

    Deciphered Text: zώ*7QJio5b\ĆMy,,-]gG=dP)%
    oN`WN"Gm+[#+eT

    As you've guessed, deciphered text should be "hello my name is christopher an I am testing my AES implementation".

    One more thing which I found really very weird. I use an rCon table instead of performing the calculation, and in every instructional video I see, it's something like

    01 02 04 08 0f 20
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0


    But the rcon table I have looks like this:

    private int[] rCon = {

    0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
    0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39,
    0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
    0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
    0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef,
    0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
    0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b,
    0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
    0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
    0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
    0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
    0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
    0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
    0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
    0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
    0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d
    };

    Kinda worrying eh! So what I did was in my code for the last three iterations I made the rCon value 0, so simulate the table. I know it's sucky, but like I said it's only a simulation, not a finished product. Still didn't make a different.

    I know this is alot to take in, but anyone who can help, PLEASE chime in!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: AES Implementation issues.

    Is this a problem with your algorithm
    or is it a problem writing java code to implement your algorithm/
    If the latter please explain your algorithm and ask specific questions about the coding problem you are having.

  3. #3
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: AES Implementation issues.

    It is a problem with the code. I just think that the key schedule I am generating with the above method is not the key schedule I should be generating, but I can't see any problems with this method... or the rest of my code. I understand the rest better, so I figured i'd start where I'm most likely to go wrong, with Key expansion :)

Similar Threads

  1. implementation of interfaces
    By Dayanand in forum New To Java
    Replies: 5
    Last Post: 03-09-2011, 09:51 AM
  2. AI implementation
    By vxaviern in forum Advanced Java
    Replies: 2
    Last Post: 11-18-2010, 10:02 AM
  3. Interface Implementation
    By Samyx in forum New To Java
    Replies: 3
    Last Post: 12-02-2009, 01:20 PM
  4. Web service Implementation
    By kirtichopra2003 in forum Advanced Java
    Replies: 0
    Last Post: 10-14-2009, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •