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 |
Résultat | ICryptoTransform |
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; }
public string Decrypt(string encrypted_str) { Twofish fish = new Twofish(); fish.Mode = CipherMode.ECB; byte[] plainText = {}; //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); byte[] bytOut = GetBytes(encrypted_str); //write out Twofish encrypted stream cryptostreamDecr.Write(bytOut, 0, bytOut.Length); cryptostreamDecr.Close(); byte[] bytOutD = msD.GetBuffer(); return GetString(bytOutD); }
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)); }
public string shadow_decrypt(string encyptedText) { fish = new Twofish(); fish.Mode = CipherMode.ECB; ms = new System.IO.MemoryStream(); //form.log("we were sent the IM with " + encyptedText); byte[] encyptedBytes = Utils.StringToBytes(encyptedText); ICryptoTransform decode = new FromBase64Transform(); //create DES Decryptor from our des instance ICryptoTransform decrypt = fish.CreateDecryptor(form.getKey(), encyptedBytes); System.IO.MemoryStream msD = new System.IO.MemoryStream(); CryptoStream cryptostreamDecode = new CryptoStream(new CryptoStream(msD,decrypt,CryptoStreamMode.Write),decode,CryptoStreamMode.Write); cryptostreamDecode.Write(encyptedBytes, 0, encyptedBytes.Length); cryptostreamDecode.Close(); byte[] bytOutD = msD.ToArray(); // we should now have our plain text back form.log("We decrypted "+encyptedText+" to " + Utils.BytesToString(bytOutD),Color.Red); return ""+this.indicator+""+Utils.BytesToString(bytOutD); }