Exemplo n.º 1
0
        // Дешифрование одного блока
        private ulong Decrypt(ulong textBit64, uint[] roundsKeys)
        {
            ulong[] L = new ulong[18];
            ulong[] R = new ulong[18];

            // тут наоборот в отличии от Encrypt
            L[17] = (textBit64 >> 32);
            R[17] = WorkWithBits.GetLowBits(textBit64, 32);

            R[16] = R[17] ^ roundsKeys[16];
            L[16] = L[17] ^ roundsKeys[17];

            uint tmp = (uint)R[16];

            R[16] = L[16];
            L[16] = tmp;

            // тут тоже наоборот
            for (int i = 15; i >= 0; i--)
            {
                R[i] = L[i + 1] ^ F_Transformation((uint)(roundsKeys[i] ^ R[i + 1]));
                L[i] = R[i + 1];
            }

            ulong answer = (L[0] << 32) | R[0];

            return(answer);
        }
Exemplo n.º 2
0
        // Шифрование одного блока
        private ulong Encrypt(ulong textBit64, uint[] roundsKeys)
        {
            // правая-левая части
            ulong[] L = new ulong[18];
            ulong[] R = new ulong[18];

            L[0] = (textBit64 >> 32);
            R[0] = WorkWithBits.GetLowBits(textBit64, 32);

            // цикл раундов
            for (int i = 0; i < 16; i++)
            {
                L[i + 1] = R[i] ^ F_Transformation((uint)(roundsKeys[i] ^ L[i]));
                R[i + 1] = L[i];
            }

            uint tmp = (uint)R[16];

            R[16] = L[16];
            L[16] = tmp;

            R[17] = R[16] ^ roundsKeys[16];
            L[17] = L[16] ^ roundsKeys[17];

            // склеиваем половинки
            ulong answer = (L[17] << 32) | R[17];

            return(answer);
        }
Exemplo n.º 3
0
        // F преобразование
        private uint F_Transformation(uint block)
        {
            uint answer = BlowfishSumWithMod(_Sblocks[0][WorkWithBits.CutBitsWithBeginningAndEndingPlaces(block, 0, 7)],
                                             _Sblocks[1][WorkWithBits.CutBitsWithBeginningAndEndingPlaces(block, 8, 15)]);

            answer = answer ^ (uint)_Sblocks[2][WorkWithBits.CutBitsWithBeginningAndEndingPlaces(block, 16, 23)];
            answer = BlowfishSumWithMod(answer, _Sblocks[3][WorkWithBits.CutBitsWithBeginningAndEndingPlaces(block, 24, 31)]);

            return(answer);
        }