Esempio n. 1
0
        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));
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public string Encrypt(string str)
        {
            Twofish fish = new Twofish();

            fish.Mode = CipherMode.ECB;

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

            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(str);

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

            cryptostream.Close();

            byte[] bytOut = ms.ToArray();

            return(GetString(bytOut));
        }
Esempio n. 4
0
        public bool unpackStageTwo()
        {
            Twofish twofish = new Twofish()
            {
                Mode = System.Security.Cryptography.CipherMode.CBC,
                Key  = new byte[] { 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89 },
                IV   = new byte[] { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 }
            };

            ICryptoTransform decrypt = twofish.CreateDecryptor();

            System.IO.MemoryStream msD = new System.IO.MemoryStream();
            CryptoStream           cryptostreamDecr = new CryptoStream(msD, decrypt, CryptoStreamMode.Write);

            cryptostreamDecr.Write(this.Buffer, 0, this.Buffer.Length);
            cryptostreamDecr.Close();

            byte[] tmp = msD.GetBuffer();
            Console.WriteLine(tmp.Length);
            for (int i = 0; i < this.Buffer.Length; i++)
            {
                this.Buffer[i] = tmp[i];
            }

            return(true);
        }
 public SshTwofish256Cbc()
     : base()
 {
     _algorithm         = new Twofish();
     _algorithm.Mode    = CipherMode.CBC;
     _algorithm.KeySize = 256;
 }
Esempio n. 6
0
        private static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV)
        {
            ValidateArguments(s, bEncrypt, pbKey, pbIV);

            Twofish f = new Twofish();

            byte[] pbLocalIV = new byte[16];
            Array.Copy(pbIV, pbLocalIV, 16);
            f.IV = pbLocalIV;

            byte[] pbLocalKey = new byte[32];
            Array.Copy(pbKey, pbLocalKey, 32);
            f.KeySize = 256;
            f.Key     = pbLocalKey;

            f.Mode    = m_rCipherMode;
            f.Padding = m_rCipherPadding;

            ICryptoTransform iTransform = (bEncrypt ? f.CreateEncryptor() : f.CreateDecryptor());

            Debug.Assert(iTransform != null);
            if (iTransform == null)
            {
                throw new SecurityException("Unable to create Twofish transform!");
            }

            return(new CryptoStream(s, iTransform, bEncrypt ? CryptoStreamMode.Write :
                                    CryptoStreamMode.Read));
        }
 public SshTwofish192Cbc()
     : base()
 {
     _algorithm = new Twofish();
     _algorithm.Mode = CipherMode.CBC;
     _algorithm.KeySize = 192;
 }
Esempio n. 8
0
        private static SymmetricAlgorithm CreateCipher(CipherAlgorithm ca, string encryptionKey)
        {
            SymmetricAlgorithm sa = null;

            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[]
                                                            { 21, 221, 125, 99, 18, 200, 181, 116, 158, 30, 180, 73, 108 });

            switch (ca)
            {
            case CipherAlgorithm.AES128:
                sa = Aes.Create();
                break;

            case CipherAlgorithm.AES256:
                sa = Aes.Create();
                break;

            case CipherAlgorithm.TwoFish128:
                sa = new Twofish();
                break;

            case CipherAlgorithm.BlowFish256:
                sa = new BlowFish();
                break;

            case CipherAlgorithm.Serpent128:
                sa = new SerpentEngine();
                break;
            }

            sa.Key = pdb.GetBytes(sa.KeySize / 8);
            sa.IV  = pdb.GetBytes(sa.BlockSize / 8);

            return(sa);
        }
Esempio n. 9
0
 public MxoTwofish()
 {
     tf           = new Twofish();
     tf.Mode      = CipherMode.CBC;
     tf.KeySize   = 128;
     tf.BlockSize = 128;
 }
Esempio n. 10
0
        /// <summary>
        /// Test the twofish cipher in ECB mode
        /// </summary>
        /// <param name="Key">The used to encrypt the data.</param>
        /// <param name="plainText">The plain text.</param>
        /// <param name="cryptText">The encrypted text to be used for comparison.</param>
        static void TestTwofishCBC(ref byte[] Key, ref byte[] plainText, ref byte[] iv, ref byte[] cryptText)
        {
            Twofish fish = new Twofish();

            fish.Mode = CipherMode.CBC;

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

            //create Twofish Encryptor from this instance
            ICryptoTransform encrypt = fish.CreateEncryptor(Key, iv);            // 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);

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

            cryptostream.Close();

            byte[] bytOut = ms.ToArray();

/*
 *                      // check the first block only
 *
 *                      for (int i=0;i<cryptText.Length;i++)
 *                      {
 *                              if (bytOut[i] != cryptText[i])
 *                              {
 *                                      Trace.Write("Cryptext match failure\n");
 *                                      break;
 *                              }
 *                      }
 */
            //create Twofish Decryptor from our twofish instance
            ICryptoTransform decrypt = fish.CreateDecryptor(Key, iv);

            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();

            // check
            for (int i = 0; i < plainText.Length; i++)
            {
                if (bytOutD[i] != plainText[i])
                {
                    Trace.Write("Plaintext match failure\n");
                    break;
                }
            }
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.Unicode;

            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));

            Console.ReadKey();
        }
