2018-12-17 11:04:26 +00:00
|
|
|
|
using System.Text;
|
2018-12-01 02:29:19 +00:00
|
|
|
|
using System.Security.Cryptography;
|
2018-12-28 10:58:07 +00:00
|
|
|
|
using System;
|
2018-12-01 02:29:19 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Crypto_Notepad
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Stores a string encrypted in memory to defend against memory dumps
|
|
|
|
|
|
/// </summary>
|
2018-12-28 10:58:07 +00:00
|
|
|
|
class EncryptedString: IDisposable
|
2018-12-01 02:29:19 +00:00
|
|
|
|
{
|
2018-12-28 10:58:07 +00:00
|
|
|
|
private readonly TripleDES des = TripleDES.Create();
|
2018-12-01 02:29:19 +00:00
|
|
|
|
private byte[] encryptedString = null;
|
|
|
|
|
|
|
2018-12-28 10:58:07 +00:00
|
|
|
|
public void Dispose() => des.Dispose();
|
|
|
|
|
|
|
2018-12-01 02:29:19 +00:00
|
|
|
|
public EncryptedString(string String)
|
|
|
|
|
|
{
|
2018-12-28 10:57:35 +00:00
|
|
|
|
des.GenerateIV();
|
|
|
|
|
|
des.GenerateKey();
|
|
|
|
|
|
Set(String);
|
2018-12-01 02:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public EncryptedString()
|
|
|
|
|
|
{
|
2018-12-28 10:57:35 +00:00
|
|
|
|
des.GenerateIV();
|
|
|
|
|
|
des.GenerateKey();
|
2018-12-01 02:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string Get()
|
|
|
|
|
|
{
|
2018-12-28 10:57:35 +00:00
|
|
|
|
if (encryptedString == null) return null;
|
|
|
|
|
|
var decryptor = des.CreateDecryptor();
|
|
|
|
|
|
byte[] output = decryptor.TransformFinalBlock(encryptedString, 0, encryptedString.Length);
|
2018-12-01 02:29:19 +00:00
|
|
|
|
return Encoding.Default.GetString(output);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Set(string String)
|
|
|
|
|
|
{
|
2018-12-28 10:57:35 +00:00
|
|
|
|
if (String == null) { encryptedString = null; return; }
|
|
|
|
|
|
var encryptor = des.CreateEncryptor();
|
2018-12-01 02:29:19 +00:00
|
|
|
|
byte[] str = Encoding.Default.GetBytes(String);
|
2018-12-28 10:57:35 +00:00
|
|
|
|
encryptedString = encryptor.TransformFinalBlock(str, 0, str.Length);
|
2018-12-01 02:29:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|