Exemple #1
0
        /// <summary>
        /// This method decrypts the specified data using the algorithm. This method should be used in checking the
        /// inputted password from the client, encrypted and passed in the authentication request packet.
        /// </summary>
        /// <param name="input">The inputted buffer to be decrypted.</param>
        public unsafe void Decrypt(byte[] input)
        {
            for (int index = 0; index < input.Length; index++)
            {
                // Is the algorithm done decrypting data?
                if (input[index] == 0)
                {
                    fixed(byte *ptr = input)
                    NativeFunctionCalls.memset(ptr + index, 0, input.Length - index);

                    return;
                }

                // Decrypt the byte at the current offset:
                byte position = _keyBuffer[input[index] * 2];
                if (position > 0x80)
                {
                    // Entered using the "SHIFT" key:
                    position     = (byte)(_keyBuffer[input[index] * 2] - 0x80);
                    input[index] = _decryptionSubstitutionBox[position];
                }
                else
                {
                    input[index] = _decryptionSubstitutionBox[position];
                    if (input[index] >= 0x41 && input[index] <= 90)
                    {
                        input[index] = (byte)(input[index] + 0x20);
                    }
                }
            }
        }
 /// <summary>
 /// Blowfish is a keyed, symmetric block cipher, designed in 1993 by Bruce Schneier and included in a large
 /// number of cipher suites and encryption products. Blowfish provides a good encryption rate in software and no
 /// effective cryptanalysis of it has been found to date. However, the Advanced Encryption Standard now receives
 /// more attention. Schneier designed Blowfish as a general-purpose algorithm, intended as an alternative to the
 /// aging DES and free of the problems and constraints associated with other algorithms. Schneier has stated that,
 /// "Blowfish is unpatented, and will remain so in all countries. The algorithm is hereby placed in the public
 /// domain, and can be freely used by anyone." The implementation type in use is CFB64.
 /// </summary>
 public BlowfishCipher()
 {
     _encryptionLock        = new object();
     _decryptionLock        = new object();
     _encryptionIncrementor = 0;
     _decryptionIncrementor = 0;
     _encryptionIV          = (byte *)NativeFunctionCalls.malloc(BF_BLOCK_SIZE);
     _decryptionIV          = (byte *)NativeFunctionCalls.malloc(BF_BLOCK_SIZE);
     NativeFunctionCalls.memset(_encryptionIV, 0, BF_BLOCK_SIZE);
     NativeFunctionCalls.memset(_decryptionIV, 0, BF_BLOCK_SIZE);
     KeySchedule(InitialKey);
 }