⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serpent_standard.java

📁 密码serpent算法源代码, 该算法是AES候选算法之一
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        byte[] result = new byte[] {            (byte)(a), (byte)(a >>> 8), (byte)(a >>> 16), (byte)(a >>> 24),            (byte)(b), (byte)(b >>> 8), (byte)(b >>> 16), (byte)(b >>> 24),            (byte)(c), (byte)(c >>> 8), (byte)(c >>> 16), (byte)(c >>> 24),            (byte)(d), (byte)(d >>> 8), (byte)(d >>> 16), (byte)(d >>> 24)        };if (DEBUG && debuglevel > 6) {System.out.println("PT="+toString(result));System.out.println();}if (DEBUG) trace(OUT, "blockDecrypt()");        return result;    }// own methods//...........................................................................    /**     * @return The bit value at position <code>i</code> in a 32-bit entity,     *      where the least significant bit (the rightmost one) is at     *      position 0.     */    private static int getBit (int x, int i) { return (x >>> i) & 0x01; }        /**     * @return The bit value at position <code>i</code> in an array of 32-bit     *      entities, where the least significant 32-bit entity is at index     *      position 0 and the least significant bit (the rightmost one) in     *      any 32-bit entity is at position 0.     */    private static int getBit (int[] x, int i) {        return (x[i / 32] >>> (i % 32)) & 0x01;    }    /**     * Set the bit at position <code>i</code> in an array of 32-bit entities     * to a given value <code>v</code>, where the least significant 32-bit     * entity is at index position 0 and the least significant bit (the     * rightmost one) in any 32-bit entity is at position 0.     */    private static void setBit (int[] x, int i, int v) {        if ((v & 0x01) == 1)            x[i / 32] |= 1 << (i % 32); // set it        else            x[i / 32] &= ~(1 << (i % 32)); // clear it    }    /**     * @return The nibble --a 4-bit entity-- in <code>x</code> given its     *      position <code>i</code>, where the least significant nibble     *      (the rightmost one) is at position 0.     */    private static int getNibble (int x, int i) { return (x >>> (4 * i)) & 0x0F; }    /**     * @return A 128-bit entity which is the result of applying the Initial     *      Permutation (IP) to a 128-bit entity <code>x</code>.     */    private static int[] IP (int[] x) { return permutate(IPtable, x); }    /**     * @return A 128-bit entity which is the result of applying the inverse of     *      the Initial Permutation to a 128-bit entity <code>x</code>.     */    private static int[] IPinverse (int[] x) { return permutate(FPtable, x); }    /**     * @return A 128-bit entity which is the result of applying the Final     *      Permutation (FP) to a 128-bit entity <code>x</code>.     */    private static int[] FP (int[] x) { return permutate(FPtable, x); }    /**     * @return A 128-bit entity which is the result of applying the inverse of     *      the Final Permutation to a 128-bit entity <code>x</code>.     */    private static int[] FPinverse (int[] x) { return permutate(IPtable, x); }        /**     * @return A 128-bit entity which is the result of applying a permutation     *      coded in a given table <code>T</code> to a 128-bit entity     *      <code>x</code>.     */    private static int[] permutate (byte[] T, int[] x) {        int[] result = new int[4];        for (int i = 0;  i < 128; i++)            setBit(result, i, getBit(x, T[i] & 0x7F));        return result;    }    /**     * @return A 128-bit entity as the result of XORing, bit-by-bit, two given     *      128-bit entities <code>x</code> and <code>y</code>.     */    private static int[] xor128 (int[] x, int[] y) {        return new int[] {x[0] ^ y[0], x[1] ^ y[1], x[2] ^ y[2], x[3] ^ y[3]};    }    /**     * @return The nibble --a 4-bit entity-- obtained by applying a given     *      S-box to a 32-bit entity <code>x</code>.     */    private static int S (int box, int x) { return Sbox[box][x] & 0x0F; }    /**     * @return The nibble --a 4-bit entity-- obtained byapplying the inverse     *      of a given S-box to a 32-bit entity <code>x</code>.     */    private static int Sinverse (int box, int x) { return SboxInverse[box][x] & 0x0F; }    /**     * @return A 128-bit entity being the result of applying, in parallel,     *      32 copies of a given S-box to a 128-bit entity <code>x</code>.     */    private static int[] Shat (int box, int[] x) {        int[] result = new int[4];        for (int i = 0; i < 4; i++)            for (int nibble = 0; nibble < 8; nibble++)                result[i] |= S(box, getNibble(x[i], nibble)) << (nibble * 4);        return result;    }    /**     * @return A 128-bit entity being the result of applying, in parallel,     *      32 copies of the inverse of a given S-box to a 128-bit entity     *      <code>x</code>.     */    private static int[] ShatInverse (int box, int[] x) {        int[] result = new int[4];        for (int i = 0; i < 4; i++)            for (int nibble = 0; nibble < 8; nibble++)                result[i] |= Sinverse(box, getNibble(x[i], nibble)) << (nibble * 4);        return result;    }    /**     * @return A 128-bit entity being the result of applying the linear     *      transformation to a 128-bit entity <code>x</code>.     */    private static int[] LT (int[] x) { return transform(LTtable, x); }    /**     * @return A 128-bit entity being the result of applying the inverse of     *      the linear transformation to a 128-bit entity <code>x</code>.     */    private static int[] LTinverse (int[] x) {        return transform(LTtableInverse, x);    }    /**     * @return A 128-bit entity being the result of applying a transformation     *      coded in a table <code>T</code> to a 128-bit entity <code>x</code>.     *      Each row, of say index <code>i</code>, in <code>T</code> indicates     *      the bits from <code>x</code> to be XORed together in order to     *      produce the resulting bit at position <code>i</code>.     */    private static int[] transform (byte[][] T, int[] x) {        int j, b;        int[] result = new int[4];        for (int i = 0; i < 128; i++) {            b = 0;            j = 0;            while (T[i][j] != xFF) {                b ^= getBit(x, T[i][j] & 0x7F);                j++;            }            setBit(result, i, b);        }        return result;    }    /**     * @return the 128-bit entity as the result of applying the round function     *      R at round <code>i</code> to the 128-bit entity <code>Bhati</code>,     *      using the appropriate subkeys from <code>Khat</code>.     */    private static int[] R (int i, int[] Bhati, int[][] Khat) {if (DEBUG && debuglevel > 6) debug("Bhat["+i+"]: "+toReversedString(Bhati));        int[] xored = xor128(Bhati, Khat[i]);if (DEBUG && debuglevel > 6) debug("xored["+i+"]: "+toReversedString(xored));        int[] Shati = Shat(i, xored);if (DEBUG && debuglevel > 6) debug("Shat["+i+"]: "+toReversedString(Shati));        int[] BhatiPlus1;        if ((0 <= i) && (i <= ROUNDS - 2))            BhatiPlus1 = LT(Shati);        else if (i == ROUNDS - 1)            BhatiPlus1 = xor128(Shati, Khat[ROUNDS]);        else            throw new RuntimeException(                "Round "+i+" is out of 0.."+(ROUNDS-1)+" range");        return BhatiPlus1;    }    /**     * @return the 128-bit entity as the result of applying the inverse of     *      the round function R at round <code>i</code> to the 128-bit     *      entity <code>Bhati</code>, using the appropriate subkeys from     *      <code>Khat</code>.     */    private static int[] Rinverse (int i, int[] BhatiPlus1, int[][] Khat) {if (DEBUG && debuglevel > 6) debug("Bhat["+i+"+1]: "+toReversedString(BhatiPlus1));        int[] Shati = new int[4];        if ((0 <= i) && (i <= ROUNDS - 2))            Shati = LTinverse(BhatiPlus1);        else if (i == ROUNDS - 1)            Shati = xor128(BhatiPlus1, Khat[ROUNDS]);        else            throw new RuntimeException(                "Round "+i+" is out of 0.."+(ROUNDS-1)+" range");if (DEBUG && debuglevel > 6) debug("Shat["+i+"]: "+toReversedString(Shati));        int[] xored = ShatInverse(i, Shati);if (DEBUG && debuglevel > 6) debug("xored["+i+"]: "+toReversedString(xored));        int[] Bhati = xor128(xored, Khat[i]);        return Bhati;    }    private static int[] Rinverse (int i, int[] BhatiPlus1, int[][] Khat, int in, int val) {        int[] Shati = new int[4];        if ((0 <= i) && (i <= ROUNDS - 2))            Shati = LTinverse(BhatiPlus1);        else if (i == ROUNDS - 1)            Shati = xor128(BhatiPlus1, Khat[ROUNDS]);        else            throw new RuntimeException(                "Round "+i+" is out of 0.."+(ROUNDS-1)+" range");        int[] xored = ShatInverse(i, Shati);	if(i==in) {	  xored[0] = val | (val<<4);	  xored[0] |= (xored[0]<<8);	  xored[0] |= (xored[0]<<16);	  xored[1] = xored[2] = xored[3] = xored[0];	}        int[] Bhati = xor128(xored, Khat[i]);        return Bhati;    }// utility static methods (from cryptix.util.core.Hex class)//...........................................................................    /**     * Returns a string of 8 hexadecimal digits (most significant     * digit first) corresponding to the integer <i>n</i>, which is     * treated as unsigned.     */    public static String intToString (int n) {        char[] buf = new char[8];        for (int i = 7; i >= 0; i--) {            buf[i] = HEX_DIGITS[n & 0x0F];            n >>>= 4;        }        return new String(buf);    }    /**     * Returns a string of hexadecimal digits from a byte array. Each     * byte is converted to 2 hex symbols.     */    private static String toString (byte[] ba) {        int length = ba.length;        char[] buf = new char[length * 2];        for (int i = 0, j = 0, k; i < length; ) {            k = ba[i++];            buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];            buf[j++] = HEX_DIGITS[ k        & 0x0F];        }        return new String(buf);    }    /**     * Returns a string of hexadecimal digits from an integer array. Each     * int is converted to 4 hex symbols.     */    private static String toString (int[] ia) {        int length = ia.length;        char[] buf = new char[length * 8];        for (int i = 0, j = 0, k; i < length; i++) {            k = ia[i];            buf[j++] = HEX_DIGITS[(k >>> 28) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>> 24) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>> 20) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>> 16) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>> 12) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>>  8) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>>  4) & 0x0F];            buf[j++] = HEX_DIGITS[ k         & 0x0F];        }        return new String(buf);    }// other utility static methods//...........................................................................    /**     * Returns an hexadecimal number (respresented as a string of hexadecimal      * digits from a byte array). Each byte is converted to 2 hex symbols.     * The order is however, as of printing a number from a little-endian     * internal representation (i.e., reverse order).     */    public static String toReversedString (byte[] ba) {        int length = ba.length;        char[] buf = new char[length * 2];        for (int i = length-1, j = 0, k; i >=0; ) {            k = ba[i--];            buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];            buf[j++] = HEX_DIGITS[ k        & 0x0F];        }        return new String(buf);    }    /**     * Returns a string of hexadecimal digits from an integer array. Each     * int is converted to 4 hex symbols.     */    private static String toReversedString (int[] ia) {        int length = ia.length;        char[] buf = new char[length * 8];        for (int i = length-1, j = 0, k; i >= 0; i--) {            k = ia[i];            buf[j++] = HEX_DIGITS[(k >>> 28) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>> 24) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>> 20) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>> 16) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>> 12) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>>  8) & 0x0F];            buf[j++] = HEX_DIGITS[(k >>>  4) & 0x0F];            buf[j++] = HEX_DIGITS[ k         & 0x0F];        }        return new String(buf);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -