blowfish.java

来自「jpeg2000编解码」· Java 代码 · 共 1,363 行 · 第 1/5 页

JAVA
1,363
字号
            0x50115E01, 0xA70683FA, 0xA002B5C4, 0x0DE6D027,            0x9AF88C27, 0x773F8641, 0xC3604C06, 0x61A806B5,            0xF0177A28, 0xC0F586E0, 0x006058AA, 0x30DC7D62,            0x11E69ED7, 0x2338EA63, 0x53C2DD94, 0xC2C21634,            0xBBCBEE56, 0x90BCB6DE, 0xEBFC7DA1, 0xCE591D76,            0x6F05E409, 0x4B7C0188, 0x39720A3D, 0x7C927C24,            0x86E3725F, 0x724D9DB9, 0x1AC15BB4, 0xD39EB8FC,            0xED545578, 0x08FCA5B5, 0xD83D7CD3, 0x4DAD0FC4,            0x1E50EF5E, 0xB161E6F8, 0xA28514D9, 0x6C51133C,            0x6FD5C7E7, 0x56E14EC4, 0x362ABFCE, 0xDDC6C837,            0xD79A3234, 0x92638212, 0x670EFA8E, 0x406000E0},        S3 = {            0x3A39CE37, 0xD3FAF5CF, 0xABC27737, 0x5AC52D1B,            0x5CB0679E, 0x4FA33742, 0xD3822740, 0x99BC9BBE,            0xD5118E9D, 0xBF0F7315, 0xD62D1C7E, 0xC700C47B,            0xB78C1B6B, 0x21A19045, 0xB26EB1BE, 0x6A366EB4,            0x5748AB2F, 0xBC946E79, 0xC6A376D2, 0x6549C2C8,            0x530FF8EE, 0x468DDE7D, 0xD5730A1D, 0x4CD04DC6,            0x2939BBDB, 0xA9BA4650, 0xAC9526E8, 0xBE5EE304,            0xA1FAD5F0, 0x6A2D519A, 0x63EF8CE2, 0x9A86EE22,            0xC089C2B8, 0x43242EF6, 0xA51E03AA, 0x9CF2D0A4,            0x83C061BA, 0x9BE96A4D, 0x8FE51550, 0xBA645BD6,            0x2826A2F9, 0xA73A3AE1, 0x4BA99586, 0xEF5562E9,            0xC72FEFD3, 0xF752F7DA, 0x3F046F69, 0x77FA0A59,            0x80E4A915, 0x87B08601, 0x9B09E6AD, 0x3B3EE593,            0xE990FD5A, 0x9E34D797, 0x2CF0B7D9, 0x022B8B51,            0x96D5AC3A, 0x017DA67D, 0xD1CF3ED6, 0x7C7D2D28,            0x1F9F25CF, 0xADF2B89B, 0x5AD6B472, 0x5A88F54C,            0xE029AC71, 0xE019A5E6, 0x47B0ACFD, 0xED93FA9B,            0xE8D3C48D, 0x283B57CC, 0xF8D56629, 0x79132E28,            0x785F0191, 0xED756055, 0xF7960E44, 0xE3D35E8C,            0x15056DD4, 0x88F46DBA, 0x03A16125, 0x0564F0BD,            0xC3EB9E15, 0x3C9057A2, 0x97271AEC, 0xA93A072A,            0x1B3F6D9B, 0x1E6321F5, 0xF59C66FB, 0x26DCF319,            0x7533D928, 0xB155FDF5, 0x03563482, 0x8ABA3CBB,            0x28517711, 0xC20AD9F8, 0xABCC5167, 0xCCAD925F,            0x4DE81751, 0x3830DC8E, 0x379D5862, 0x9320F991,            0xEA7A90C2, 0xFB3E7BCE, 0x5121CE64, 0x774FBE32,            0xA8B6E37E, 0xC3293D46, 0x48DE5369, 0x6413E680,            0xA2AE0810, 0xDD6DB224, 0x69852DFD, 0x09072166,            0xB39A460A, 0x6445C0DD, 0x586CDECF, 0x1C20C8AE,            0x5BBEF7DD, 0x1B588D40, 0xCCD2017F, 0x6BB4E3BB,            0xDDA26A7E, 0x3A59FF45, 0x3E350A44, 0xBCB4CDD5,            0x72EACEA8, 0xFA6484BB, 0x8D6612AE, 0xBF3C6F47,            0xD29BE463, 0x542F5D9E, 0xAEC2771B, 0xF64E6370,            0x740E0D8D, 0xE75B1357, 0xF8721671, 0xAF537D5D,            0x4040CB08, 0x4EB4E2CC, 0x34D2466A, 0x0115AF84,            0xE1B00428, 0x95983A1D, 0x06B89FB4, 0xCE6EA048,            0x6F3F3B82, 0x3520AB82, 0x011A1D4B, 0x277227F8,            0x611560B1, 0xE7933FDC, 0xBB3A792B, 0x344525BD,            0xA08839E1, 0x51CE794B, 0x2F32C9B7, 0xA01FBAC9,            0xE01CC87E, 0xBCC7D1F6, 0xCF0111C3, 0xA1E8AAC7,            0x1A908749, 0xD44FBD9A, 0xD0DADECB, 0xD50ADA38,            0x0339C32A, 0xC6913667, 0x8DF9317C, 0xE0B12B4F,            0xF79E59B7, 0x43F5BB3A, 0xF2D519FF, 0x27D9459C,            0xBF97222C, 0x15E6FC2A, 0x0F91FC71, 0x9B941525,            0xFAE59361, 0xCEB69CEB, 0xC2A86459, 0x12BAA8D1,            0xB6C1075E, 0xE3056A0C, 0x10D25065, 0xCB03A442,            0xE0EC6E0E, 0x1698DB3B, 0x4C98A0BE, 0x3278E964,            0x9F1F9532, 0xE0D392DF, 0xD3A0342B, 0x8971F21E,            0x1B0A7441, 0x4BA3348C, 0xC5BE7120, 0xC37632D8,            0xDF359F8D, 0x9B992F2E, 0xE60B6F47, 0x0FE3F11D,            0xE54CDA54, 0x1EDAD891, 0xCE6279CF, 0xCD3E7E6F,            0x1618B166, 0xFD2C1D05, 0x848FD2C5, 0xF6FB2299,            0xF523F357, 0xA6327623, 0x93A83531, 0x56CCCD02,            0xACF08162, 0x5A75EBB5, 0x6E163697, 0x88D273CC,            0xDE966292, 0x81B949D0, 0x4C50901B, 0x71C65614,            0xE6C6C7BD, 0x327A140A, 0x45E1D006, 0xC3F27B9A,            0xC9AA53FD, 0x62A80F00, 0xBB25BFE2, 0x35BDD2F6,            0x71126905, 0xB2040222, 0xB6CBCF7C, 0xCD769C2B,            0x53113EC0, 0x1640E3D3, 0x38ABBD60, 0x2547ADF0,            0xBA38209C, 0xF746CE76, 0x77AFA1C5, 0x20756060,            0x85CBFE4E, 0x8AE88DD8, 0x7AAAF9B0, 0x4CF9AA7E,            0x1948C25C, 0x02FB8A8C, 0x01C36AE4, 0xD6EBE1F9,            0x90D4F869, 0xA65CDEA0, 0x3F09252D, 0xC208E69F,            0xB74E6132, 0xCE77E25B, 0x578FDFE3, 0x3AC372E6};    private static final int        DEFAULT_NOF_ROUNDS = 16,        // default value        MIN_NOF_ROUNDS = 16,            // this is also the default value        MAX_NOF_ROUNDS = 16,            // should be 20, but we don't have enough P0 data        BLOCK_SIZE = 8,                 // Blowfish block size in bytes        MIN_USER_KEY_LENGTH =  40 / 8,  // given in bytes from a value in bits        MAX_USER_KEY_LENGTH = 448 / 8;  // given in bytes from a value in bits    /**     * The current number of rounds. May get changed if a 'rounds'     * property is set in the security provider properties file.     * Defaults to DEFAULT_NOF_ROUNDS; ie. 16.     */    private int rounds = DEFAULT_NOF_ROUNDS;    /**     * This Blowfish instance's P-array placeholder. Actual size depends     * on the maximum number of rounds.     */    private int[] P = new int[MAX_NOF_ROUNDS + 2];    /**     * Individual ints representing the session key. Based on 16 rounds     * Blowfish; ie. the current supported number of rounds which is also     * the default value.     */    private int        K0, K1,  K2,  K3,  K4,  K5,  K6,  K7,  K8,        K9, K10, K11, K12, K13, K14, K15, K16, K17;        /** This instance's S-boxes data. */    private int[] S = new int[1024];// Static code//...........................................................................    /** Set to true iff CRC passes for initial S and P0 values. */    private static final boolean OK;    // blank final    static {        // compute OK        int crc = 0;        int i;        //        // Step 1.        // Walk through initial values P0.        //        for (i = 0; i < MAX_NOF_ROUNDS + 2; i++)            crc = (crc << 1 | crc >>> 31) + P0[i];        //        // ...and the Si (s-boxes).        //        for (i = 0; i < 256; i++) {            crc *= 13;            crc = (crc << 11 | crc >>> 21) + S0[i];        }        for (i = 0; i < 256; i++) {            crc *= 13;            crc = (crc << 11 | crc >>> 21) + S1[i];        }        for (i = 0; i < 256; i++) {            crc *= 13;            crc = (crc << 11 | crc >>> 21) + S2[i];        }        for (i = 0; i < 256; i++) {            crc *= 13;            crc = (crc << 11 | crc >>> 21) + S3[i];        }if (DEBUG && debuglevel >= 4) debug("crc = " + Hex.intToString(crc));        //        // This number is a check for the S and P boxes.        //        OK = (crc == 0x55861A61);if (DEBUG && debuglevel >= 3) debug("Good CRC on initial P and S boxes? " + OK);    }// Constructor, finalizer, and clone()//...........................................................................    /**     * Constructs a Blowfish cipher object, in the UNINITIALIZED state.     * This calls the Cipher constructor with <i>implBuffering</i> false,     * <i>implPadding</i> false and the provider set to "Cryptix".     * <p>     * We define a "rounds" property to allow running the algorithm in a number     * of rounds different than the default value; i.e. 16.     * Note however that there is not enough defined initial data for any     * number of rounds other than 16 for the time being.     *     * @exception CryptixException if a CRC check fails on the initial     *            values of the S and P boxes.     */    public Blowfish() {        super(false, false, "Cryptix");                if (! OK) throw new CryptixException(            getAlgorithm() + ": CRC failed on initial data");        link();        try {            String ps = Security.getAlgorithmProperty("Blowfish", "rounds");            if (ps != null) setRounds(Integer.parseInt(ps));        } catch (Exception e) {if (DEBUG && debuglevel > 0) debug("Could not set number of rounds");        }    }    /** Cleans up resources used by this instance, if necessary. */    protected final void finalize() {        if (native_lock != null) {            synchronized(native_lock) {                String error = native_finalize(); // may be called more than once                if (error != null)                    debug(error + " in native_finalize");            }        }    }    /**     * Always throws a CloneNotSupportedException (cloning of ciphers is not     * supported for security reasons).     */    public final Object clone() throws CloneNotSupportedException {        throw new CloneNotSupportedException();    }// Implementation of JCE methods//...........................................................................        /**     * <b>SPI</b>: Returns the length of an input block, in bytes.     *     * @return the length in bytes of an input block for this cipher.     */    protected int engineBlockSize() { return BLOCK_SIZE; }    /**     * <b>SPI</b>: Initializes this cipher for encryption, using the     * specified key.     *     * @param  key  the key to use for encryption.     * @exception InvalidKeyException when one of the following occurs: <ul>     *                <li> key.getEncoded() == null;     *                <li> The encoded byte array form of the key is zero-length;     *                <li> The length of the user key data array is out of the     *                permissible limits.     *              </ul>     * @exception CryptixException if any one of the two self-tests fail.     *              The two self-tests are as follows: <ol>     *                <li> encrypt 0 ten times using the initial S and P boxes     *                values. Check the result against a known value. If equal     *                decrypt the result 10 times and compare it to 0;     *                <li> complete the key expansion process and use the newly     *                formed key to encrypt 10 times 0; decrypt the result 10     *                times and compare.     *              </ol>     */    protected void engineInitEncrypt(Key key)    throws InvalidKeyException, CryptixException {        makeKey(key);    }    /**     * <b>SPI</b>: Initializes this cipher for decryption, using the     * specified key.     *     * @param  key  the key to use for decryption.     * @exception InvalidKeyException when one of the following occurs: <ul>     *                <li> key.getEncoded() == null;     *                <li> The encoded byte array form of the key is zero-length;     *                <li> The length of the user key data array is out of the     *                permissible limits.     *              </ul>     * @exception CryptixException if any one of the two self-tests fail.     *              The two self-tests are as follows: <ol>     *                <li> encrypt 0 ten times using the initial S and P boxes     *                values. Check the result against a known value. If equal     *                decrypt the result 10 times and compare it to 0;     *                <li> complete the key expansion process and use the newly     *                formed key to encrypt 10 times 0; decrypt the result 10     *                times and compare.     *              </ol>     */    protected void engineInitDecrypt (Key key)    throws InvalidKeyException, CryptixException {        makeKey(key);    }

⌨️ 快捷键说明

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