Beispiel #1
0
            public void ShiftLeftWrapped(BLOCK8BYTE S, int iBitShift)
            {
                // this shift is only applied to the first 32 bits, and parity bit is ignored

                // Declaration of local variables
                int  iByteOffset = 0;
                bool bBit        = false;

                // Copy byte and shift regardless
                for (iByteOffset = 0; iByteOffset < 4; iByteOffset++)
                {
                    m_data[iByteOffset] = Convert.ToByte((S.m_data[iByteOffset] << iBitShift) & 0xFF);
                }

                // if shifting by 1...
                if (iBitShift == 1)
                {
                    // repair bits on right of BYTE
                    for (iByteOffset = 0; iByteOffset < 3; iByteOffset++)
                    {
                        // get repairing bit offsets
                        bBit = S.GetBit(iByteOffset + 1, 7);
                        this.SetBit(iByteOffset, 1, bBit);
                    }

                    // wrap around the final bit
                    this.SetBit(3, 1, S.GetBit(0, 7));
                }
                else if (iBitShift == 2)
                {
                    // repair bits on right of BYTE
                    for (iByteOffset = 0; iByteOffset < 3; iByteOffset++)
                    {
                        // get repairing bit offsets
                        bBit = S.GetBit(iByteOffset + 1, 7);
                        this.SetBit(iByteOffset, 2, bBit);
                        bBit = S.GetBit(iByteOffset + 1, 6);
                        this.SetBit(iByteOffset, 1, bBit);
                    }

                    // wrap around the final bit
                    this.SetBit(3, 2, S.GetBit(0, 7));
                    this.SetBit(3, 1, S.GetBit(0, 6));
                }
#if DEBUG
                else
                {
                    Debug.Assert(false);
                }
#endif // #if DEBUG
            }
Beispiel #2
0
            public void ShiftLeftWrapped(BLOCK8BYTE S, int iBitShift)
            {

                // this shift is only applied to the first 32 bits, and parity bit is ignored

                // Declaration of local variables
                int iByteOffset = 0;
                bool bBit = false;

                // Copy byte and shift regardless
                for (iByteOffset = 0; iByteOffset < 4; iByteOffset++)
                    m_data[iByteOffset] = Convert.ToByte((S.m_data[iByteOffset] << iBitShift) & 0xFF);

                // if shifting by 1...
                if (iBitShift == 1)
                {

                    // repair bits on right of BYTE
                    for (iByteOffset = 0; iByteOffset < 3; iByteOffset++)
                    {

                        // get repairing bit offsets
                        bBit = S.GetBit(iByteOffset + 1, 7);
                        this.SetBit(iByteOffset, 1, bBit);

                    }

                    // wrap around the final bit
                    this.SetBit(3, 1, S.GetBit(0, 7));

                }
                else if (iBitShift == 2)
                {

                    // repair bits on right of BYTE
                    for (iByteOffset = 0; iByteOffset < 3; iByteOffset++)
                    {

                        // get repairing bit offsets
                        bBit = S.GetBit(iByteOffset + 1, 7);
                        this.SetBit(iByteOffset, 2, bBit);
                        bBit = S.GetBit(iByteOffset + 1, 6);
                        this.SetBit(iByteOffset, 1, bBit);

                    }

                    // wrap around the final bit
                    this.SetBit(3, 2, S.GetBit(0, 7));
                    this.SetBit(3, 1, S.GetBit(0, 6));

                }
#if DEBUG
                else
                    Debug.Assert(false);
#endif // #if DEBUG

            }
Beispiel #3
0
        private static KEY_SET _expandKey(byte[] Key, int iOffset)
        {

            //
            // Expand an 8 byte DES key into a set of permuted keys
            //

            // Declare return variable
            KEY_SET Ftmp = new KEY_SET();

            // Declaration of local variables
            int iTableOffset, iArrayOffset, iPermOffset, iByteOffset, iBitOffset;
            bool bBit;

            // Put key into an 8-bit block
            BLOCK8BYTE K = new BLOCK8BYTE();
            K.Set(Key, iOffset);

            // Permutate Kp with PC1
            BLOCK8BYTE Kp = new BLOCK8BYTE();
            for (iArrayOffset = 0; iArrayOffset < bytePC1.Length; iArrayOffset++)
            {

                // Get permute offset
                iPermOffset = bytePC1[iArrayOffset];
                iPermOffset--;

                // Get and set bit
                Kp.SetBit(
                    _bitAddressToByteOffset(iArrayOffset, 7),
                    _bitAddressToBitOffset(iArrayOffset, 7),
                    K.GetBit(
                        _bitAddressToByteOffset(iPermOffset, 8),
                        _bitAddressToBitOffset(iPermOffset, 8)
                    )
                );

            }

            // Create 17 blocks of C and D from Kp
            BLOCK8BYTE[] KpCn = new BLOCK8BYTE[17];
            BLOCK8BYTE[] KpDn = new BLOCK8BYTE[17];
            for (iArrayOffset = 0; iArrayOffset < 17; iArrayOffset++)
            {
                KpCn[iArrayOffset] = new BLOCK8BYTE();
                KpDn[iArrayOffset] = new BLOCK8BYTE();
            }
            for (iArrayOffset = 0; iArrayOffset < 32; iArrayOffset++)
            {

                // Set bit in KpCn
                iByteOffset = _bitAddressToByteOffset(iArrayOffset, 8);
                iBitOffset = _bitAddressToBitOffset(iArrayOffset, 8);
                bBit = Kp.GetBit(iByteOffset, iBitOffset);
                KpCn[0].SetBit(iByteOffset, iBitOffset, bBit);

                // Set bit in KpDn
                bBit = Kp.GetBit(iByteOffset + 4, iBitOffset);
                KpDn[0].SetBit(iByteOffset, iBitOffset, bBit);

            }
            for (iArrayOffset = 1; iArrayOffset < 17; iArrayOffset++)
            {

                // Shift left wrapped
                KpCn[iArrayOffset].ShiftLeftWrapped(KpCn[iArrayOffset - 1], byteShifts[iArrayOffset - 1]);
                KpDn[iArrayOffset].ShiftLeftWrapped(KpDn[iArrayOffset - 1], byteShifts[iArrayOffset - 1]);

            }

            // Create 17 keys Kn
            for (iArrayOffset = 0; iArrayOffset < 17; iArrayOffset++)
            {

                // Loop through the bits
                for (iTableOffset = 0; iTableOffset < 48; iTableOffset++)
                {

                    // Get address if bit
                    iPermOffset = bytePC2[iTableOffset];
                    iPermOffset--;

                    // Convert to byte and bit offsets
                    iByteOffset = _bitAddressToByteOffset(iPermOffset, 7);
                    iBitOffset = _bitAddressToBitOffset(iPermOffset, 7);

                    // Get bit
                    if (iByteOffset < 4)
                        bBit = KpCn[iArrayOffset].GetBit(iByteOffset, iBitOffset);
                    else
                        bBit = KpDn[iArrayOffset].GetBit(iByteOffset - 4, iBitOffset);

                    // Set bit
                    iByteOffset = _bitAddressToByteOffset(iTableOffset, 6);
                    iBitOffset = _bitAddressToBitOffset(iTableOffset, 6);
                    Ftmp.GetAt(iArrayOffset).SetBit(iByteOffset, iBitOffset, bBit);

                }

            }

            // Return variable
            return Ftmp;

        }
