MD5:
//Winform: public static string StringToMD5Hash(string inputString) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(inputString)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < encryptedBytes.Length; i++) { sb.AppendFormat("{0:x2}", encryptedBytes[i]); } return sb.ToString(); } //Webform: public static string md5(string pwd) { string md5pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5"); return md5pwd; }
Base64:
这种编码方式,我曾经在一个项目中用到过,当时的情况是将图片字节数组转化为Base64编码,当时在图片字节转base64编码这个帖子中进行了讨论。这里只给出C#中的实现。
关键代码如下:
DataRow dr = ds.Tables["temp_pic"].Rows[0]; byte[] buffer = (byte[])dr["imagedata1"]; //string base64Str = Convert.ToBase64String(bytes); //byte[] buffer = Convert.FromBase64String(base64Str); MemoryStream ms2 = new MemoryStream(buffer, 0, buffer.Length); ms2.Seek(0, SeekOrigin.Begin); Image image2 = Image.FromStream(ms2); pictureBox1.Image = image2;
DES:
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string result = DESStr("guwei4037"); Console.WriteLine("这是你的加密结果:{0}", result);//1tE8zvi9ouQJNb4FlxlKFQ== string src = FromDESStr(result); Console.WriteLine("这是我的解密结果:{0}", src);//guwei4037 } /// <summary> /// DES加密 /// </summary> /// <param name="src"></param> /// <returns></returns> public static string DESStr(string src) { string des = string.Empty; byte[] bysData = Encoding.UTF8.GetBytes(src); DESCryptoServiceProvider objDESCryptoServiceProvider = new DESCryptoServiceProvider(); objDESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes("12345678");//密钥 objDESCryptoServiceProvider.Mode = CipherMode.ECB; objDESCryptoServiceProvider.Padding = PaddingMode.None; byte[] bysFixSizeData = new byte[(int)Math.Ceiling(bysData.Length / 8.0) * 8]; Array.Copy(bysData, bysFixSizeData, bysData.Length); byte[] bysEncrypted = objDESCryptoServiceProvider.CreateEncryptor().TransformFinalBlock(bysFixSizeData, 0, bysFixSizeData.Length);//加密 des = Convert.ToBase64String(bysEncrypted);//加密后的字符串 if (des.Equals("")) des = "error"; return des; } /// <summary> /// DES解密 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string FromDESStr(string str) { string des = string.Empty; byte[] inputByteArray = Convert.FromBase64String(str); DESCryptoServiceProvider objDESCryptoServiceProvider = new DESCryptoServiceProvider(); objDESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes("12345678");//密钥 objDESCryptoServiceProvider.Mode = CipherMode.ECB; objDESCryptoServiceProvider.Padding = PaddingMode.None; byte[] bysDEcrypted = objDESCryptoServiceProvider.CreateDecryptor().TransformFinalBlock(inputByteArray, 0, inputByteArray.Length);//解密 byte[] bysFixSizeData = new byte[(int)Math.Ceiling(bysDEcrypted.Length / 8.0) * 8]; Array.Copy(bysDEcrypted, bysFixSizeData, bysDEcrypted.Length); des = Encoding.UTF8.GetString(bysFixSizeData);//解密后的字符串 if (des.Equals("")) des = "error"; return des; } } }
3DES:
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string dealString = DES3Encrypt("bindreport", "123456788765432112345678"); Console.WriteLine("dealString is :{0}", dealString);//iTJd2yNItV6/d7v1sWnRBQ== string rawString = DES3Decrypt("iTJd2yNItV6/d7v1sWnRBQ==", "123456788765432112345678"); Console.WriteLine("rawString is :{0}", rawString);//bindreport } /// <summary> /// 3DES加密 /// </summary> /// <param name="data">要加密的数据</param> /// <param name="key">密钥</param> /// <returns>3DES加密结果</returns> public static string DES3Encrypt(string data, string key) { TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.Mode = CipherMode.ECB; DES.Padding = PaddingMode.PKCS7; ICryptoTransform DESEncrypt = DES.CreateEncryptor(); byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(data); return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); } /// <summary> /// 3DES解密 /// </summary> /// <param name="data">要解密的数据</param> /// <param name="key">密钥</param> /// <returns>3DES解密结果</returns> public static string DES3Decrypt(string data, string key) { TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.Mode = CipherMode.ECB; DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ICryptoTransform DESDecrypt = DES.CreateDecryptor(); byte[] Buffer = Convert.FromBase64String(data); return ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); } } }
RSA:
namespace ConsoleApplication1 { class Program { static void Main() { //RSA加解密 UnicodeEncoding ByteConverter = new UnicodeEncoding(); byte[] dataToEncrypt = ByteConverter.GetBytes("guwei4037"); using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { byte[] encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false); byte[] decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false); string encryptedString = Convert.ToBase64String(encryptedData); string decryptedString = ByteConverter.GetString(decryptedData); Console.WriteLine("加密后的结果为:{0}\r\n", encryptedString); Console.WriteLine("解密后的结果为:{0}\r\n", decryptedString); } //RAS数字签名 RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(); RSAParameters Key = RSAalg.ExportParameters(true); byte[] signedData = HashAndSignBytes(dataToEncrypt, Key); if (VerifySignedHash(dataToEncrypt, signedData, Key)) { Console.WriteLine("数据验证通过"); } } /// <summary> /// RSA加密 /// </summary> /// <param name="DataToEncrypt">要加密的数据</param> /// <param name="RSAKeyInfo">RSA密钥</param> /// <param name="DoOAEPPadding"></param> /// <returns></returns> static byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) { try { using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { RSA.ImportParameters(RSAKeyInfo); return RSA.Encrypt(DataToEncrypt, DoOAEPPadding); } } catch { return null; } } /// <summary> /// RSA解密 /// </summary> /// <param name="DataToDecrypt">要解密的数据</param> /// <param name="RSAKeyInfo">RSA密钥</param> /// <param name="DoOAEPPadding"></param> /// <returns></returns> static byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) { try { using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { RSA.ImportParameters(RSAKeyInfo); return RSA.Decrypt(DataToDecrypt, DoOAEPPadding); } } catch { return null; } } /// <summary> /// 进行数字签名 /// </summary> /// <param name="DataToSign">要进行数字签名的数据</param> /// <param name="Key">RAS参数</param> /// <returns>数字签名结果</returns> static byte[] HashAndSignBytes(byte[] DataToSign, RSAParameters Key) { try { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.ImportParameters(Key); return RSA.SignData(DataToSign, new SHA1CryptoServiceProvider()); } catch { return null; } } /// <summary> /// 验证数字签名 /// </summary> /// <param name="DataToVerify">要进行验证的数据</param> /// <param name="SignedData">数字签名</param> /// <param name="Key">RAS参数</param> /// <returns>验证数字签名结果</returns> static bool VerifySignedHash(byte[] DataToVerify, byte[] SignedData, RSAParameters Key) { try { RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(); RSAalg.ImportParameters(Key); return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), SignedData); } catch { return false; } } } }