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

addRoundKey ステップは、暗号化しようとしている暗号鍵とテキストのそれぞれから列を取得し、それらを xor すると思いますよね?

これが私の実装です。「範囲外の値」エラーが発生する理由は理解できます。これは、byte が -128 から 127 の範囲の数値を取るためですよね?

しかし、私はそれを修正する方法がよくわかりません。Byte である keyArray のタイプを変更できません。

4

2 に答える 2

1

「99」byteを基数 16 として解析するとエラーが発生します。これは、次のように言い換えることができます。

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

bytesignedで、有効な範囲は -128 から 127 ですが、あなたは

最初に Integer.parseInt() を使用して整数として解析し、次にそれを符号付きバイトに変換します。

keyArray[row][col] = (byte)Integer.parseInt(result, 16);
于 2013-11-02T04:52:44.233 に答える
1

行を変更

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

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


ボヘミアンの答えで正しく述べられているように、編集またはさらに短く:

keyArray[row][col] = (byte) Integer.parseInt(result, 16);
于 2013-11-01T20:54:36.717 に答える