using System.Globalization;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// SecureString 的基底是 ProtectedData 類別。
/// 因為用 Uincode 進行編解碼故不適用中文字。
/// </summary>
internal class SecureStringService
{
public string Decrypt(string encryptedText)
{
// Remove all new-lines
encryptedText = encryptedText.Replace(Environment.NewLine, "");
// 解開 Protected SecureString
string decryptedText = DoUpprotectText(encryptedText);
return decryptedText;
}
public string Encrypt(string encryptedText)
{
string cypherText = DoProtectText(encryptedText);
return cypherText;
}
/// <summary>
/// 說明:SecureString 的基底是 ProtectedData 類別。
/// </summary>
static string DoUpprotectText(string cypherText)
{
// Convert the hex dump to byte array
int length = cypherText.Length / 2;
byte[] encryptedData = new byte[length];
for (int index = 0; index < length; ++index)
{
var chunk = cypherText.Substring(2 * index, 2);
encryptedData[index] = byte.Parse(chunk, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
byte[] data = ProtectedData.Unprotect(encryptedData, null, DataProtectionScope.LocalMachine);
string unprotectedText = Encoding.Unicode.GetString(data);
return unprotectedText;
}
/// <summary>
/// 說明:SecureString 的基底是 ProtectedData 類別。
/// </summary>
static string DoProtectText(string plainText)
{
byte[] plainBlob = Encoding.Unicode.GetBytes(plainText);
byte[] cypherBlob = ProtectedData.Protect(plainBlob, null, DataProtectionScope.LocalMachine);
string cypherText = Convert.ToHexString(cypherBlob);
return cypherText;
}
}