Repository navigation
Expand file tree
/
Copy pathEncryptionService.cs
More file actions
131 lines (112 loc) · 5.04 KB
/
Copy pathEncryptionService.cs
File metadata and controls
131 lines (112 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;
using SmartStore.Core.Domain.Security;
namespace SmartStore.Services.Security
{
public class EncryptionService : IEncryptionService
{
private readonly SecuritySettings _securitySettings;
public EncryptionService(SecuritySettings securitySettings)
{
this._securitySettings = securitySettings;
}
/// <summary>
/// Create salt key
/// </summary>
/// <param name="size">Key size</param>
/// <returns>Salt key</returns>
public virtual string CreateSaltKey(int size)
{
// Generate a cryptographic random number
var rng = new RNGCryptoServiceProvider();
var buff = new byte[size];
rng.GetBytes(buff);
// Return a Base64 string representation of the random number
return Convert.ToBase64String(buff);
}
/// <summary>
/// Create a password hash
/// </summary>
/// <param name="password">{assword</param>
/// <param name="saltkey">Salk key</param>
/// <param name="passwordFormat">Password format (hash algorithm)</param>
/// <returns>Password hash</returns>
public virtual string CreatePasswordHash(string password, string saltkey, string passwordFormat = "SHA1")
{
if (String.IsNullOrEmpty(passwordFormat))
passwordFormat = "SHA1";
string saltAndPassword = String.Concat(password, saltkey);
//return FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPassword, passwordFormat);
var algorithm = HashAlgorithm.Create(passwordFormat);
if (algorithm == null)
throw new ArgumentException("Unrecognized hash name", "hashName");
var hashByteArray = algorithm.ComputeHash(Encoding.UTF8.GetBytes(saltAndPassword));
return BitConverter.ToString(hashByteArray).Replace("-", "");
}
/// <summary>
/// Encrypt text
/// </summary>
/// <param name="plainText">Text to encrypt</param>
/// <param name="encryptionPrivateKey">Encryption private key</param>
/// <returns>Encrypted text</returns>
public virtual string EncryptText(string plainText, string encryptionPrivateKey = "")
{
if (string.IsNullOrEmpty(plainText))
return plainText;
if (String.IsNullOrEmpty(encryptionPrivateKey))
encryptionPrivateKey = _securitySettings.EncryptionKey;
var tDESalg = new TripleDESCryptoServiceProvider();
tDESalg.Key = new ASCIIEncoding().GetBytes(encryptionPrivateKey.Substring(0, 16));
tDESalg.IV = new ASCIIEncoding().GetBytes(encryptionPrivateKey.Substring(8, 8));
byte[] encryptedBinary = EncryptTextToMemory(plainText, tDESalg.Key, tDESalg.IV);
return Convert.ToBase64String(encryptedBinary);
}
/// <summary>
/// Decrypt text
/// </summary>
/// <param name="cipherText">Text to decrypt</param>
/// <param name="encryptionPrivateKey">Encryption private key</param>
/// <returns>Decrypted text</returns>
public virtual string DecryptText(string cipherText, string encryptionPrivateKey = "")
{
if (String.IsNullOrEmpty(cipherText))
return cipherText;
if (String.IsNullOrEmpty(encryptionPrivateKey))
encryptionPrivateKey = _securitySettings.EncryptionKey;
var tDESalg = new TripleDESCryptoServiceProvider();
tDESalg.Key = new ASCIIEncoding().GetBytes(encryptionPrivateKey.Substring(0, 16));
tDESalg.IV = new ASCIIEncoding().GetBytes(encryptionPrivateKey.Substring(8, 8));
byte[] buffer = Convert.FromBase64String(cipherText);
return DecryptTextFromMemory(buffer, tDESalg.Key, tDESalg.IV);
}
#region Utilities
private byte[] EncryptTextToMemory(string data, byte[] key, byte[] iv)
{
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, new TripleDESCryptoServiceProvider().CreateEncryptor(key, iv), CryptoStreamMode.Write))
{
byte[] toEncrypt = new UnicodeEncoding().GetBytes(data);
cs.Write(toEncrypt, 0, toEncrypt.Length);
cs.FlushFinalBlock();
}
return ms.ToArray();
}
}
private string DecryptTextFromMemory(byte[] data, byte[] key, byte[] iv)
{
using (var ms = new MemoryStream(data))
{
using (var cs = new CryptoStream(ms, new TripleDESCryptoServiceProvider().CreateDecryptor(key, iv), CryptoStreamMode.Read))
{
var sr = new StreamReader(cs, new UnicodeEncoding());
return sr.ReadLine();
}
}
}
#endregion
}
}