小编use*_*632的帖子

在 C# 中将 Aes.Key 转换为 SecureString

如何将 Aes.Key 转换为 secureString ?我正在做一个字节 [] -> 字符串 -> 安全字符串。我正面临一个不同的问题。将字节 [] 中的密钥转换为字符串并返回到字节 [] 时,我得到了不同的字节 []。代码有什么问题?

Aes aes = Aes.Create();
aes.GenerateIV();
aes.GenerateKey();

byte[] byteKey1 = aes.Key; 

string sKey = Encoding.UniCode.GetString(byteKey);
byte[] byteKey2= Encoding.UniCode.GetBytes(sKey);
Run Code Online (Sandbox Code Playgroud)

“byteKey1”和“byteKey2”有时是不同的。如果我使用 Encoding.Default,它们是相等的,但是当不同的机器具有不同的默认编码时会出现问题。

如何将 byte[] 中的 Key 转换为 SecureString 并返回到 byte[] ?

谢谢。

c# encryption bytearray securestring

0
推荐指数
1
解决办法
3163
查看次数

标签 统计

bytearray ×1

c# ×1

encryption ×1

securestring ×1