字符串、图片的加密解密
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");
}
{
/// <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");
}
相关文章推荐
- 使用Des,MD5 加密.解密.字符串.文件
- php基于mcrypt_encrypt和mcrypt_decrypt实现字符串加密解密的方法
- [C++]DES加密解密工具2.1及其代码——支持字符串及文件加密,支持3重DES
- C# 字符串的加密与解密
- BASE64加密图片字符串转码
- Java中对字符串进行加密和解密
- 数据结构上机:字符串的加密解密
- 对字符串进行SHA1和DES(加密,解密),加密和解密的类
- C#中对字符串的加密和解密
- android imageloader加密解密图片
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- java 对字符串的加密解密
- json对象转为字符串,当做参数传递时加密解密的实现方法
- iOS里加密字符串、图片、视频方法
- 安卓NDK开发案列一:字符串的加密和解密
- 用java编写一个简单的字符串加密解密程序,将字符串分成若干行,实现字符串以一列一列读取,并还原之前输入的字符串。这里实现4行输出。
- web.config 连接字符串 加密解密,代码方式
- BASE64加密解密(字符串)
- 图片加密解密程序2
- 字符串CryptoJS前台加密,pycrypto后台解密(备份一下)