2014-12-02 79 views
-1

我在Java中使用這些代碼來對PRINCE進行加密和解密。這些是Sbox和Sbox Inverse的代碼。每當我運行該程序,我得到和錯誤消息:錯誤:線程「main」中的異常java.lang.ArrayIndexOutOfBoundsException:-1

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 
    at prince.SInv(prince.java:146) 
    at prince.Encrypt(prince.java:485) 
    at prince_analysis_main.main(prince_analysis_main.java:21) 

我已經實現try和catch方法參照http://mcnewton.org/blog/tag/exception-in-thread-main-java-lang-arrayindexoutofboundsexception-5/但它並沒有解決問題。 我可以知道,如果這個錯誤信息是由有限的內存分配引起的還是執行錯誤?

public static long S(long x) { 
      long[] sbox_8bits = new long[] { 0xBB, 0xBF, 0xB3, 0xB2, 0xBA, 0xBC, 
       0xB9, 0xB1, 0xB6, 0xB7, 0xB8, 0xB0, 0xBE, 0xB5, 0xBD, 0xB4, 
       0xFB, 0xFF, 0xF3, 0xF2, 0xFA, 0xFC, 0xF9, 0xF1, 0xF6, 0xF7, 
       0xF8, 0xF0, 0xFE, 0xF5, 0xFD, 0xF4, 0x3B, 0x3F, 0x33, 0x32, 
       0x3A, 0x3C, 0x39, 0x31, 0x36, 0x37, 0x38, 0x30, 0x3E, 0x35, 
       0x3D, 0x34, 0x2B, 0x2F, 0x23, 0x22, 0x2A, 0x2C, 0x29, 0x21, 
       0x26, 0x27, 0x28, 0x20, 0x2E, 0x25, 0x2D, 0x24, 0xAB, 0xAF, 
       0xA3, 0xA2, 0xAA, 0xAC, 0xA9, 0xA1, 0xA6, 0xA7, 0xA8, 0xA0, 
       0xAE, 0xA5, 0xAD, 0xA4, 0xCB, 0xCF, 0xC3, 0xC2, 0xCA, 0xCC, 
       0xC9, 0xC1, 0xC6, 0xC7, 0xC8, 0xC0, 0xCE, 0xC5, 0xCD, 0xC4, 
       0x9B, 0x9F, 0x93, 0x92, 0x9A, 0x9C, 0x99, 0x91, 0x96, 0x97, 
       0x98, 0x90, 0x9E, 0x95, 0x9D, 0x94, 0x1B, 0x1F, 0x13, 0x12, 
       0x1A, 0x1C, 0x19, 0x11, 0x16, 0x17, 0x18, 0x10, 0x1E, 0x15, 
       0x1D, 0x14, 0x6B, 0x6F, 0x63, 0x62, 0x6A, 0x6C, 0x69, 0x61, 
       0x66, 0x67, 0x68, 0x60, 0x6E, 0x65, 0x6D, 0x64, 0x7B, 0x7F, 
       0x73, 0x72, 0x7A, 0x7C, 0x79, 0x71, 0x76, 0x77, 0x78, 0x70, 
       0x7E, 0x75, 0x7D, 0x74, 0x8B, 0x8F, 0x83, 0x82, 0x8A, 0x8C, 
       0x89, 0x81, 0x86, 0x87, 0x88, 0x80, 0x8E, 0x85, 0x8D, 0x84, 
       0x0B, 0x0F, 0x03, 0x02, 0x0A, 0x0C, 0x09, 0x01, 0x06, 0x07, 
       0x08, 0x00, 0x0E, 0x05, 0x0D, 0x04, 0xEB, 0xEF, 0xE3, 0xE2, 
       0xEA, 0xEC, 0xE9, 0xE1, 0xE6, 0xE7, 0xE8, 0xE0, 0xEE, 0xE5, 
       0xED, 0xE4, 0x5B, 0x5F, 0x53, 0x52, 0x5A, 0x5C, 0x59, 0x51, 
       0x56, 0x57, 0x58, 0x50, 0x5E, 0x55, 0x5D, 0x54, 0xDB, 0xDF, 
       0xD3, 0xD2, 0xDA, 0xDC, 0xD9, 0xD1, 0xD6, 0xD7, 0xD8, 0xD0, 
       0xDE, 0xD5, 0xDD, 0xD4, 0x4B, 0x4F, 0x43, 0x42, 0x4A, 0x4C, 
       0x49, 0x41, 0x46, 0x47, 0x48, 0x40, 0x4E, 0x45, 0x4D, 0x44 }; 
      return (((long)sbox_8bits[(int) ((x & 0xFF00000000000000l) >> 56)] << 56)^
        ((long)sbox_8bits[(int) ((x & 0x00FF000000000000l) >> 48)] << 48)^
        ((long)sbox_8bits[(int) ((x & 0x0000FF0000000000l) >> 40)] << 40)^
        ((long)sbox_8bits[(int) ((x & 0x000000FF00000000l) >> 32)] << 32)^
        ((long)sbox_8bits[(int) ((x & 0x00000000FF000000l) >> 24)] << 24)^
        ((long)sbox_8bits[(int) ((x & 0x0000000000FF0000l) >> 16)] << 16)^
        ((long)sbox_8bits[(int) ((x & 0x000000000000FF00l) >> 8)] << 8)^
        (long)sbox_8bits[(int) (x & 0x00000000000000FFl)]); 
    } 

    public static long SInv(long x) { 
     long[] sboxInv_8bits = new long[] { 0xBB, 0xB7, 0xB3, 0xB2, 0xBF, 0xBD, 
       0xB8, 0xB9, 0xBA, 0xB6, 0xB4, 0xB0, 0xB5, 0xBE, 0xBC, 0xB1, 
       0x7B, 0x77, 0x73, 0x72, 0x7F, 0x7D, 0x78, 0x79, 0x7A, 0x76, 
       0x74, 0x70, 0x75, 0x7E, 0x7C, 0x71, 0x3B, 0x37, 0x33, 0x32, 
       0x3F, 0x3D, 0x38, 0x39, 0x3A, 0x36, 0x34, 0x30, 0x35, 0x3E, 
       0x3C, 0x31, 0x2B, 0x27, 0x23, 0x22, 0x2F, 0x2D, 0x28, 0x29, 
       0x2A, 0x26, 0x24, 0x20, 0x25, 0x2E, 0x2C, 0x21, 0xFB, 0xF7, 
       0xF3, 0xF2, 0xFF, 0xFD, 0xF8, 0xF9, 0xFA, 0xF6, 0xF4, 0xF0, 
       0xF5, 0xFE, 0xFC, 0xF1, 0xDB, 0xD7, 0xD3, 0xD2, 0xDF, 0xDD, 
       0xD8, 0xD9, 0xDA, 0xD6, 0xD4, 0xD0, 0xD5, 0xDE, 0xDC, 0xD1, 
       0x8B, 0x87, 0x83, 0x82, 0x8F, 0x8D, 0x88, 0x89, 0x8A, 0x86, 
       0x84, 0x80, 0x85, 0x8E, 0x8C, 0x81, 0x9B, 0x97, 0x93, 0x92, 
       0x9F, 0x9D, 0x98, 0x99, 0x9A, 0x96, 0x94, 0x90, 0x95, 0x9E, 
       0x9C, 0x91, 0xAB, 0xA7, 0xA3, 0xA2, 0xAF, 0xAD, 0xA8, 0xA9, 
       0xAA, 0xA6, 0xA4, 0xA0, 0xA5, 0xAE, 0xAC, 0xA1, 0x6B, 0x67, 
       0x63, 0x62, 0x6F, 0x6D, 0x68, 0x69, 0x6A, 0x66, 0x64, 0x60, 
       0x65, 0x6E, 0x6C, 0x61, 0x4B, 0x47, 0x43, 0x42, 0x4F, 0x4D, 
       0x48, 0x49, 0x4A, 0x46, 0x44, 0x40, 0x45, 0x4E, 0x4C, 0x41, 
       0x0B, 0x07, 0x03, 0x02, 0x0F, 0x0D, 0x08, 0x09, 0x0A, 0x06, 
       0x04, 0x00, 0x05, 0x0E, 0x0C, 0x01, 0x5B, 0x57, 0x53, 0x52, 
       0x5F, 0x5D, 0x58, 0x59, 0x5A, 0x56, 0x54, 0x50, 0x55, 0x5E, 
       0x5C, 0x51, 0xEB, 0xE7, 0xE3, 0xE2, 0xEF, 0xED, 0xE8, 0xE9, 
       0xEA, 0xE6, 0xE4, 0xE0, 0xE5, 0xEE, 0xEC, 0xE1, 0xCB, 0xC7, 
       0xC3, 0xC2, 0xCF, 0xCD, 0xC8, 0xC9, 0xCA, 0xC6, 0xC4, 0xC0, 
       0xC5, 0xCE, 0xCC, 0xC1, 0x1B, 0x17, 0x13, 0x12, 0x1F, 0x1D, 
       0x18, 0x19, 0x1A, 0x16, 0x14, 0x10, 0x15, 0x1E, 0x1C, 0x11 }; 
     return (((long)sboxInv_8bits[(int) ((x & 0xFF00000000000000l) >> 56)] << 56)^
        ((long)sboxInv_8bits[(int) ((x & 0x00FF000000000000l) >> 48)] << 48)^
        ((long)sboxInv_8bits[(int) ((x & 0x0000FF0000000000l) >> 40)] << 40)^
        ((long)sboxInv_8bits[(int) ((x & 0x000000FF00000000l) >> 32)] << 32)^
        ((long)sboxInv_8bits[(int) ((x & 0x00000000FF000000l) >> 24)] << 24)^
        ((long)sboxInv_8bits[(int) ((x & 0x0000000000FF0000l) >> 16)] << 16)^
        ((long)sboxInv_8bits[(int) ((x & 0x000000000000FF00l) >> 8)] << 8)^
        (long)sboxInv_8bits[ (int) (x & 0x00000000000000FFl)]); 
    } 
+0

這不是完整的代碼。你的主要方法在哪裏?在哪一行是拋出的異常。請學習基本的調試。你的[其他問題](http://stackoverflow.com/questions/27126145/java-formula-parsing-error)也沒有做得太好。順便說一句,你爲什麼創建一個新的帳戶? – 2014-12-02 09:48:49

回答

0

數組索引越界異常意味着你試圖訪問 不存在數組的索引。

示例如果您的數組中有5個元素表示要求第6個元素。

我會看看你的SInv函數爲你的數組索引計算。

相關問題