项目常用代码拾遗(1) ( Archived on 2008-6-17 21:03:18 414 Views )
1.使用TripleDES加密和解密:强度比较高
using System;
using System.Security.Cryptography;
using System.Text;
namespace CSST.Common
{
public class TripleDESHelper
{
//-----------------:a1c2e3g4i5k6m7o8q9s0u1w2-----------
private TripleDESHelper()
{
}
public static string Encrypt(string toEncrypt, string key, bool useHashing)
{
try
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
catch
{
}
return string.Empty;
}
public static string Decrypt(string toDecrypt, string key, bool useHashing)
{
try
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
catch
{
}
return string.Empty;
}
}
}
2.MD5加密
public static string MD5(string str)
{
if (str.Length > 0)
{
byte[] bytes = Encoding.Default.GetBytes(str);
bytes = new MD5CryptoServiceProvider().ComputeHash(bytes);
string str2 = "";
for (int i = 0; i < bytes.Length; i++)
{
str2 = str2 + bytes[i].ToString("x").PadLeft(2, '0');
}
return str2;
}
else
{
return string.Empty;
}
}
3.发送邮件
using System;
using System.Collections;
using System.Net.Mail;
namespace CSST.Common
{
public class MailUnit
{
public string smtp;
public string from;
public string pwd;
public string to;
public string subject;
public string body;
public ArrayList paths;
public MailUnit(string Psmtp, string Pfrom, string Ppwd, string Pto, string Psubject, string Pbody, ArrayList Ppaths)
{
smtp = Psmtp;
from = Pfrom;
pwd = Ppwd;
to = Pto;
subject = Psubject;
body = Pbody;
paths = Ppaths;
}
/*发邮件*/
public bool SendMail()
{
//创建smtpclient对象
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = smtp;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(from, pwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage(from, to);
message.Subject = subject;
//正文默认格式为html
message.Body = body;
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
//添加附件
if (paths.Count != 0)
{
foreach (string path in paths)
{
Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
}
}
try
{
client.Send(message);
//MessageBox.Show("Email successfully sent.");
return true;
}
catch (Exception ex)
{
//MessageBox.Show("Send Email Failed." + ex.ToString());
throw new Exception(ex.Message);
return false;
}
}
/*发邮件:线程中使用*/
public void SendMail2()
{
//All.runing++;
//创建smtpclient对象
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = smtp;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(from, pwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage(from, to);
message.Subject = subject;
//正文默认格式为html
message.Body = body;
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
//添加附件
if (paths.Count != 0)
{
foreach (string path in paths)
{
Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
}
}
try
{
client.Send(message);
//All.success++;
//All.runing--;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
//All.fail++;
//All.runing--;
}
}
}
}