RijndaelManaged 自定义key和iv
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace SynEncrption
{
public partial class Form1 : Form
{
private string key = "abcde234123@#ew9182h4r1238132&AUIFQEIfaidsfnqeiqewrndsifqnwqiwernqewiqeiqe";
private string iv = "s9afn1@#$!#asdfiqwnerasidfnqwiesndaf912#$!@341";
public Form1()
{
InitializeComponent();
}
private void smbEncrpty_Click(object sender, EventArgs e)
{
string text = encrption(this.memoEdit1.Text, key.Substring(0, 32), iv.Substring(0, 16));
this.memoEdit2.Text = text;
}
private string encrption(string input, string key, string iv)
{
MemoryStream msEncrypt = null;
RijndaelManaged aesAlg = null;
string sresult = string.Empty;
try
{
byte[] keys = System.Text.Encoding.UTF8.GetBytes(key);
byte[] ivs = System.Text.Encoding.UTF8.GetBytes(iv);
aesAlg = new RijndaelManaged();
aesAlg.Key = keys;
aesAlg.IV = ivs;
ICryptoTransform ict = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
msEncrypt = new MemoryStream();
using (CryptoStream cts = new CryptoStream(msEncrypt, ict, CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cts))
{
sw.Write(input);
}
}
}
finally
{
if (aesAlg != null)
{
aesAlg.Dispose();
aesAlg.Clear();
}
}
if (msEncrypt != null)
{
byte[] content = msEncrypt.ToArray();
sresult = Convert.ToBase64String(content);
}
return sresult;
}
private string decrption(string input, string key, string iv)
{
string sresult = string.Empty;
byte[] keys = System.Text.Encoding.UTF8.GetBytes(key);
byte[] ivs = System.Text.Encoding.UTF8.GetBytes(iv);
byte[] inputbytes = Convert.FromBase64String(input);
RijndaelManaged rm =null;
try
{
rm = new RijndaelManaged();
rm.Key = keys;
rm.IV = ivs;
ICryptoTransform ict = rm.CreateDecryptor(rm.Key, rm.IV);
using (MemoryStream ms = new MemoryStream(inputbytes))
{
using (CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(cs))
{
sresult = sr.ReadToEnd();
}
}
}
}
finally
{
if (rm != null)
{
rm.Dispose();
rm.Clear();
}
}
return sresult;
}
private void smbDecrypt_Click(object sender, EventArgs e)
{
string text = decrption(this.memoEdit2.Text, key.Substring(0, 32), iv.Substring(0, 16));
this.memoEdit3.Text = text;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace SynEncrption
{
public partial class Form1 : Form
{
private string key = "abcde234123@#ew9182h4r1238132&AUIFQEIfaidsfnqeiqewrndsifqnwqiwernqewiqeiqe";
private string iv = "s9afn1@#$!#asdfiqwnerasidfnqwiesndaf912#$!@341";
public Form1()
{
InitializeComponent();
}
private void smbEncrpty_Click(object sender, EventArgs e)
{
string text = encrption(this.memoEdit1.Text, key.Substring(0, 32), iv.Substring(0, 16));
this.memoEdit2.Text = text;
}
private string encrption(string input, string key, string iv)
{
MemoryStream msEncrypt = null;
RijndaelManaged aesAlg = null;
string sresult = string.Empty;
try
{
byte[] keys = System.Text.Encoding.UTF8.GetBytes(key);
byte[] ivs = System.Text.Encoding.UTF8.GetBytes(iv);
aesAlg = new RijndaelManaged();
aesAlg.Key = keys;
aesAlg.IV = ivs;
ICryptoTransform ict = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
msEncrypt = new MemoryStream();
using (CryptoStream cts = new CryptoStream(msEncrypt, ict, CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cts))
{
sw.Write(input);
}
}
}
finally
{
if (aesAlg != null)
{
aesAlg.Dispose();
aesAlg.Clear();
}
}
if (msEncrypt != null)
{
byte[] content = msEncrypt.ToArray();
sresult = Convert.ToBase64String(content);
}
return sresult;
}
private string decrption(string input, string key, string iv)
{
string sresult = string.Empty;
byte[] keys = System.Text.Encoding.UTF8.GetBytes(key);
byte[] ivs = System.Text.Encoding.UTF8.GetBytes(iv);
byte[] inputbytes = Convert.FromBase64String(input);
RijndaelManaged rm =null;
try
{
rm = new RijndaelManaged();
rm.Key = keys;
rm.IV = ivs;
ICryptoTransform ict = rm.CreateDecryptor(rm.Key, rm.IV);
using (MemoryStream ms = new MemoryStream(inputbytes))
{
using (CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(cs))
{
sresult = sr.ReadToEnd();
}
}
}
}
finally
{
if (rm != null)
{
rm.Dispose();
rm.Clear();
}
}
return sresult;
}
private void smbDecrypt_Click(object sender, EventArgs e)
{
string text = decrption(this.memoEdit2.Text, key.Substring(0, 32), iv.Substring(0, 16));
this.memoEdit3.Text = text;
}
}
}