تغییرات رمزنگاری اطلاعات در NET Core.
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۰۷/۱۷ ۱۲:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using (var sha256 = new SHA256Managed())
{
// Crypto code here...
} public static string GetHash(string text)
{
using (var sha256 = SHA256.Create())
{
var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(text));
return BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
}
} "dependencies": {
"System.Security.Cryptography.Algorithms": "4.2.0"
}, using (var md5 = IncrementalHash.CreateHash(HashAlgorithmName.MD5))
{
md5.AppendData(byteArray1, 0, byteArray1.Length);
md5.AppendData(byteArray2, 0, byteArray2.Length);
var hash = md5.GetHashAndReset();
} private static readonly RNGCryptoServiceProvider Rand = new RNGCryptoServiceProvider();
var rand = new RNGCryptoServiceProvider();
public interface IRandomNumberProvider
{
int Next();
int Next(int max);
int Next(int min, int max);
}
public class RandomNumberProvider : IRandomNumberProvider
{
private readonly RandomNumberGenerator _rand = RandomNumberGenerator.Create();
public int Next()
{
var randb = new byte[4];
_rand.GetBytes(randb);
var value = BitConverter.ToInt32(randb, 0);
if (value < 0) value = -value;
return value;
}
public int Next(int max)
{
var randb = new byte[4];
_rand.GetBytes(randb);
var value = BitConverter.ToInt32(randb, 0);
value = value % (max + 1); // % calculates remainder
if (value < 0) value = -value;
return value;
}
public int Next(int min, int max)
{
var value = Next(max - min) + min;
return value;
}
} public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.TryAddSingleton<IRandomNumberProvider, RandomNumberProvider>(); public interface IProtectionProvider
{
string Decrypt(string inputText);
string Encrypt(string inputText);
}
namespace Providers
{
public class ProtectionProvider : IProtectionProvider
{
private readonly IDataProtector _dataProtector;
public ProtectionProvider(IDataProtectionProvider dataProtectionProvider)
{
_dataProtector = dataProtectionProvider.CreateProtector(typeof(ProtectionProvider).FullName);
}
public string Decrypt(string inputText)
{
var inputBytes = Convert.FromBase64String(inputText);
var bytes = _dataProtector.Unprotect(inputBytes);
return Encoding.UTF8.GetString(bytes);
}
public string Encrypt(string inputText)
{
var inputBytes = Encoding.UTF8.GetBytes(inputText);
var bytes = _dataProtector.Protect(inputBytes);
return Convert.ToBase64String(bytes);
}
}
} public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.TryAddSingleton<IProtectionProvider, ProtectionProvider>(); public string Decrypt(string inputText, string key, string salt)
{
var inputBytes = Convert.FromBase64String(inputText);
var pdb = new Rfc2898DeriveBytes(key, Encoding.UTF8.GetBytes(salt));
using (var ms = new MemoryStream())
{
var alg = Aes.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
using (var cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputBytes, 0, inputBytes.Length);
}
return Encoding.UTF8.GetString(ms.ToArray());
}
}
public string Encrypt(string inputText, string key, string salt)
{
var inputBytes = Encoding.UTF8.GetBytes(inputText);
var pdb = new Rfc2898DeriveBytes(key, Encoding.UTF8.GetBytes(salt));
using (var ms = new MemoryStream())
{
var alg = Aes.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
using (var cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputBytes, 0, inputBytes.Length);
}
return Convert.ToBase64String(ms.ToArray());
}
} PM> Install-Package DNTCommon.Web.Core
var serviceCollection = new ServiceCollection(); serviceCollection.AddDataProtection(); var services = serviceCollection.BuildServiceProvider(); var protectionProvider = ActivatorUtilities.CreateInstance<ProtectionProvider>(services); string result = protectionProvider.Encrypt(Str);
public int GetRandomNumber() => RandomNumberGenerator.GetInt32(1, 1000000);
byte[] randomBytes = RandomNumberGenerator.GetBytes(100);
using System.Security.Cryptography;
byte[] data = default; // Some data
using (SHA256 hash = SHA256.Create())
{
byte[] digest = hash.ComputeHash(data);
} using System.Security.Cryptography; byte[] data = default; // Some data byte[] digest = SHA256.HashData(data);
using System.Security.Cryptography;
byte[] salt = RandomNumberGenerator.GetBytes(32);
byte[] prk = Rfc2898DeriveBytes.Pbkdf2(
userPassword,
salt,
iterations: 200_000,
HashAlgorithmName.SHA256,
outputLength: 32); var inputBytes = Convert.FromBase64String(inputText); var bytes = _dataProtector.Unprotect(inputBytes); return Encoding.UTF8.GetString(bytes);