ManyMonkeys.Cryptography.Twofish.CreateEncryptor C# (CSharp) Method

CreateEncryptor() public method

Creates an object that supports ICryptoTransform that can be used to encrypt data using the Twofish encryption algorithm.
public CreateEncryptor ( byte key, byte iv ) : ICryptoTransform
key byte A byte array that contains a key. The length of this key should be equal to the KeySize property
iv byte A byte array that contains an initialization vector. The length of this IV should be equal to the BlockSize property
return ICryptoTransform
        public override ICryptoTransform CreateEncryptor(byte[] key, byte[] iv)
        {
            Key = key; // this appears to make a new copy

            if (Mode == CipherMode.CBC)
                IV = iv;

            return new TwofishEncryption(KeySize, ref KeyValue, ref IVValue, ModeValue, TwofishBase.EncryptionDirection.Encrypting);
        }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            Twofish fish = new Twofish();

            fish.Mode = CipherMode.ECB;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            byte[] Key = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
            byte[] dummy = {};

            //create Twofish Encryptor from this instance
            ICryptoTransform encrypt = fish.CreateEncryptor(Key, dummy); // we use the plainText as the IV as in ECB mode the IV is not used

            //Create Crypto Stream that transforms file stream using twofish encryption
            CryptoStream cryptostream = new CryptoStream(ms, encrypt, CryptoStreamMode.Write);

            byte[] plainText = GetBytes("Some string to encrypt");

            //write out Twofish encrypted stream
            cryptostream.Write(plainText, 0, plainText.Length);

            cryptostream.Close();

            byte[] bytOut = ms.ToArray();

            System.Console.WriteLine( "Encrypted string: " + GetString( bytOut ) );

            //create Twofish Decryptor from our twofish instance
            ICryptoTransform decrypt = fish.CreateDecryptor(Key, plainText);

            System.IO.MemoryStream msD = new System.IO.MemoryStream();

            //create crypto stream set to read and do a Twofish decryption transform on incoming bytes
            CryptoStream cryptostreamDecr = new CryptoStream(msD, decrypt, CryptoStreamMode.Write);

            //write out Twofish encrypted stream
            cryptostreamDecr.Write(bytOut, 0, bytOut.Length);

            cryptostreamDecr.Close();

            byte[] bytOutD = msD.GetBuffer();

            System.Console.WriteLine("Decrypted string: " + GetString(bytOutD));
        }
All Usage Examples Of ManyMonkeys.Cryptography.Twofish::CreateEncryptor