Пример #1
0
        /// <see cref="System.Security.Cryptography.SymmetricAlgorithm.CreateDecryptor(byte[], byte[])"/>
        public override ICryptoTransform CreateDecryptor(byte[] key, byte[] iv)
        {
            BlowfishAlgorithm result = new BlowfishAlgorithm(
                key,
                iv,
                (CipherMode.CBC == ModeValue),
                false);

            CopyPadding(result);
            return(result);
        }
Пример #2
0
        void CopyPadding(BlowfishAlgorithm ba)
        {
            switch (Padding)
            {
            case PaddingMode.ANSIX923:
            case PaddingMode.ISO10126:
            case PaddingMode.PKCS7:
            case PaddingMode.Zeros:
            {
                ba.Padding = Padding;
                break;
            }

            default:
            {
                throw new CryptographicException();
            }
            }
        }
Пример #3
0
        /// <summary>Creates a new Blowfish stream.</summary>
        /// <param name="stm">The stream to read or write to.</param>
        /// <param name="mode">Operation mode</param>
        /// <param name="key">The buffer with the key material.</param>
        /// <param name="ofs">Where the key material starts in the buffer.</param>
        /// <param name="len">Length of the key material in bytes.</param>
        public static BlowfishStream Create(
            Stream stm,
            BlowfishStreamMode mode,
            byte[] key,
            int ofs,
            int len)
        {
            SHA1 sha = new SHA1CryptoServiceProvider();
            BlowfishAlgorithm balg = new BlowfishAlgorithm();

            balg.Key     = sha.ComputeHash(key, ofs, len);
            balg.Padding = PaddingMode.PKCS7;
            balg.Mode    = CipherMode.CBC;
            sha.Clear();

            if (BlowfishStreamMode.Write == mode)
            {
                byte[] iv = balg.IV;
                stm.Write(iv, 0, iv.Length);
                return(new BlowfishStream(stm, balg.CreateEncryptor(), CryptoStreamMode.Write));
            }
            else
            {
                byte[] iv = new byte[balg.BlockSize >> 3];
                for (int i = 0; i < iv.Length; i++)
                {
                    int ivb = stm.ReadByte();
                    if (-1 == ivb)
                    {
                        throw new IOException(Properties.Resources.JAVAIOP_CANNOT_READ_IV);
                    }
                    iv[i] = (byte)ivb;
                }
                balg.IV = iv;
                return(new BlowfishStream(stm, balg.CreateDecryptor(), CryptoStreamMode.Read));
            }
        }
        /// <summary>Creates a new Blowfish stream.</summary>
        /// <param name="stm">The stream to read or write to.</param>
        /// <param name="mode">Operation mode</param>
        /// <param name="key">The buffer with the key material.</param>
        /// <param name="ofs">Where the key material starts in the buffer.</param>
        /// <param name="len">Length of the key material in bytes.</param>
        public static BlowfishStream Create(
            Stream stm,
            BlowfishStreamMode mode,
            byte[] key,
            int ofs,
            int len)
        {
            SHA1 sha = new SHA1CryptoServiceProvider();
            BlowfishAlgorithm balg = new BlowfishAlgorithm();
            balg.Key = sha.ComputeHash(key, ofs, len);
            balg.Padding = PaddingMode.PKCS7;
            balg.Mode = CipherMode.CBC;
            sha.Clear();

            if (BlowfishStreamMode.Write == mode)
            {
                byte[] iv = balg.IV;
                stm.Write(iv, 0, iv.Length);
                return new BlowfishStream(stm, balg.CreateEncryptor(), CryptoStreamMode.Write);
            }
            else
            {
                byte[] iv = new byte[balg.BlockSize >> 3];
                for (int i = 0; i < iv.Length; i++)
                {
                    int ivb = stm.ReadByte();
                    if (-1 == ivb)
                    {
                        throw new IOException( Properties.Resources.JAVAIOP_CANNOT_READ_IV);
                    }
                    iv[i] = (byte)ivb;
                }
                balg.IV = iv;
                return new BlowfishStream(stm, balg.CreateDecryptor(), CryptoStreamMode.Read);
            }
        }
 void CopyPadding(BlowfishAlgorithm ba)
 {
     switch (Padding)
     {
         case PaddingMode.ANSIX923:
         case PaddingMode.ISO10126:
         case PaddingMode.PKCS7:
         case PaddingMode.Zeros:
         {
             ba.Padding = Padding;
             break;
         }
         default:
         {
             throw new CryptographicException();
         }
     }
 }
        /// <see cref="System.Security.Cryptography.SymmetricAlgorithm.CreateEncryptor(byte[], byte[])"/>
        public override ICryptoTransform CreateEncryptor(byte[] key, byte[] iv)
        {
            BlowfishAlgorithm result = new BlowfishAlgorithm(
                key,
                iv,
                (CipherMode.CBC == ModeValue),
                true);

            CopyPadding(result);
            return result;
        }