Esempio n. 1
0
        private void EncryptButton_Click(object sender, EventArgs e)
        {
            Form encryptForm = new EncryptForm();

            encryptForm.Show();
            encryptForm.Location = this.Location;
            encryptForm.Left    += 600;
            encryptForm.Top     += 200;
        }
        public static byte[] EncryptStringToBytes(EncryptForm ef, string plainText, byte[] Key, CipherMode cipherMode, byte[] IV)
        {
            if (plainText == null || plainText.Length <= 0)
            {
                throw new ArgumentNullException("plainText");
            }
            if (Key == null || Key.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }

            byte[] encrypted;

            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key  = Key;
                rijAlg.Mode = cipherMode;
                rijAlg.IV   = IV;

                // Create a decryptor to perform the stream transform.
                var encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
                int i         = 0;
                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }

            return(encrypted);
        }