这可以简化为一个班轮吗?只要secureString被正确初始化,就可以完全重写它.
SecureString secureString = new SecureString ();
foreach (char c in "fizzbuzz".ToCharArray())
{
secureString.AppendChar (c);
}
Run Code Online (Sandbox Code Playgroud) 假设我需要在Powershell中执行此操作:
$SecurePass = Get-Content $CredPath | ConvertTo-SecureString -Key (1..16)
[String]$CleartextPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($CredPass));
Run Code Online (Sandbox Code Playgroud)
$ CredPath的内容是一个包含ConvertFrom-SecureString -Key(1..16)输出的文件.
如何ConvertTo-SecureString -key (1..16)
在C#/ .NET中完成该部分?
我知道如何创建SecureString
,但我不确定应该如何处理加密.
我是否使用AES加密每个字符,或者解密字符串然后为每个字符创建一个安全字符串?
我对密码学几乎一无所知,但从我收集的内容中我可能只想使用C#调用Powershell命令.
作为参考,我在这里发现了类似的关于AES加密/解密的帖子: 在C#中使用AES加密
UPDATE
我已经回顾了Keith发布的链接,但我还面临其他未知数.DecryptStringFromBytes_Aes有三个参数:
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
Run Code Online (Sandbox Code Playgroud)
第一个参数是字节数组表示加密文本.这里的问题是,如何在字节数组中表示字符串?它应该用或不用编码表示?
byte[] ciphertext = Encoding.ASCII.GetBytes(encrypted_text);
byte[] ciphertext = Encoding.UTF8.GetBytes(encrypted_text);
byte[] ciphertext = Encoding.Unicode.GetBytes(encrypted_text);
byte[] ciphertext = new byte[encrypted_password.Length * sizeof(char)];
System.Buffer.BlockCopy(encrypted_password.ToCharArray(), 0, text, 0, text.Length);
Run Code Online (Sandbox Code Playgroud)
第二个字节数组的键应该只是一个整数数组:
byte[] key = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
Run Code Online (Sandbox Code Playgroud)
第三个字节数组是"初始化向量" - 看起来Aes.Create()调用将随机生成一个用于IV的byte [].阅读,我发现我可能需要使用相同的IV.由于ConvertFrom-SecureString和ConvertTo-SecureString能够使用简单的密钥加密/解密,因此我假设IV []可以是随机的 - 或者 - 具有静态定义. …