private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV) { //Create the file streams to handle the input and output files. FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0);
//Create variables to help with read and write. byte[] bin = new byte[100]; //This is intermediate storage for the encryption. long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
Console.WriteLine("Encrypting...");
//Read from the input file, then encrypt and write to the output file. while(rdlen < totlen) { len = fin.Read(bin, 0, 100); encStream.Write(bin, 0, len); rdlen = rdlen + len; Console.WriteLine("{0} bytes processed", rdlen); }
private static extern void b64_des(StringBuilder in_str, StringBuilder out_str, string key, int lenth, uint option);
///第一个参数就是你要进行加密解密的字符串,第二个参数就是要输出的字符串,第三个是指定参数,第四个是字符串长度,最后一个是加密解密参数。0加密 1解密。
[DllImport("des64.dll")]
private static extern int b64_size(int lenth, uint option); /// <summary>
/// <param name="m_Str"></param>
/// <param name="m_key"></param>
/// <param name="flag"></param>
/// <returns></returns>
/// </summary>
public static string KeyOrNoKey(string m_Str,string m_key,uint flag)
{
int outLen = b64_size(m_Str.Length, flag);
StringBuilder Result = new StringBuilder(0xff);
Result.Capacity = outLen;
Result.Length = outLen;
StringBuilder Source = new StringBuilder(m_Str);
b64_des(Source, Result,m_key, m_Str.Length, flag);
string s = Result.ToString().Trim();
return Result.ToString().Trim();
}
这里有个地方要注意。就是为什么要用stringbuilder。因为那个参数是可变的。
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
Console.WriteLine("Encrypting...");
//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
encStream.Close();
fout.Close();
fin.Close();
}
自己改改函数~
http://www.easyde.net/article/23/226/2006/2006060730461.html
现在是做一个基于以前系统的模块扩展,原来的是用des64进行加密的
private void trystart(object sender, EventArgs e)
{
string EnStr = NeedEn.Text.Trim();
string Entrypted = Entrypt(EnStr);
LabelResult.Text = Entrypted;
}
private string Entrypt(string str)
{
System.Security.Cryptography.DESCryptoServiceProvider TestEntrypt = new System.Security.Cryptography.DESCryptoServiceProvider();
byte[] waitEntrypt = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
TestEntrypt.GenerateIV();
TestEntrypt.GenerateKey();
byte[] Results = TestEntrypt.CreateEncryptor().TransformFinalBlock(waitEntrypt, 0, waitEntrypt.Length);
string result = Convert.ToBase64String(Results);
return result;
}
</script><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DES Data Entryption Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Entryption String:<asp:TextBox runat="server" ID="NeedEn"></asp:TextBox>
<br />
<br />
<asp:Button runat="server" Text="try" UseSubmitBehavior="true" OnClick="trystart" />
<br />
<br />
<asp:Label runat="server" ID="LabelResult"></asp:Label>
</div>
</form>
</body>
</html>