0
/*keyArray contains a line of cipherkey, and inputArray contains a text that is being encrypted.*/
public static void addRoundKey() {
        String keyEle = "";
        String inputEle = "";
        String result = "";
        for(int col=0; col<4; col++) {
            for(int row = 0; row<4; row++) {                
                keyEle = Integer.toHexString(keyArray[row][col] & 0xff);
                inputEle = Integer.toHexString(inputArray[row][col] & 0xff);
                if(keyEle.equals("0")) {
                    keyEle = "00";
                }
                if(inputEle.equals("0")) {
                    inputEle = "00";
                }
                BigInteger keyNum = new BigInteger(keyEle,16);
                BigInteger inputNum = new BigInteger(inputEle, 16);
                result = keyNum.xor(inputNum).toString();
                System.out.println("result = " + result);
                keyArray[row][col] = Byte.valueOf(result, 16); 
                //The above line causes Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"99" Radix:16`

                //keyArray[row][col]  = (byte) (Integer.parseInt(result) & 0xff);           
            }
        }
    }

I think addRoundKey step takes a column from each of cipher key and text that I am trying to encrypt, and then xor them, right?

So, that's my implementation, I understand why "value out of range" error occurs, it's because byte takes numbers that range from -128 to 127, right?

But I am not so sure how to fix it. I can't change the type of keyArray, which is Byte.

Nayana
  • 1,513
  • 3
  • 24
  • 39
  • Could you please post exact error and line it complains about ? – kiruwka Nov 01 '13 at 20:18
  • They are already in my code as comments. Please check the comment in this line, "keyArray[row][col] = Byte.valueOf(result, 16);" – Nayana Nov 01 '13 at 20:20

2 Answers2

1

Change line

keyArray[row][col] = Byte.valueOf(result, 16);

to

keyArray[row][col] = (byte) Integer.valueOf(result, 16).intValue();

edit
or even shorter, as correctly stated in Bohemian's answer :

keyArray[row][col] = (byte) Integer.parseInt(result, 16);
kiruwka
  • 9,250
  • 4
  • 30
  • 41
  • Thank you for the comment. This compiled, but still doesn't give me the correct result. Do you know if there is anything wrong with my implementation of AES's "add round key"? – Nayana Nov 02 '13 at 01:43
  • @Nayana no idea, sorry – kiruwka Nov 02 '13 at 08:15
1

You are getting an error parsing "99" as a byte using base 16, which may be paraphrased as:

byte b = Byte.valueOf("99", 16);

because byte is signed, with valid range -128 to 127, but you are

First parse it as an Integer, using Integer.parseInt(), then convert it to a signed byte, eg:

keyArray[row][col] = (byte)Integer.parseInt(result, 16);
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • +1 for mentioning `Integer.parseInt` instead of `Integer.valueOf` in my answer. However, `byte b = (byte) (i < 128 ? i : i - 256);` is equivalent to `byte b = (byte) i;` BTW, your original assignment `keyArray[row][col] = i < 128 ? i : i - 256;` without cast to byte won't compile – kiruwka Nov 02 '13 at 16:04
  • @kiruwka THANJS for the feedback. I type all my code in using an iPhone, so I sometimes overlook stuff like that. I've corrected my answer - looks much better now:) Cheers – Bohemian Nov 02 '13 at 21:36