⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blowfishalgorithm.cs

📁 source for advanced algorithm
💻 CS
📖 第 1 页 / 共 2 页
字号:
/*
  Copyright 2001-2007 Markus Hahn 
  All rights reserved. See documentation for license details.
*/

using System;
using System.Security;
using System.Security.Cryptography;
using BlowfishNET.Properties;

namespace BlowfishNET
{
        /// <summary>Implementation of the Blowfish algorithm as a standard component for
    /// the .NET security framework.</summary>
        public class BlowfishAlgorithm : SymmetricAlgorithm, ICryptoTransform
        {
                // in factory mode the Blowfish instances are always null, they only get
        // initialized in transformation mode

        BlowfishECB bfe;
        BlowfishCBC bfc;

                bool isEncryptor;
        byte[] block;

                RNGCryptoServiceProvider rng;

                /// <summary>Default constructor. Starts as an uninitialized ECB instance.</summary>
                public BlowfishAlgorithm() : base()
                {
                        KeySizeValue = BlowfishECB.MAX_KEY_LENGTH << 3;

                        LegalBlockSizesValue = new KeySizes[1];
                        LegalBlockSizesValue[0] = new KeySizes(
                                BlockSize, 
                                BlockSize, 
                                BlowfishECB.BLOCK_SIZE);

                        LegalKeySizesValue = new KeySizes[1];
                        LegalKeySizesValue[0] = new KeySizes(
                                0, 
                                BlowfishECB.MAX_KEY_LENGTH << 3,
                8);

                        ModeValue = CipherMode.ECB;
                }

                private BlowfishAlgorithm(byte[] key, byte[] iv, bool useCBC, bool isEncryptor)
                {
                        if (null == key) 
                        {       
                                GenerateKey(); 
                        }
                        else 
                        {       
                                Key = key;
                        }

                        if (useCBC)
                        {
                IV = iv;
        
                                this.bfc = new BlowfishCBC(KeyValue, 0, KeyValue.Length);
                                this.bfc.SetIV(IVValue, 0);
                        }
                        else
                        {
                                this.bfe = new BlowfishECB(KeyValue, 0, KeyValue.Length);
                        }
                        
                        this.isEncryptor = isEncryptor;
                }

                /// <see cref="System.Security.Cryptography.SymmetricAlgorithm.BlockSize"/>
                public override int BlockSize
                {
                        get
                        {
                                return BlowfishECB.BLOCK_SIZE << 3;
                        }
                        set
                        {
                                if (value != BlowfishECB.BLOCK_SIZE << 3)
                                {
                    throw new CryptographicException(Resources.BFALGO_INVALID_BLOCKSIZE);
                                }
                        }
                }

                /// <see cref="System.Security.Cryptography.SymmetricAlgorithm.IV"/>
                public override byte[] IV
                {
                        get
                        {
                if (null == IVValue)
                {
                    GenerateIV();
                }
                return (byte[])IVValue.Clone();
                        }
                        set
                        {
                                if (null == value) 
                                {
                                        throw new ArgumentNullException();
                                }
                                if (value.Length != BlowfishECB.BLOCK_SIZE) 
                                {
                    throw new CryptographicException(Resources.BFALGO_INVALID_IV_LENGTH);
                                }
                                IVValue = (byte[])value.Clone();
                        }
                }

                /// <see cref="System.Security.Cryptography.SymmetricAlgorithm.Key"/>
                public override byte[] Key
                {
                        get
                        {
                                return KeyValue;
                        }
                        set
                        {
                                KeyValue = value;
                        }
                }

                /// <see cref="System.Security.Cryptography.SymmetricAlgorithm.KeySize"/>
                public override int KeySize
                {
                        get
                        {
                                return KeySizeValue;
                        }
                        set
                        {
                                KeySizes ks = LegalKeySizes[0];

                                if ((0 != (value % ks.SkipSize)) ||
                                        (value > ks.MaxSize) ||
                                        (value < ks.MinSize))
                                {
                    throw new CryptographicException(Resources.BFALGO_INVALID_KEYSIZE);
                                }
                                KeySizeValue = value;
                        }
                }

                /// <see cref="System.Security.Cryptography.SymmetricAlgorithm.Mode"/>
                public override CipherMode Mode
                {
                        get
                        {
                                return ModeValue;
                        }
                        set
                        {
                                if (value != CipherMode.CBC &&
                                        value != CipherMode.ECB)
                                {
                    throw new CryptographicException(Resources.BFALGO_INVALID_CIPHERMODE);
                                }
                                ModeValue = value;
                        }
                }

        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(String.Format(Resources.BFALGO_UNSUPPORTED_PADDINGMODE_1, Padding));
                }
            }
        }

                /// <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;
                }

                /// <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;
                }

                /// <see cref="System.Security.Cryptography.SymmetricAlgorithm.GenerateKey"/>
                public override void GenerateKey()
                {
                        if (null == this.rng) 
                        {       
                                this.rng = new RNGCryptoServiceProvider();
                        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -