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

CreateDecryptor() public method

Creates an object that supports ICryptoTransform that can be used to decrypt data using the Twofish encryption algorithm.
public CreateDecryptor ( 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 CreateDecryptor(byte[] key, byte[] iv)
        {
            Key = key;

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

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

Usage Example

Example #1
0
        public byte[] Decrypt(byte[] file)
        {
            Twofish fish = new Twofish();

            fish.Mode = CipherMode.ECB;

            byte[] dummy = { };

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

            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(file, 0, file.Length);

            cryptostreamDecr.Close();

            byte[] buf = msD.GetBuffer();

            // TODO: It might be pretty dangerous to cut on the size of the input buffer
            // because of the padding some bytes might be added. However these bytes will
            // be only zeros (External.Twofish uses Padding.Zero) so zeros should be OK.
            Array.Resize(ref buf, file.Length);
            // We can not remove any other padding bytes because we can not distinuish between
            // bytes added by the crypto algo and bytes belonging to the original unecrtypted file.

            return buf;
        }
All Usage Examples Of ManyMonkeys.Cryptography.Twofish::CreateDecryptor