如何解决保存在 txt 文件中后,字节对于“取消保护”功能无效
我想将编码的用户凭据保存在 txt 文件中以备后用,并在用户注销时删除此文件。 这是我用于加密数据并将其写入 txt 文件的代码:
// encrypt pw and username
Byte[] bUser = new UTF8Encoding(true).GetBytes(User);
Byte[] bPw = new UTF8Encoding(true).GetBytes(Pw);
// Generate additional entropy (will be used as the Initialization vector)
byte[] entropy = new byte[20];
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(entropy);
}
byte[] cipheruser = ProtectedData.Protect(bUser,entropy,DataProtectionScope.CurrentUser);
byte[] cipherpw = ProtectedData.Protect(bPw,DataProtectionScope.CurrentUser);
//save pw and Username in file for later Use
// Create a new file
File.WriteAllBytes(UserNameFile,cipheruser);
File.WriteAllBytes(PwFile,cipherpw);
但是当我想解密数据时,我收到错误“数据无效”,尽管它在调试器中的外观与保存在 txt 文件中之前的方式相同。 这是我的解密代码:
byte[] entropy = new byte[20];
byte[] Userlines = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + @"LoginDataUserName.txt");
byte[] username = ProtectedData.Unprotect(Userlines,DataProtectionScope.CurrentUser);
byte[] Pwlines = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + @"LoginDataPassWord.txt");
byte[] password = ProtectedData.Unprotect(Pwlines,DataProtectionScope.CurrentUser);
字节数据是否通过保存以某种方式改变了?是否可以避免或者是否有更好的方法来存储加密的用户凭据? 感谢您的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。