C#如何实现des的加解密 请问各位附件的DES加解密如何实现?输入的值是十六进制的,2个字节。密钥也是十六进制,2个字节。要求加密或解密后的值也是十六进制的,2个字节。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 图片似乎传不上去图片地址是:http://www.5899222.com/DES.JPG public static void Encrypt(string pathIn, string pathOut, string password) { try { byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] key = System.Text.Encoding.UTF8.GetBytes(password); System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read); System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write); outfs.SetLength(0); byte[] myBytes = new byte[100]; long myInLength = 0; long myLength = fs.Length; System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider(); System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateEncryptor(key, iv), CryptoStreamMode.Write); while (myInLength < myLength) { int myLen = fs.Read(myBytes, 0, 100); cs.Write(myBytes, 0, myLen); myInLength += myLen; } cs.Close(); fs.Close(); outfs.Close(); } catch (IOException ioex) { Log.debug("Encrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace); } } public static void Decrypt(string pathIn, string pathOut, string password) { try { System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read); System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write); outfs.SetLength(0); long myLength = fs.Length; long myInLength = 0; byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] key = System.Text.Encoding.UTF8.GetBytes(password); System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider(); System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateDecryptor(key, iv), CryptoStreamMode.Write); byte[] myBytes = new byte[100]; while (myInLength < myLength) { int myLen = fs.Read(myBytes, 0, 100); cs.Write(myBytes, 0, myLen); myInLength += myLen; } cs.Close(); fs.Close(); outfs.Close(); } catch (IOException ioex) { Log.debug("Dncrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace); } }把这个改改, 参考。。/// <summary> /// Encrypt the string /// Attention:key must be 8 bits /// </summary> /// <param name="strText">string</param> /// <param name="strEncrKey">key</param> /// <returns></returns> public string DesEncrypt(string strText, string strEncrKey) { byte[] byKey = null; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; try { byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch (System.Exception error) { // MessageBox.Show(error.Message); return "error:" + error.Message + "\r"; } } /// <summary> /// Decrypt string /// Attention:key must be 8 bits /// </summary> /// <param name="strText">Decrypt string</param> /// <param name="sDecrKey">key</param> /// <returns>output string</returns> public string DesDecrypt(string strText, string sDecrKey) { byte[] byKey = null; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] inputByteArray = new Byte[strText.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); return encoding.GetString(ms.ToArray()); } catch (System.Exception error) { // MessageBox.Show(error.Message); return "error:" + error.Message + "\r"; } } 请问try catch里面可以套try catch吗? 怎么解决一个方法被不同调用者同时访问问题? 很简单的查询 [color=#FF0000]请问哪位有ClChart的一些学习资料,小弟不胜感激!![/color] 将“中国”变为“%D6%D0”用什么函数 100分,此段代码的优化问题. 说起来简单的问题, 设置窗体光标默认所在 button winform问题求助!50分! c#如何实现这样的功能,难,高手近来看看 关于两个DataTable获取差异和共同部分 求一个 报警的 GIF 图片 Float随机数值重复的问题s
图片地址是:
http://www.5899222.com/DES.JPG
public static void Encrypt(string pathIn, string pathOut, string password)
{
try
{
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] key = System.Text.Encoding.UTF8.GetBytes(password);
System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read);
System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write);
outfs.SetLength(0);
byte[] myBytes = new byte[100];
long myInLength = 0;
long myLength = fs.Length;
System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);
while (myInLength < myLength)
{
int myLen = fs.Read(myBytes, 0, 100);
cs.Write(myBytes, 0, myLen);
myInLength += myLen;
}
cs.Close();
fs.Close();
outfs.Close();
}
catch (IOException ioex)
{
Log.debug("Encrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace);
}
} public static void Decrypt(string pathIn, string pathOut, string password)
{
try
{
System.IO.FileStream fs = new FileStream(pathIn, FileMode.Open, FileAccess.Read);
System.IO.FileStream outfs = new FileStream(pathOut, FileMode.OpenOrCreate, FileAccess.Write);
outfs.SetLength(0);
long myLength = fs.Length;
long myInLength = 0;
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] key = System.Text.Encoding.UTF8.GetBytes(password);
System.Security.Cryptography.DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(outfs, dsp.CreateDecryptor(key, iv), CryptoStreamMode.Write);
byte[] myBytes = new byte[100];
while (myInLength < myLength)
{
int myLen = fs.Read(myBytes, 0, 100);
cs.Write(myBytes, 0, myLen);
myInLength += myLen;
}
cs.Close();
fs.Close();
outfs.Close();
}
catch (IOException ioex)
{
Log.debug("Dncrypt(pathIn, pathOut, password): " + ioex.Message + "\r\nSource:" + ioex.Source + "\r\nStackTrace:" + ioex.StackTrace);
}
}把这个改改,
/// <summary>
/// Encrypt the string
/// Attention:key must be 8 bits
/// </summary>
/// <param name="strText">string</param>
/// <param name="strEncrKey">key</param>
/// <returns></returns>
public string DesEncrypt(string strText, string strEncrKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (System.Exception error)
{
// MessageBox.Show(error.Message);
return "error:" + error.Message + "\r";
}
}
/// <summary>
/// Decrypt string
/// Attention:key must be 8 bits
/// </summary>
/// <param name="strText">Decrypt string</param>
/// <param name="sDecrKey">key</param>
/// <returns>output string</returns>
public string DesDecrypt(string strText, string sDecrKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = new Byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch (System.Exception error)
{
// MessageBox.Show(error.Message);
return "error:" + error.Message + "\r";
}
}