Exemplo n.º 1
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);
            }
        }