Facebook
From Mustard Pintail, 5 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7.  
  8.  
  9. namespace BezpieczenstwoSystemowKomputerowych2803
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int caseSwitch = 0;
  16.             string text = "Fabian";
  17.             string afterHash = "";
  18.             do
  19.             {
  20.                 Console.WriteLine("Różne szyfrowania tekstu: {0}", text);
  21.                 Console.WriteLine("Podaj metodę szyfrowania: \n1-md5\n2-sha1\n3-sha256\n4-sha384\n5-sha512\nwyjście=0");
  22.                 int.TryParse(Console.ReadLine(), out caseSwitch);
  23.                 switch (caseSwitch)
  24.                 {
  25.                     case 1:
  26.                         {
  27.                             afterHash = md5encrypt(text);
  28.                             Console.WriteLine("MD5: {0}", afterHash);
  29.                             break;
  30.                         };
  31.                     case 2:
  32.                         {
  33.                             afterHash = sha1encrypt(text);
  34.                             Console.WriteLine("sha1: {0}", afterHash);
  35.                             break;
  36.                         };
  37.                     case 3:
  38.                         {
  39.                             afterHash = sha256encrypt(text);
  40.                             Console.WriteLine("sha256: {0}", afterHash);
  41.                             break;
  42.                         };
  43.                     case 4:
  44.                         {
  45.                             afterHash = sha384encrypt(text);
  46.                             Console.WriteLine("sha384: {0}", afterHash);
  47.                             break;
  48.                         };
  49.                     case 5:
  50.                         {
  51.                             afterHash = sha512encrypt(text);
  52.                             Console.WriteLine("sha512: {0}", afterHash);
  53.                             break;
  54.                         };
  55.                     default:
  56.                         {
  57.                             Console.WriteLine("niepoprawny wybór!");
  58.                             break;
  59.                         };
  60.                 }
  61.             } while (caseSwitch != 0);
  62.            
  63.             /*
  64.             afterHash = md5encrypt(text);
  65.             Console.WriteLine("MD5: {0}", afterHash);
  66.             afterHash = sha1encrypt(text);
  67.             Console.WriteLine("sha1: {0}", afterHash);
  68.             afterHash = sha256encrypt(text);
  69.             Console.WriteLine("sha256: {0}", afterHash);
  70.             afterHash = sha384encrypt(text);
  71.             Console.WriteLine("sha384: {0}", afterHash);
  72.             afterHash = sha512encrypt(text);
  73.             Console.WriteLine("sha512: {0}", afterHash);
  74.             */
  75.             Console.ReadKey();
  76.         }
  77.  
  78.         public static string md5encrypt(string phrase)
  79.         {
  80.             UTF8Encoding encoder = new UTF8Encoding();
  81.             MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
  82.             byte[] hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(phrase));
  83.             return byteArrayToString(hashedDataBytes);
  84.         }
  85.  
  86.         public static string sha1encrypt(string phrase)
  87.         {
  88.             UTF8Encoding encoder = new UTF8Encoding();
  89.             SHA1CryptoServiceProvider sha1hasher = new SHA1CryptoServiceProvider();
  90.             byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
  91.             return byteArrayToString(hashedDataBytes);
  92.  
  93.         }
  94.  
  95.         public static string sha256encrypt(string pharse)
  96.         {
  97.             UTF8Encoding encoder = new UTF8Encoding();
  98.             SHA256Managed sha256hasher = new SHA256Managed();
  99.             byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(pharse));
  100.             return byteArrayToString(hashedDataBytes);
  101.         }
  102.  
  103.         public static string sha384encrypt(string pharse)
  104.         {
  105.             UTF8Encoding encoder = new UTF8Encoding();
  106.             SHA384Managed sha384hasher = new SHA384Managed();
  107.             byte[] hashedDataBytes = sha384hasher.ComputeHash(encoder.GetBytes(pharse));
  108.             return byteArrayToString(hashedDataBytes);
  109.         }
  110.  
  111.         public static string sha512encrypt(string pharse)
  112.         {
  113.             UTF8Encoding encoder = new UTF8Encoding();
  114.             SHA512Managed sha512encoder = new SHA512Managed();
  115.             byte[] hashedDataBytes = sha512encoder.ComputeHash(encoder.GetBytes(pharse));
  116.             return byteArrayToString(hashedDataBytes);
  117.         }
  118.  
  119.         public static string byteArrayToString(byte[] inputArray)
  120.         {
  121.             StringBuilder output = new StringBuilder("");
  122.             for (int i = 0; i < inputArray.Length; i++)
  123.             {
  124.                 output.Append(inputArray[i].ToString("X2"));
  125.             }
  126.             return output.ToString();
  127.         }
  128.     }
  129. }
  130.