您的位置:首页 > 其它

字符串、图片的加密解密

2010-08-17 10:37 435 查看
class DES
{
/// <summary>
///加密
/// </summary>
/// <param name="pToEncrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string MD5Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}

/// <summary>
///加密
/// </summary>
/// <param name="pToEncrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static byte[] MD5Encrypt(byte[] inputByteArray, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return ms.ToArray();
}

/// <summary>
/// 解密
/// </summary>
/// <param name="pToDecrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string MD5Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}

des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}

/// <summary>
/// 解密
/// </summary>
/// <param name="pToDecrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static byte[] MD5Decrypt(byte[] inputByteArray, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return ms.ToArray();
}

/// <summary>
/// 生成key
/// </summary>
/// <returns></returns>
public static string GenerateKey()
{
DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}
}

main()

{

/*图片 文件加密*/
string filename = "xiongmao.jpg";
//读取文件 或者读取数据库图片信息
FileStream myfilestream = new FileStream(filename, FileMode.Open);
BinaryReader reader = reader = new BinaryReader(myfilestream);
byte[] image = reader.ReadBytes((int)myfilestream.Length);
//加密
string key=DES.GenerateKey();
byte[] jiami = DES.MD5Encrypt(image, key);
//解密
byte[] imageData = DES.MD5Decrypt(jiami, key);
//显示图片
MemoryStream mStream = new MemoryStream();
mStream.Write(imageData, 0, imageData.Length);
mStream.Flush();
Bitmap img = new Bitmap(mStream);
pictureBox1.Image = img;

/*字符串加密解密*/
string s = DES.MD5Encrypt("fdsa你好,世博会", key);
string sss = DES.MD5Decrypt(s, key);

}

===================

或者

===================

class DES2
{
/// <summary>
/// DES加密方法
/// </summary>
/// <param name="strPlain">明文</param>
/// <param name="strDESKey">密钥</param>
/// <param name="strDESIV">向量</param>
/// <returns>密文</returns>
public static string DESEncrypt(string strPlain, string strDESKey, string strDESIV)
{
byte[] bytesDESKey = ASCIIEncoding.ASCII.GetBytes(strDESKey.Substring(0, 8));

byte[] bytesDESIV = ASCIIEncoding.ASCII.GetBytes(strDESIV);
DESCryptoServiceProvider desEncrypt = new DESCryptoServiceProvider();
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, desEncrypt.CreateEncryptor(bytesDESKey, bytesDESIV), CryptoStreamMode.Write);
StreamWriter swEncrypt = new StreamWriter(csEncrypt);

swEncrypt.WriteLine(strPlain);
swEncrypt.Close();
csEncrypt.Close();
byte[] bytesCipher = msEncrypt.ToArray();
msEncrypt.Close();
return Convert.ToBase64String(bytesCipher);
}
/// <summary>
/// DES解密方法
/// </summary>
/// <param name="strCipher">密文</param>
/// <param name="strDESKey">密钥</param>
/// <param name="strDESIV">向量</param>
/// <returns>明文</returns>
public static string DESDecrypt(string strCipher, string strDESKey, string strDESIV)
{
byte[] bytesDESKey = ASCIIEncoding.ASCII.GetBytes(strDESKey.Substring(0, 8));
byte[] bytesDESIV = ASCIIEncoding.ASCII.GetBytes(strDESIV);
byte[] bytesCipher = Convert.FromBase64String(strCipher);
DESCryptoServiceProvider desDecrypt = new DESCryptoServiceProvider();
MemoryStream msDecrypt = new MemoryStream(bytesCipher);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, desDecrypt.CreateDecryptor(bytesDESKey, bytesDESIV), CryptoStreamMode.Read);
StreamReader srDecrypt = new StreamReader(csDecrypt);
string strPlainText = srDecrypt.ReadLine();
srDecrypt.Close();
csDecrypt.Close();
msDecrypt.Close();
return strPlainText;
}

/// <summary>
/// DES加密方法
/// </summary>
/// <param name="Plain">明文</param>
/// <param name="strDESKey">密钥</param>
/// <param name="strDESIV">向量</param>
/// <returns>密文</returns>
public static byte[] DESEncrypt(byte[] bytesPlain, string strDESKey, string strDESIV)
{
byte[] bytesDESKey = ASCIIEncoding.ASCII.GetBytes(strDESKey.Substring(0, 8));

byte[] bytesDESIV = ASCIIEncoding.ASCII.GetBytes(strDESIV);
DESCryptoServiceProvider desEncrypt = new DESCryptoServiceProvider();
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, desEncrypt.CreateEncryptor(bytesDESKey, bytesDESIV), CryptoStreamMode.Write);
csEncrypt.Write(bytesPlain, 0, bytesPlain.Length);
csEncrypt.Close();
byte[] bytesCipher = msEncrypt.ToArray();
msEncrypt.Close();
return bytesCipher;
}
/// <summary>
/// DES解密方法
/// </summary>
/// <param name="bytesCipher">密文</param>
/// <param name="strDESKey">密钥</param>
/// <param name="strDESIV">向量</param>
/// <returns>明文</returns>
public static byte[] DESDecrypt(byte[] bytesCipher, string strDESKey, string strDESIV)
{
byte[] bytesDESKey = ASCIIEncoding.ASCII.GetBytes(strDESKey.Substring(0, 8));
byte[] bytesDESIV = ASCIIEncoding.ASCII.GetBytes(strDESIV);
DESCryptoServiceProvider desDecrypt = new DESCryptoServiceProvider();
MemoryStream msDecrypt = new MemoryStream(bytesCipher);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, desDecrypt.CreateDecryptor(bytesDESKey, bytesDESIV), CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[bytesCipher.Length];
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
csDecrypt.Close();
msDecrypt.Close();
return fromEncrypt;
}

}

main()

{

/*图片 文件加密*/
string filename = "xiongmao.jpg";
//读取文件 或者读取数据库图片信息
FileStream myfilestream = new FileStream(filename, FileMode.Open);
BinaryReader reader = reader = new BinaryReader(myfilestream);
byte[] image = reader.ReadBytes((int)myfilestream.Length);
//加密
byte[] jiami = DES2.DESEncrypt(image, "abxiongabxiong", "xiongabxcddabxiongabxiongmaocddmaocdd");
//解密
byte[] imageData = DES2.DESDecrypt(jiami, "abxiongabxiong", "xiongabxcddabxiongabxiongmaocddmaocdd");
//显示图片
MemoryStream mStream = new MemoryStream();
mStream.Write(imageData, 0, imageData.Length);
mStream.Flush();
Bitmap img = new Bitmap(mStream);
pictureBox1.Image = img;

/*字符串加密解密*/
string s = DES2.DESEncrypt("fdsa你好,世博会","abxiongabxiong", "xiongabxcddabxiongabxiongmaocddmaocdd");
string sss = DES2.DESDecrypt(s, "abxiongabxiong", "xiongabxcddabxiongabxiongmaocddmaocdd");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: