c#中如何对一个文件进行RSA加密解密
我想做一个加密文件传输的程序,传输部分已经做好了,但是加密这块一点头绪没有,请各位大侠能否赐教,谢谢啊
using System.Security.Cryptography;
程序代码:
/// <summary> /// 加密文件 /// </summary> /// <param name="file_in">待加密文件</param> /// <param name="file_out">输出文件</param> /// <param name="password">密码</param> private void Encrypt(string file_in, string file_out,string password) { if (File.Exists(file_in) && file_out != "") { if (File.Exists(file_out)) { if (MessageBox.Show("文件已存在,是否覆盖?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) { return; } } DESCryptoServiceProvider des = new DESCryptoServiceProvider(); FileStream fs = File.OpenRead(file_in); byte[] inputByteArray = new byte[fs.Length]; fs.Read(inputByteArray, 0, (int)fs.Length); fs.Close(); byte[] mypasswordbyte = Encoding.Default.GetBytes(password); System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] hash_byte = get_(mypasswordbyte); string resule = System.BitConverter.ToString(hash_byte); resule = resule.Replace("-", ""); byte[] keyByteArray = Encoding.Default.GetBytes(resule.Substring(0, 8)); des.Key = keyByteArray; des.IV = keyByteArray; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); fs = File.OpenWrite(file_out); foreach (byte b in ms.ToArray()) { fs.WriteByte(b); } fs.Close(); cs.Close(); ms.Close(); if (File.Exists(file_out)) { MessageBox.Show("加密成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("加密失败!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("未能找到文件 " + file_in + "或者输出文件路径错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } /// <summary> /// 解密文件 /// </summary> /// <param name="file_in">待解密文件</param> /// <param name="file_out">输出文件</param> /// <param name="password">密码</param> private void Decrypt(string file_in, string file_out, string password) { if (File.Exists(file_in) && file_out != "") { byte[] info; FileInfo MyFileInfo = new FileInfo(file_in); StreamReader mp3info = new StreamReader(file_in); info = new Byte[MyFileInfo.Length]; info = File.ReadAllBytes(file_in); byte[] newjiamifile = new byte[MyFileInfo.Length - 1000]; if (File.Exists(file_out)) { if (MessageBox.Show("文件已存在,是否覆盖", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } } DESCryptoServiceProvider des = new DESCryptoServiceProvider(); FileStream fs = File.OpenRead(file_in); byte[] inputByteArray = new byte[fs.Length]; fs.Read(inputByteArray, 0, (int)fs.Length); fs.Close(); byte[] mypasswordbyte = Encoding.Default.GetBytes(password); System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] hash_byte = get_(mypasswordbyte); string resule = System.BitConverter.ToString(hash_byte); resule = resule.Replace("-", ""); byte[] keyByteArray = Encoding.Default.GetBytes(resule.Substring(0, 8)); des.Key = keyByteArray; des.IV = keyByteArray; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); fs = File.OpenWrite(file_out); foreach (byte b in ms.ToArray()) { fs.WriteByte(b); } fs.Close(); cs.Close(); ms.Close(); if (File.Exists(file_out)) { MessageBox.Show("解密成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("解密失败!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("未能找到文件 " + file_in + "或者输出文件错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }