using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Diagnostics;
namespace JiaMi
{
class Program
{
static void Main(string[] args)
{
const string dataToProject = "This is abunch of super secret content!";
var dataToProtectAsArray = Encoding.Unicode.GetBytes(dataToProject);
#region file.Encrypt 文件保护
//var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyDataFile.txt");
//File.WriteAllText(fileName, dataToProject);
//File.Encrypt(fileName);
#endregion
#region Window Data of Protection 系统保护
//var wdpEncryptedData = ProtectedData.Protect(dataToProtectAsArray, null, DataProtectionScope.CurrentUser);
//var wdpUnEncryptedData = ProtectedData.Unprotect(wdpEncryptedData, null, DataProtectionScope.CurrentUser);
//var wdunencryptedString = Encoding.Unicode.GetString(wdpUnEncryptedData);
//Debug.Assert(dataToProject.Equals(wdunencryptedString));
#endregion
#region Hashing
//var storedPasswordHash = new byte[]
// {
// 148,152,235,242,51,18,100,176,51,147,
// 249,128,175,164,106,204,48,17,154,75,
// 82,83,170,111,8,107,51,13,83,2,252
// };
//var password = Encoding.Unicode.GetBytes("P4sswOrd!");
//var passwordHash = SHA256.Create().ComputeHash(password);
//if (passwordHash.SequenceEqual(storedPasswordHash))
//{
// Console.WriteLine("1");
//}
#endregion
#region RAS加密
var key = new byte[] { 12, 2, 56, 117, 12, 67, 33, 23, 12, 2, 56, 117, 12, 67, 33, 23 };
var initializationVector = new byte[] { 37, 99, 102, 12, 22, 156, 204, 11, 12, 23, 44, 55, 1, 157, 233,1 };
byte[] symEncryptedData;
var algorithm = Aes.Create();
using (var encrypor = algorithm.CreateEncryptor(key, initializationVector))
using (var memoryStream = new MemoryStream())
using (var crypoStream = new CryptoStream(memoryStream, encrypor, CryptoStreamMode.Write))
{
crypoStream.Write(dataToProtectAsArray, 0, dataToProtectAsArray.Length);
crypoStream.FlushFinalBlock();
symEncryptedData = memoryStream.ToArray();
}
byte[] symUnencryptedData;
using (var decryptor = algorithm.CreateDecryptor(key, initializationVector))
using (var memoryStream = new MemoryStream())
using (var crypoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write))
{
crypoStream.Write(symEncryptedData, 0, symEncryptedData.Length);
crypoStream.FlushFinalBlock();
symUnencryptedData = memoryStream.ToArray();
}
algorithm.Dispose();
var x = dataToProtectAsArray.SequenceEqual(symUnencryptedData);
#endregion
}
}
}