Winify

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 13  →  ?path2? @ 14
/trunk/Winify/Utilities/AES.cs
@@ -0,0 +1,97 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
 
using System.IO;
using System.Linq;
using System.Security.Cryptography;
 
namespace Winify.Utilities
{
public static class AES
{
#region Static Fields and Constants
 
private const int AESKeySaltBytes = 16;
 
private static readonly RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider();
 
#endregion
 
#region Public Methods
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
public static byte[] Encrypt(byte[] plain, string password)
{
using (var rijndael = Rijndael.Create())
{
rijndael.BlockSize = 128;
rijndael.Mode = CipherMode.CBC;
rijndael.KeySize = 256;
rijndael.Padding = PaddingMode.PKCS7;
var salt = new byte[AESKeySaltBytes];
Rng.GetBytes(salt);
var pdb = new Rfc2898DeriveBytes(password, salt, 1000);
rijndael.Key = pdb.GetBytes(rijndael.KeySize / 8);
rijndael.IV = pdb.GetBytes(rijndael.BlockSize / 8);
 
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream =
new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(plain, 0, plain.Length);
cryptoStream.Close();
 
var ciphertext = memoryStream.ToArray();
 
return salt.Concat(ciphertext)
.ToArray();
}
}
}
}
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
public static byte[] Decrypt(byte[] cipher, string password)
{
using (var rijndael = Rijndael.Create())
{
rijndael.BlockSize = 128;
rijndael.Mode = CipherMode.CBC;
rijndael.KeySize = 256;
rijndael.Padding = PaddingMode.PKCS7;
 
var salt = cipher.Take(16)
.ToArray();
 
var ciphertext = cipher.Skip(16)
.ToArray();
 
var pdb = new Rfc2898DeriveBytes(password, salt, 1000);
rijndael.Key = pdb.GetBytes(rijndael.KeySize / 8);
rijndael.IV = pdb.GetBytes(rijndael.BlockSize / 8);
 
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream =
new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(ciphertext, 0, ciphertext.Length);
cryptoStream.Close();
 
return memoryStream.ToArray();
}
}
}
}
 
#endregion
}
}
/trunk/Winify/Utilities/Miscellaneous.cs
@@ -164,6 +164,39 @@
}
}
 
/// <summary>
/// Returns the machine GUID.
/// </summary>
/// <returns>the GUID of the machine</returns>
/// <remarks>https://stackoverflow.com/questions/2333149/how-to-fast-get-hardware-id-in-c</remarks>
public static string GetMachineGuid()
{
var location = @"SOFTWARE\Microsoft\Cryptography";
var name = "MachineGuid";
 
using (var localMachineX64View =
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var rk = localMachineX64View.OpenSubKey(location))
{
if (rk == null)
{
throw new KeyNotFoundException(
string.Format("Key Not Found: {0}", location));
}
 
var machineGuid = rk.GetValue(name);
if (machineGuid == null)
{
throw new IndexOutOfRangeException(
string.Format("Index Not Found: {0}", name));
}
 
return machineGuid.ToString();
}
}
}
 
#endregion
}
}