Beispiel #4
0
        private static KEY_SET _expandKey(byte[] Key, int iOffset)
        {
            //
            // Expand an 8 byte DES key into a set of permuted keys
            //

            // Declare return variable
            KEY_SET Ftmp = new KEY_SET();

            // Declaration of local variables
            int  iTableOffset, iArrayOffset, iPermOffset, iByteOffset, iBitOffset;
            bool bBit;

            // Put key into an 8-bit block
            BLOCK8BYTE K = new BLOCK8BYTE();

            K.Set(Key, iOffset);

            // Permutate Kp with PC1
            BLOCK8BYTE Kp = new BLOCK8BYTE();

            for (iArrayOffset = 0; iArrayOffset < bytePC1.Length; iArrayOffset++)
            {
                // Get permute offset
                iPermOffset = bytePC1[iArrayOffset];
                iPermOffset--;

                // Get and set bit
                Kp.SetBit(
                    _bitAddressToByteOffset(iArrayOffset, 7),
                    _bitAddressToBitOffset(iArrayOffset, 7),
                    K.GetBit(
                        _bitAddressToByteOffset(iPermOffset, 8),
                        _bitAddressToBitOffset(iPermOffset, 8)
                        )
                    );
            }

            // Create 17 blocks of C and D from Kp
            BLOCK8BYTE[] KpCn = new BLOCK8BYTE[17];
            BLOCK8BYTE[] KpDn = new BLOCK8BYTE[17];
            for (iArrayOffset = 0; iArrayOffset < 17; iArrayOffset++)
            {
                KpCn[iArrayOffset] = new BLOCK8BYTE();
                KpDn[iArrayOffset] = new BLOCK8BYTE();
            }
            for (iArrayOffset = 0; iArrayOffset < 32; iArrayOffset++)
            {
                // Set bit in KpCn
                iByteOffset = _bitAddressToByteOffset(iArrayOffset, 8);
                iBitOffset  = _bitAddressToBitOffset(iArrayOffset, 8);
                bBit        = Kp.GetBit(iByteOffset, iBitOffset);
                KpCn[0].SetBit(iByteOffset, iBitOffset, bBit);

                // Set bit in KpDn
                bBit = Kp.GetBit(iByteOffset + 4, iBitOffset);
                KpDn[0].SetBit(iByteOffset, iBitOffset, bBit);
            }
            for (iArrayOffset = 1; iArrayOffset < 17; iArrayOffset++)
            {
                // Shift left wrapped
                KpCn[iArrayOffset].ShiftLeftWrapped(KpCn[iArrayOffset - 1], byteShifts[iArrayOffset - 1]);
                KpDn[iArrayOffset].ShiftLeftWrapped(KpDn[iArrayOffset - 1], byteShifts[iArrayOffset - 1]);
            }

            // Create 17 keys Kn
            for (iArrayOffset = 0; iArrayOffset < 17; iArrayOffset++)
            {
                // Loop through the bits
                for (iTableOffset = 0; iTableOffset < 48; iTableOffset++)
                {
                    // Get address if bit
                    iPermOffset = bytePC2[iTableOffset];
                    iPermOffset--;

                    // Convert to byte and bit offsets
                    iByteOffset = _bitAddressToByteOffset(iPermOffset, 7);
                    iBitOffset  = _bitAddressToBitOffset(iPermOffset, 7);

                    // Get bit
                    if (iByteOffset < 4)
                    {
                        bBit = KpCn[iArrayOffset].GetBit(iByteOffset, iBitOffset);
                    }
                    else
                    {
                        bBit = KpDn[iArrayOffset].GetBit(iByteOffset - 4, iBitOffset);
                    }

                    // Set bit
                    iByteOffset = _bitAddressToByteOffset(iTableOffset, 6);
                    iBitOffset  = _bitAddressToBitOffset(iTableOffset, 6);
                    Ftmp.GetAt(iArrayOffset).SetBit(iByteOffset, iBitOffset, bBit);
                }
            }

            // Return variable
            return(Ftmp);
        }