|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace Signum.Services
{
public static class Security
{
static MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
public static string EncodePassword(string originalPassword)
{
byte[] originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
byte[] encodedBytes = provider.ComputeHash(originalBytes);
return BitConverter.ToString(encodedBytes);
}
}
public class CryptorEngine
{
private string key;
public CryptorEngine(string key)
{
this.key = key;
}
byte[] GetMD5KeyHash()
{
using (MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider())
{
return hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
}
}
TripleDESCryptoServiceProvider TripleDesCryptoService()
{
return new TripleDESCryptoServiceProvider()
{
Key = GetMD5KeyHash(),
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
};
}
public string Encrypt(string text)
{
using (TripleDESCryptoServiceProvider tdes = TripleDesCryptoService())
{
byte[] toEncrypt = UTF8Encoding.UTF8.GetBytes(text);
byte[] result = tdes.CreateEncryptor().TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
return Convert.ToBase64String(result, 0, result.Length);
}
}
public string Decrypt(string encrypted)
{
using (TripleDESCryptoServiceProvider tdes = TripleDesCryptoService())
{
byte[] toDecrypt = Convert.FromBase64String(encrypted);
byte[] resultArray = tdes.CreateDecryptor().TransformFinalBlock(toDecrypt, 0, toDecrypt.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
public static string CalculateMD5Hash(string input)
{
// step 1, calculate MD5 hash from input
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
return CalculateMD5Hash(inputBytes);
}
public static string CalculateMD5Hash(byte[] inputBytes)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] hash = md5.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.
www.signumframework.com
I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...