Esempio n. 12
0
        public string shadow_encrypt(string plainText)
        {
            fish      = new Twofish();
            fish.Mode = CipherMode.ECB;
            ms        = new System.IO.MemoryStream();
            //form.log("we were guna send the IM with " + plainText);
            byte []          plainBytes   = Utils.StringToBytes(plainText);
            ICryptoTransform encode       = new ToBase64Transform();
            ICryptoTransform encrypt      = fish.CreateEncryptor(form.getKey(), plainBytes);
            CryptoStream     cryptostream = new CryptoStream(new CryptoStream(ms, encode, CryptoStreamMode.Write), encrypt, CryptoStreamMode.Write);

            cryptostream.Write(plainBytes, 0, plainBytes.Length);
            cryptostream.Close();
            byte[] bytOut = ms.ToArray();
            form.log("We encrypted " + plainText + " to " + Utils.BytesToString(bytOut), Color.DarkRed);
            return(Utils.BytesToString(bytOut));
        }
Esempio n. 13
0
        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));
        }
Esempio n. 14
0
        public ShadowPlugin(ProxyFrame frame)
        {
            fish      = new Twofish();
            fish.Mode = CipherMode.ECB;
            ms        = new System.IO.MemoryStream();

            formthread = new Thread(new ThreadStart(delegate()
            {
                form = new ShadowForm1(this);
                Application.Run(form);
            }));
            formthread.SetApartmentState(ApartmentState.STA);
            formthread.Start();


            this.frame = frame;
            this.proxy = frame.proxy;
            this.proxy.AddDelegate(PacketType.ScriptDialogReply, Direction.Outgoing, new PacketDelegate(OutDialogFromViewer));
            this.proxy.AddDelegate(PacketType.ChatFromViewer, Direction.Outgoing, new PacketDelegate(OutChatFromViewerHandler));
            this.proxy.AddDelegate(PacketType.ImprovedInstantMessage, Direction.Incoming, new PacketDelegate(RecivedIM));
            this.proxy.AddDelegate(PacketType.ImprovedInstantMessage, Direction.Outgoing, new PacketDelegate(SendingIM));
            this.proxy.AddDelegate(PacketType.ChatFromSimulator, Direction.Incoming, new PacketDelegate(InChatFromServerHandler));
        }
Esempio n. 15
0
        /// <summary>
        /// This encrypts our data using twofish and then converts to base64 and then reverses the process
        /// </summary>
        /// <param name="Key">The Key to use for the encryption stage</param>
        /// <param name="plainText">The plain text to encrypt and encode and then to compare when it has been decoded and decrypted</param>
        static void Cascade(ref byte[] Key, ref byte[] plainText)
        {
            Twofish fish = new Twofish();

            fish.Mode = CipherMode.ECB;

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

            // create an encoder
            ICryptoTransform encode = new ToBase64Transform();

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

            // we have to work backwords defining the last link in the chain first
            CryptoStream cryptostreamEncode = new CryptoStream(ms, encode, CryptoStreamMode.Write);
            CryptoStream cryptostream       = new CryptoStream(cryptostreamEncode, encrypt, CryptoStreamMode.Write);

            // or we could do this as we don't need to use cryptostreamEncode
            //CryptoStream cryptostream = new CryptoStream(new CryptoStream(ms,encode,CryptoStreamMode.Write),
            //										encrypt,CryptoStreamMode.Write);


            cryptostream.Write(plainText, 0, plainText.Length);


            cryptostream.Close();

            //long pos = ms.Position; // our stream is closed so we cannot find out what the size of the buffer is - daft
            byte[] bytOut = ms.ToArray();

            // and now we undo what we did

            // create a decoder
            ICryptoTransform decode = new FromBase64Transform();

            //create DES Decryptor from our des 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 cryptostreamD      = new CryptoStream(msD, decrypt, CryptoStreamMode.Write);
            CryptoStream cryptostreamDecode = new CryptoStream(cryptostreamD, decode, CryptoStreamMode.Write);

            // again we could do the following
            //CryptoStream cryptostreamDecode = new CryptoStream(new CryptoStream(msD,decrypt,CryptoStreamMode.Write),
            //											decode,CryptoStreamMode.Write);


            //write out the decrypted stream
            cryptostreamDecode.Write(bytOut, 0, bytOut.Length);

            cryptostreamDecode.Close();

            byte[] bytOutD = msD.ToArray();             // we should now have our plain text back

            for (int i = 0; i < plainText.Length; i++)
            {
                if (bytOutD[i] != plainText[i])
                {
                    Trace.Write("Plaintext match failure");
                    break;
                }
            }
        }
Esempio n. 16
0
 public void Init()
 {
     twofish128BitKey = new Twofish(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 });
     twofish192BitKey = new Twofish(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 });
     twofish256BitKey = new Twofish(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 });
 }