本文整理汇总了C#中System.Security.SecureString.ToArray方法的典型用法代码示例。如果您正苦于以下问题:C# SecureString.ToArray方法的具体用法?C# SecureString.ToArray怎么用?C# SecureString.ToArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.SecureString
的用法示例。
在下文中一共展示了SecureString.ToArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Open
public static MinerWallet Open(string path, SecureString password)
{
byte[] data;
byte[] iv = new byte[16];
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
fs.Read(iv, 0, iv.Length);
data = new byte[fs.Length - iv.Length];
fs.Read(data, 0, data.Length);
}
byte[] masterKey = password.ToArray().Sha256().Sha256();
using (AesManaged aes = new AesManaged())
using (ICryptoTransform decryptor = aes.CreateDecryptor(masterKey, iv))
{
return new MinerWallet(decryptor.TransformFinalBlock(data, 0, data.Length));
}
}
示例2: Save
private void Save(string path, SecureString password)
{
byte[] masterKey = password.ToArray().Sha256().Sha256();
byte[] iv = new byte[16];
byte[] data;
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
rng.GetNonZeroBytes(iv);
}
using (AesManaged aes = new AesManaged())
using (ICryptoTransform encryptor = aes.CreateEncryptor(masterKey, iv))
{
ProtectedMemory.Unprotect(key_exported, MemoryProtectionScope.SameProcess);
data = encryptor.TransformFinalBlock(key_exported, 0, key_exported.Length);
ProtectedMemory.Protect(key_exported, MemoryProtectionScope.SameProcess);
}
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
fs.Write(iv, 0, iv.Length);
fs.Write(data, 0, data.Length);
}
}