2016-09-29 5 views
1

たとえば、パスワードは "Hello World"です。どうすればRIPEMD160ハッシュ文字列に戻すことができますか?文字列 "a830d7beb04eb7549ce990fb7dc962e499a27230"を返します。私はすでに私の質問への答えのためのインターネットを検索したが、文字列の代わりにコードはRIPEMD160にファイルを暗号化することである。C#:RIPEMD160に文字列をハッシュする方法

答えて

0

OK私はすでに問題の解決方法を知っています。文字列をバイトに変換し、RIPEMD160関数に渡し、StringBuilderを作成し、返されたRIPEMD160関数のバイトを渡します。返されたStringBuilderを文字列に変換し、再び小文字に変換します。私はそれのための関数を作成しました。以下は私のコードです:

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

namespace Password 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string thePassword = "Hello World"; 
      string theHash = getHash(thePassword); 
      Console.WriteLine("String: " + thePassword); 
      Console.WriteLine("Encrypted Hash: " + theHash); 
      Console.ReadKey(true); 
     } 

     static string getHash(string password) 
     { 
      // create a ripemd160 object 
      RIPEMD160 r160 = RIPEMD160Managed.Create(); 
      // convert the string to byte 
      byte[] myByte = System.Text.Encoding.ASCII.GetBytes(password); 
      // compute the byte to RIPEMD160 hash 
      byte[] encrypted = r160.ComputeHash(myByte); 
      // create a new StringBuilder process the hash byte 
      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < encrypted.Length; i++) 
      { 
       sb.Append(encrypted[i].ToString("X2")); 
      } 
      // convert the StringBuilder to String and convert it to lower case and return it. 
      return sb.ToString().ToLower(); 
     } 
    } 
} 
+0

'ToString(" x2 ")'を使うと、小文字に変換する必要はありません。 – Howwie

関連する問題