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

📄 cast5engine.java

📁 进行与数字证书相关开发必须的java源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        x03 = IntsTo32bits(x, 0);
        x47 = IntsTo32bits(x, 4);
        x8B = IntsTo32bits(x, 8);
        xCF = IntsTo32bits(x, 12);
        z03 = x03 ^ S5[x[13]] ^ S6[x[15]] ^ S7[x[12]] ^ S8[x[14]] ^ S7[x[8]];
        Bits32ToInts(z03, z, 0);
        z47 = x8B ^ S5[z[0]] ^ S6[z[2]] ^ S7[z[1]] ^ S8[z[3]] ^ S8[x[10]];
        Bits32ToInts(z47, z, 4);
        z8B = xCF ^ S5[z[7]] ^ S6[z[6]] ^ S7[z[5]] ^ S8[z[4]] ^ S5[x[9]];
        Bits32ToInts(z8B, z, 8);
        zCF = x47 ^ S5[z[10]] ^ S6[z[9]] ^ S7[z[11]] ^ S8[z[8]] ^ S6[x[11]];
        Bits32ToInts(zCF, z, 12);
        _Kr[9] = (S5[z[3]] ^ S6[z[2]] ^ S7[z[12]] ^ S8[z[13]] ^ S5[z[9]]) & 0x1f;
        _Kr[10] = (S5[z[1]] ^ S6[z[0]] ^ S7[z[14]] ^ S8[z[15]] ^ S6[z[12]]) & 0x1f;
        _Kr[11] = (S5[z[7]] ^ S6[z[6]] ^ S7[z[8]] ^ S8[z[9]] ^ S7[z[2]]) & 0x1f;
        _Kr[12] = (S5[z[5]] ^ S6[z[4]] ^ S7[z[10]] ^ S8[z[11]] ^ S8[z[6]]) & 0x1f;
        z03 = IntsTo32bits(z, 0);
        z47 = IntsTo32bits(z, 4);
        z8B = IntsTo32bits(z, 8);
        zCF = IntsTo32bits(z, 12);
        x03 = z8B ^ S5[z[5]] ^ S6[z[7]] ^ S7[z[4]] ^ S8[z[6]] ^ S7[z[0]];
        Bits32ToInts(x03, x, 0);
        x47 = z03 ^ S5[x[0]] ^ S6[x[2]] ^ S7[x[1]] ^ S8[x[3]] ^ S8[z[2]];
        Bits32ToInts(x47, x, 4);
        x8B = z47 ^ S5[x[7]] ^ S6[x[6]] ^ S7[x[5]] ^ S8[x[4]] ^ S5[z[1]];
        Bits32ToInts(x8B, x, 8);
        xCF = zCF ^ S5[x[10]] ^ S6[x[9]] ^ S7[x[11]] ^ S8[x[8]] ^ S6[z[3]];
        Bits32ToInts(xCF, x, 12);
        _Kr[13] = (S5[x[8]] ^ S6[x[9]] ^ S7[x[7]] ^ S8[x[6]] ^ S5[x[3]]) & 0x1f;
        _Kr[14] = (S5[x[10]] ^ S6[x[11]] ^ S7[x[5]] ^ S8[x[4]] ^ S6[x[7]]) & 0x1f;
        _Kr[15] = (S5[x[12]] ^ S6[x[13]] ^ S7[x[3]] ^ S8[x[2]] ^ S7[x[8]]) & 0x1f;
        _Kr[16] = (S5[x[14]] ^ S6[x[15]] ^ S7[x[1]] ^ S8[x[0]] ^ S8[x[13]]) & 0x1f;
    }

    protected int encryptBlock(byte src[], int srcIndex, byte dst[], int dstIndex)
    {
        int result[] = new int[2];
        int L0 = BytesTo32bits(src, srcIndex);
        int R0 = BytesTo32bits(src, srcIndex + 4);
        CAST_Encipher(L0, R0, result);
        Bits32ToBytes(result[0], dst, dstIndex);
        Bits32ToBytes(result[1], dst, dstIndex + 4);
        return 8;
    }

    protected int decryptBlock(byte src[], int srcIndex, byte dst[], int dstIndex)
    {
        int result[] = new int[2];
        int L16 = BytesTo32bits(src, srcIndex);
        int R16 = BytesTo32bits(src, srcIndex + 4);
        CAST_Decipher(L16, R16, result);
        Bits32ToBytes(result[0], dst, dstIndex);
        Bits32ToBytes(result[1], dst, dstIndex + 4);
        return 8;
    }

    protected final int F1(int D, int Kmi, int Kri)
    {
        int I = Kmi + D;
        I = I << Kri | I >>> 32 - Kri;
        return ((S1[I >>> 24 & 0xff] ^ S2[I >>> 16 & 0xff]) - S3[I >>> 8 & 0xff]) + S4[I & 0xff];
    }

    protected final int F2(int D, int Kmi, int Kri)
    {
        int I = Kmi ^ D;
        I = I << Kri | I >>> 32 - Kri;
        return (S1[I >>> 24 & 0xff] - S2[I >>> 16 & 0xff]) + S3[I >>> 8 & 0xff] ^ S4[I & 0xff];
    }

    protected final int F3(int D, int Kmi, int Kri)
    {
        int I = Kmi - D;
        I = I << Kri | I >>> 32 - Kri;
        return (S1[I >>> 24 & 0xff] + S2[I >>> 16 & 0xff] ^ S3[I >>> 8 & 0xff]) - S4[I & 0xff];
    }

    protected final void CAST_Encipher(int L0, int R0, int result[])
    {
        int Lp = L0;
        int Rp = R0;
        int Li = L0;
        int Ri = R0;
        for(int i = 1; i <= _rounds; i++)
        {
            Lp = Li;
            Rp = Ri;
            Li = Rp;
            switch(i)
            {
            case 1: // '\001'
            case 4: // '\004'
            case 7: // '\007'
            case 10: // '\n'
            case 13: // '\r'
            case 16: // '\020'
                Ri = Lp ^ F1(Rp, _Km[i], _Kr[i]);
                break;

            case 2: // '\002'
            case 5: // '\005'
            case 8: // '\b'
            case 11: // '\013'
            case 14: // '\016'
                Ri = Lp ^ F2(Rp, _Km[i], _Kr[i]);
                break;

            case 3: // '\003'
            case 6: // '\006'
            case 9: // '\t'
            case 12: // '\f'
            case 15: // '\017'
                Ri = Lp ^ F3(Rp, _Km[i], _Kr[i]);
                break;
            }
        }

        result[0] = Ri;
        result[1] = Li;
    }

    protected final void CAST_Decipher(int L16, int R16, int result[])
    {
        int Lp = L16;
        int Rp = R16;
        int Li = L16;
        int Ri = R16;
        for(int i = _rounds; i > 0; i--)
        {
            Lp = Li;
            Rp = Ri;
            Li = Rp;
            switch(i)
            {
            case 1: // '\001'
            case 4: // '\004'
            case 7: // '\007'
            case 10: // '\n'
            case 13: // '\r'
            case 16: // '\020'
                Ri = Lp ^ F1(Rp, _Km[i], _Kr[i]);
                break;

            case 2: // '\002'
            case 5: // '\005'
            case 8: // '\b'
            case 11: // '\013'
            case 14: // '\016'
                Ri = Lp ^ F2(Rp, _Km[i], _Kr[i]);
                break;

            case 3: // '\003'
            case 6: // '\006'
            case 9: // '\t'
            case 12: // '\f'
            case 15: // '\017'
                Ri = Lp ^ F3(Rp, _Km[i], _Kr[i]);
                break;
            }
        }

        result[0] = Ri;
        result[1] = Li;
    }

    protected final void Bits32ToInts(int in, int b[], int offset)
    {
        b[offset + 3] = in & 0xff;
        b[offset + 2] = in >>> 8 & 0xff;
        b[offset + 1] = in >>> 16 & 0xff;
        b[offset] = in >>> 24 & 0xff;
    }

    protected final int IntsTo32bits(int b[], int i)
    {
        int rv = 0;
        rv = (b[i] & 0xff) << 24 | (b[i + 1] & 0xff) << 16 | (b[i + 2] & 0xff) << 8 | b[i + 3] & 0xff;
        return rv;
    }

    protected final void Bits32ToBytes(int in, byte b[], int offset)
    {
        b[offset + 3] = (byte)in;
        b[offset + 2] = (byte)(in >>> 8);
        b[offset + 1] = (byte)(in >>> 16);
        b[offset] = (byte)(in >>> 24);
    }

    protected final int BytesTo32bits(byte b[], int i)
    {
        return (b[i] & 0xff) << 24 | (b[i + 1] & 0xff) << 16 | (b[i + 2] & 0xff) << 8 | b[i + 3] & 0xff;
    }

    static 
    {
        M32 = -1;
        MAX_ROUNDS = 16;
        RED_ROUNDS = 12;
        BLOCK_SIZE = 8;
    }
}

⌨️ 快捷键说明

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