幾種常見的加密方法的實現 1.ACSII碼加密 //ACSII碼加密 private static string ACSIIPWd(string rpwd) { string Ret; byte[] array = System.Text.Encoding.ASCII.GetBytes(rpwd); ...
幾種常見的加密方法的實現
1.ACSII碼加密
//ACSII碼加密 private static string ACSIIPWd(string rpwd) { string Ret; byte[] array = System.Text.Encoding.ASCII.GetBytes(rpwd); byte[] byteArray = new byte[array.Length]; for (int i = 0; i < array.Length; i++) { int asciicode = (int)(array[i]); asciicode = asciicode + 1; byteArray[i] = (byte)asciicode; } System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); string strCharacter = asciiEncoding.GetString(byteArray); Ret = strCharacter; return Ret; }
2.MD5加密
//MD5加密 private static string MD5PWd(string rpwd) { string Ret; MD5 md5 = new MD5CryptoServiceProvider(); byte[] palindata = Encoding.Default.GetBytes(rpwd);//將要加密的字元串轉換為位元組數組 byte[] encryptdata = md5.ComputeHash(palindata);//將字元串加密後也轉換為字元數組 Ret = Convert.ToBase64String(encryptdata); return Ret; }
3.RSA加密
//RSA加密 private static string RSAPWD1(string myKeyContainerName) { string ret = ""; CspParameters cp = new CspParameters(); cp.KeyContainerName = myKeyContainerName; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp); ret = rsa.ToXmlString(true); Console.WriteLine("Key is : \n" + rsa.ToXmlString(true)); return ret; }
4.DES加密
//DES加密 private static string DESPWD(string ciphertext) { string desPWD = ""; byte[] buffer; DESCryptoServiceProvider DesCSP = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream();//先創建 一個記憶體流 CryptoStream cryStream = new CryptoStream(ms, DesCSP.CreateEncryptor(), CryptoStreamMode.Write);//將記憶體流連接到加密轉換流 StreamWriter sw = new StreamWriter(cryStream); sw.WriteLine(ciphertext);//將要加密的字元串寫入加密轉換流 sw.Close(); cryStream.Close(); buffer = ms.ToArray();//將加密後的流轉換為位元組數組 desPWD = Convert.ToBase64String(buffer);//將加密後的位元組數組轉換為字元串 return desPWD; }