Renci.SshNet.Security.Cryptography.Ciphers.DesCipher.DesFunc C# (CSharp) Method

DesFunc() protected static method

Performs DES function.
protected static DesFunc ( int wKey, byte input, int inOff, byte outBytes, int outOff ) : void
wKey int The w key.
input byte The input.
inOff int The in off.
outBytes byte The out bytes.
outOff int The out off.
return void
        protected static void DesFunc(int[] wKey, byte[] input, int inOff, byte[] outBytes, int outOff)
        {
            uint left = BigEndianToUInt32(input, inOff);
            uint right = BigEndianToUInt32(input, inOff + 4);

            var work = ((left >> 4) ^ right) & 0x0f0f0f0f;
            right ^= work;
            left ^= (work << 4);
            work = ((left >> 16) ^ right) & 0x0000ffff;
            right ^= work;
            left ^= (work << 16);
            work = ((right >> 2) ^ left) & 0x33333333;
            left ^= work;
            right ^= (work << 2);
            work = ((right >> 8) ^ left) & 0x00ff00ff;
            left ^= work;
            right ^= (work << 8);
            right = (right << 1) | (right >> 31);
            work = (left ^ right) & 0xaaaaaaaa;
            left ^= work;
            right ^= work;
            left = (left << 1) | (left >> 31);

            for (int round = 0; round < 8; round++)
            {
                work = (right << 28) | (right >> 4);
                work ^= (uint)wKey[round * 4 + 0];
                var fval = SP7[work & 0x3f];
                fval |= SP5[(work >> 8) & 0x3f];
                fval |= SP3[(work >> 16) & 0x3f];
                fval |= SP1[(work >> 24) & 0x3f];
                work = right ^ (uint)wKey[round * 4 + 1];
                fval |= SP8[work & 0x3f];
                fval |= SP6[(work >> 8) & 0x3f];
                fval |= SP4[(work >> 16) & 0x3f];
                fval |= SP2[(work >> 24) & 0x3f];
                left ^= fval;
                work = (left << 28) | (left >> 4);
                work ^= (uint)wKey[round * 4 + 2];
                fval = SP7[work & 0x3f];
                fval |= SP5[(work >> 8) & 0x3f];
                fval |= SP3[(work >> 16) & 0x3f];
                fval |= SP1[(work >> 24) & 0x3f];
                work = left ^ (uint)wKey[round * 4 + 3];
                fval |= SP8[work & 0x3f];
                fval |= SP6[(work >> 8) & 0x3f];
                fval |= SP4[(work >> 16) & 0x3f];
                fval |= SP2[(work >> 24) & 0x3f];
                right ^= fval;
            }

            right = (right << 31) | (right >> 1);
            work = (left ^ right) & 0xaaaaaaaa;
            left ^= work;
            right ^= work;
            left = (left << 31) | (left >> 1);
            work = ((left >> 8) ^ right) & 0x00ff00ff;
            right ^= work;
            left ^= (work << 8);
            work = ((left >> 2) ^ right) & 0x33333333;
            right ^= work;
            left ^= (work << 2);
            work = ((right >> 16) ^ left) & 0x0000ffff;
            left ^= work;
            right ^= (work << 16);
            work = ((right >> 4) ^ left) & 0x0f0f0f0f;
            left ^= work;
            right ^= (work << 4);

            UInt32ToBigEndian(right, outBytes, outOff);
            UInt32ToBigEndian(left, outBytes, outOff + 4);
        }
    }

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region of the output byte array.
        /// </summary>
        /// <param name="inputBuffer">The input data to encrypt.</param>
        /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
        /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
        /// <param name="outputBuffer">The output to which to write encrypted data.</param>
        /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
        /// <returns>
        /// The number of bytes encrypted.
        /// </returns>
        public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
        {
            if ((inputOffset + this.BlockSize) > inputBuffer.Length)
            {
                throw new IndexOutOfRangeException("input buffer too short");
            }

            if ((outputOffset + this.BlockSize) > outputBuffer.Length)
            {
                throw new IndexOutOfRangeException("output buffer too short");
            }

            if (this._encryptionKey1 == null || this._encryptionKey2 == null || this._encryptionKey3 == null)
            {
                var part1 = new byte[8];
                var part2 = new byte[8];

                Buffer.BlockCopy(this.Key, 0, part1, 0, 8);
                Buffer.BlockCopy(this.Key, 8, part2, 0, 8);

                this._encryptionKey1 = this.GenerateWorkingKey(true, part1);

                this._encryptionKey2 = this.GenerateWorkingKey(false, part2);

                if (this.Key.Length == 24)
                {
                    var part3 = new byte[8];
                    Buffer.BlockCopy(this.Key, 16, part3, 0, 8);

                    this._encryptionKey3 = this.GenerateWorkingKey(true, part3);
                }
                else
                {
                    this._encryptionKey3 = this._encryptionKey1;
                }
            }

            byte[] temp = new byte[this.BlockSize];

            DesCipher.DesFunc(this._encryptionKey1, inputBuffer, inputOffset, temp, 0);
            DesCipher.DesFunc(this._encryptionKey2, temp, 0, temp, 0);
            DesCipher.DesFunc(this._encryptionKey3, temp, 0, outputBuffer, outputOffset);

            return(this.BlockSize);
        }
All Usage Examples Of Renci.SshNet.Security.Cryptography.Ciphers.DesCipher::DesFunc