Crypto-Notepad/Crypto Notepad/External Libraries/EncryptedString.cs

47 lines
1.3 KiB
C#
Raw Permalink Normal View History

2018-12-17 11:04:26 +00:00
using System.Text;
using System.Security.Cryptography;
2018-12-28 10:58:07 +00:00
using System;
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-28 10:58:07 +00:00
private readonly TripleDES des = TripleDES.Create();
private byte[] encryptedString = null;
2018-12-28 10:58:07 +00:00
public void Dispose() => des.Dispose();
public EncryptedString(string String)
{
2018-12-28 10:57:35 +00:00
des.GenerateIV();
des.GenerateKey();
Set(String);
}
public EncryptedString()
{
2018-12-28 10:57:35 +00:00
des.GenerateIV();
des.GenerateKey();
}
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);
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();
byte[] str = Encoding.Default.GetBytes(String);
2018-12-28 10:57:35 +00:00
encryptedString = encryptor.TransformFinalBlock(str, 0, str.Length);
}
}
}