2015-05-08 96 views
0

我有一個包含用戶窗體的WPF應用程序。我有一個密碼字段,我需要decript它,我只是有這個一個加密方法:在WPF應用程序中編碼和解碼密碼

public static string Encode(string value) 
    { 
     var hash = System.Security.Cryptography.SHA1.Create(); 
     var encoder = new System.Text.ASCIIEncoding(); 
     var combined = encoder.GetBytes(value ?? ""); 
     return BitConverter.ToString(hash.ComputeHash(combined)).ToLower().Replace("-", ""); 

    } 

如何創建這樣的解碼方法?

+0

你要做的是獲得你的字符串密碼,並通過相同的加密,然後比較存儲在數據庫中的內容 –

回答

0

您不能解密SHA1哈希,因爲它是單向哈希。散列並非可逆操作。

下面一個輔助類,加密和decrypte字符串

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Security.Cryptography; 
using System.Text; 
using System.Threading.Tasks; 

namespace EncryptStringSample 
{ 
    public static class StringCipher 
    { 
     // This constant string is used as a "salt" value for the PasswordDeriveBytes function calls. 
     // This size of the IV (in bytes) must = (keysize/8). Default keysize is 256, so the IV must be 
     // 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array. 
     private static readonly byte[] initVectorBytes = Encoding.ASCII.GetBytes("tu89geji340t89u2"); 

     // This constant is used to determine the keysize of the encryption algorithm. 
     private const int keysize = 256; 

     public static string Encrypt(string plainText, string passPhrase) 
     { 
      byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); 
      using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null)) 
      { 
       byte[] keyBytes = password.GetBytes(keysize/8); 
       using (RijndaelManaged symmetricKey = new RijndaelManaged()) 
       { 
        symmetricKey.Mode = CipherMode.CBC; 
        using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)) 
        { 
         using (MemoryStream memoryStream = new MemoryStream()) 
         { 
          using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) 
          { 
           cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); 
           cryptoStream.FlushFinalBlock(); 
           byte[] cipherTextBytes = memoryStream.ToArray(); 
           return Convert.ToBase64String(cipherTextBytes); 
          } 
         } 
        } 
       } 
      } 
     } 

     public static string Decrypt(string cipherText, string passPhrase) 
     { 
      byte[] cipherTextBytes = Convert.FromBase64String(cipherText); 
      using(PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null)) 
      { 
       byte[] keyBytes = password.GetBytes(keysize/8); 
       using(RijndaelManaged symmetricKey = new RijndaelManaged()) 
       { 
        symmetricKey.Mode = CipherMode.CBC; 
        using(ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)) 
        { 
         using(MemoryStream memoryStream = new MemoryStream(cipherTextBytes)) 
         { 
          using(CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) 
          { 
           byte[] plainTextBytes = new byte[cipherTextBytes.Length]; 
           int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); 
           return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

上面的類可以用類似下面的代碼很簡單使用:

using System; 
using System.Linq; 

namespace EncryptStringSample 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Please enter a password to use:"); 
      string password = Console.ReadLine(); 
      Console.WriteLine("Please enter a string to encrypt:"); 
      string plaintext = Console.ReadLine(); 
      Console.WriteLine(""); 

      Console.WriteLine("Your encrypted string is:"); 
      string encryptedstring = StringCipher.Encrypt(plaintext, password); 
      Console.WriteLine(encryptedstring); 
      Console.WriteLine(""); 

      Console.WriteLine("Your decrypted string is:"); 
      string decryptedstring = StringCipher.Decrypt(encryptedstring, password); 
      Console.WriteLine(decryptedstring); 
      Console.WriteLine(""); 

      Console.ReadLine(); 
     } 
    } 
} 

這裏是另一種方式來解密和加密使用RSA

用您的RSA密鑰替換your_rsa_key。

var provider = new System.Security.Cryptography.RSACryptoServiceProvider(); 
    provider.ImportParameters(your_rsa_key); 

    var encryptedBytes = provider.Encrypt(
     System.Text.Encoding.UTF8.GetBytes("Hello World!"), true); 

    string decryptedTest = System.Text.Encoding.UTF8.GetString(
     provider.Decrypt(encryptedBytes, true)); 

下面是關於如何RSA作品帶有明顯的〔實施例

+0

這個RSA密鑰的工作原理是什麼? –

+0

在你的第一個例子上面如何獲取StringCipher.Decrypt(encryptedstring,password);來自另一個班級? –

+0

我不明白 –

1

一個很好的鏈接在這種情況下,關鍵是,你不想來解密密碼。你想要做的是加密用戶使用相同功能輸入的密碼,並將結果與​​存儲在用戶帳戶數據庫中的結果進行比較。

1

散列是做正確的事,但是你也應該散列你存儲在數據庫中的值,並且比較這兩個哈希,如果可能的話,你不應該存儲密碼,即使它們是加密的。

1

您應該將單向散列存儲在數據庫中而不是可逆加密。用戶的真實密碼碰巧不是你關心的東西,只是用戶提供的密碼哈希值與數據庫中的哈希值相匹配。但是,您可以對其進行加密,以便在服務器上解密傳輸,然後散列存儲在數據庫中。

  1. 從數據庫中檢索鹽和哈希密碼。
  2. 給出候選人密碼並將其散列。
  3. 比較值。