using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace BezpieczenstwoSystemowKomputerowych2803
{
class Program
{
static void Main(string[] args)
{
int caseSwitch = 0;
string text = "Fabian";
string afterHash = "";
do
{
Console.WriteLine("Różne szyfrowania tekstu: {0}", text);
Console.WriteLine("Podaj metodę szyfrowania: \n1-md5\n2-sha1\n3-sha256\n4-sha384\n5-sha512\nwyjście=0");
int.TryParse(Console.ReadLine(), out caseSwitch);
switch (caseSwitch)
{
case 1:
{
afterHash = md5encrypt(text);
Console.WriteLine("MD5: {0}", afterHash);
break;
};
case 2:
{
afterHash = sha1encrypt(text);
Console.WriteLine("sha1: {0}", afterHash);
break;
};
case 3:
{
afterHash = sha256encrypt(text);
Console.WriteLine("sha256: {0}", afterHash);
break;
};
case 4:
{
afterHash = sha384encrypt(text);
Console.WriteLine("sha384: {0}", afterHash);
break;
};
case 5:
{
afterHash = sha512encrypt(text);
Console.WriteLine("sha512: {0}", afterHash);
break;
};
default:
{
Console.WriteLine("niepoprawny wybór!");
break;
};
}
} while (caseSwitch != 0);
/*
afterHash = md5encrypt(text);
Console.WriteLine("MD5: {0}", afterHash);
afterHash = sha1encrypt(text);
Console.WriteLine("sha1: {0}", afterHash);
afterHash = sha256encrypt(text);
Console.WriteLine("sha256: {0}", afterHash);
afterHash = sha384encrypt(text);
Console.WriteLine("sha384: {0}", afterHash);
afterHash = sha512encrypt(text);
Console.WriteLine("sha512: {0}", afterHash);
*/
Console.ReadKey();
}
public static string md5encrypt(string phrase)
{
UTF8Encoding encoder
= new UTF8Encoding
();
MD5CryptoServiceProvider md5hasher
= new MD5CryptoServiceProvider
();
byte[] hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}
public static string sha1encrypt(string phrase)
{
UTF8Encoding encoder
= new UTF8Encoding
();
SHA1CryptoServiceProvider sha1hasher
= new SHA1CryptoServiceProvider
();
byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}
public static string sha256encrypt(string pharse)
{
UTF8Encoding encoder
= new UTF8Encoding
();
SHA256Managed sha256hasher
= new SHA256Managed
();
byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(pharse));
return byteArrayToString(hashedDataBytes);
}
public static string sha384encrypt(string pharse)
{
UTF8Encoding encoder
= new UTF8Encoding
();
SHA384Managed sha384hasher
= new SHA384Managed
();
byte[] hashedDataBytes = sha384hasher.ComputeHash(encoder.GetBytes(pharse));
return byteArrayToString(hashedDataBytes);
}
public static string sha512encrypt(string pharse)
{
UTF8Encoding encoder
= new UTF8Encoding
();
SHA512Managed sha512encoder
= new SHA512Managed
();
byte[] hashedDataBytes = sha512encoder.ComputeHash(encoder.GetBytes(pharse));
return byteArrayToString(hashedDataBytes);
}
public static string byteArrayToString(byte[] inputArray)
{
StringBuilder output
= new StringBuilder
("");
for (int i = 0; i < inputArray.Length; i++)
{
output.Append(inputArray[i].ToString("X2"));
}
return output.ToString();
}
}
}