· 8 years ago · Nov 15, 2017, 10:04 AM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Security.Cryptography;
7
8namespace Common
9{
10 public class Encryption
11 {
12 public string HashString(string mytext)
13 {
14 SHA256 myAlg = SHA256.Create(); //initialize the algorithm instance
15
16 byte[] input = Encoding.UTF32.GetBytes(mytext); //converting from string to byte[]
17
18 byte[] digest = myAlg.ComputeHash(input); //hashing byte[] >> base64 bytes
19
20 return Convert.ToBase64String(digest); //converting back from byte[] to string
21 }
22
23 public SymmetricParameters GenerateSymmetricParameters(string input)
24 {
25 Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(input, new byte[] { 20, 240, 69, 1, 50, 120});
26
27 Rijndael myAlg = Rijndael.Create();
28
29 SymmetricParameters myParams = new SymmetricParameters()
30 {
31 SecretKey = rfc.GetBytes(myAlg.KeySize / 8),
32 IV = rfc.GetBytes(myAlg.BlockSize / 8)
33 };
34
35 return myParams;
36 }
37
38 public string EncryptString(string input, string passwordToBeUsedInSecurity)
39 {
40 SymmetricParameters myParameters = GenerateSymmetricParameters(passwordToBeUsedInSecurity);
41
42 Rijndael myAlg = Rijndael.Create();
43
44 byte[] clearDataAsBytes = Encoding.UTF32.GetBytes(input);
45 }
46 }
47
48 public class SymmetricParameters
49 {
50 public byte[] SecretKey { get; set; }
51 public byte[] IV { get; set; }
52 }
53}