Esempio n. 1
0
    unsafe public void Blowfish_Encrypt(BLOWFISH_CTX ctx, ulong *xl, ulong *xr)
    {
        ulong Xl;
        ulong Xr;
        ulong temp;
        short i;

        Xl = *xl;
        Xr = *xr;

        for (i = 0; i < nValue; ++i)
        {
            Xl = Xl ^ ctx.P[i];
            Xr = F(ctx, Xl) ^ Xr;

            temp = Xl;
            Xl   = Xr;
            Xr   = temp;
        }

        temp = Xl;
        Xl   = Xr;
        Xr   = temp;

        Xr = Xr ^ ctx.P[nValue];
        Xl = Xl ^ ctx.P[nValue + 1];

        *xl = Xl;
        *xr = Xr;
    }
Esempio n. 2
0
    unsafe public void Blowfish_Decrypt(BLOWFISH_CTX ctx, ulong *xl, ulong *xr)
    {
        ulong Xl;
        ulong Xr;
        ulong temp;
        short i;

        Xl = *xl;
        Xr = *xr;

        for (i = nValue + 1; i > 1; --i)
        {
            Xl = Xl ^ ctx.P[i];
            Xr = F(ctx, Xl) ^ Xr;

            /* Exchange Xl and Xr */
            temp = Xl;
            Xl   = Xr;
            Xr   = temp;
        }

        /* Exchange Xl and Xr */
        temp = Xl;
        Xl   = Xr;
        Xr   = temp;

        Xr = Xr ^ ctx.P[1];
        Xl = Xl ^ ctx.P[0];

        *xl = Xl;
        *xr = Xr;
    }
Esempio n. 3
0
    unsafe public void Blowfish_Init(BLOWFISH_CTX ctx, string inKey, int keyLen)
    {
//				char* key = (char*)inKey;

        int   i, j, k;
        ulong data, datal, datar;

        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 256; j++)
            {
                ctx.S[i, j] = ORIG_S[i, j];
            }
        }

        j = 0;
        for (i = 0; i < nValue + 2; ++i)
        {
            data = 0x00000000;
            for (k = 0; k < 4; ++k)
            {
                data = (data << 8) | inKey[j];
                j    = j + 1;
                if (j >= keyLen)
                {
                    j = 0;
                }
            }
            ctx.P[i] = ORIG_P[i] ^ data;
        }

        datal = 0x00000000;
        datar = 0x00000000;

        for (i = 0; i < nValue + 2; i += 2)
        {
            Blowfish_Encrypt(ctx, &datal, &datar);
            ctx.P[i]     = datal;
            ctx.P[i + 1] = datar;
        }

        for (i = 0; i < 4; ++i)
        {
            for (j = 0; j < 256; j += 2)
            {
                Blowfish_Encrypt(ctx, &datal, &datar);
                ctx.S[i, j]     = datal;
                ctx.S[i, j + 1] = datar;
            }
        }
    }
Esempio n. 4
0
    unsafe static ulong F(BLOWFISH_CTX ctx, ulong x)
    {
        short a, b, c, d;
        ulong y;

        d   = (short)(x & 0xFF);
        x >>= 8;
        c   = (short)(x & 0xFF);
        x >>= 8;
        b   = (short)(x & 0xFF);
        x >>= 8;
        a   = (short)(x & 0xFF);
        y   = ctx.S[0, a] + ctx.S[1, b];
        y   = y ^ ctx.S[2, c];
        y   = y + ctx.S[3, d];

        return(y);
    }