最简单的C#对称加密例子备忘

 private void button1_Click(object sender, EventArgs e)
        {
            System.Security.Cryptography.RijndaelManaged r = (System.Security.Cryptography.RijndaelManaged)System.Security.Cryptography.RijndaelManaged.Create();
            r.GenerateIV();
            r.GenerateKey();
            string Key = Convert.ToBase64String(r.Key);
            string IV = Convert.ToBase64String(r.IV);
            MessageBox.Show(Key); MessageBox.Show(IV);
            byte[] encryptbyte = CryptoTransform(r.CreateEncryptor(Convert.FromBase64String(Key), Convert.FromBase64String(IV)), Encoding.UTF8.GetBytes("http://www.izoq.net/"));
            string encryptkey = Convert.ToBase64String(encryptbyte);

            byte[] byteDecrypted = CryptoTransform(r.CreateDecryptor(Convert.FromBase64String(Key), Convert.FromBase64String(IV)), Convert.FromBase64String(encryptkey));
            string decryptstring = Encoding.UTF8.GetString(byteDecrypted);

            MessageBox.Show(encryptkey);
            MessageBox.Show(decryptstring);
            MessageBox.Show("http://www.dc9.cn");
        }

        private byte[] CryptoTransform(System.Security.Cryptography.ICryptoTransform cryptoTransform, byte[] bSrcs)
        {
          
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, cryptoTransform, System.Security.Cryptography.CryptoStreamMode.Write);
            try
            {
                cs.Write(bSrcs, 0, bSrcs.Length);
                cs.FlushFinalBlock();
                ms.Position = 0;
            }
            catch (System.Security.Cryptography.CryptographicException)
            {
                
            }
            finally
            {
                ms.Close();
                cs.Close();
            }
//www.dc9.cn
            return ms.ToArray();
        }

2 comments

  1. Alexis Mcwilliams 说道:

    I simply want to tell you that I am just newbie to blogs and definitely loved this website. Most likely I’m going to bookmark your blog post . You really have terrific posts. Appreciate it for sharing with us your web site.

  2. Mohammed Rinkus 说道:

    It’s going to be ending of mine day, except before ending I am reading this great paragraph to improve my knowledge.

发表评论

电子邮件地址不会被公开。