- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写密码加密例程。我写了下面的应用程序来说明我的问题。大约 20% 的时间,此代码按预期工作。其余时间,解密会抛出加密异常 - “数据无效”。
我认为问题出在加密部分,因为解密部分每次都一样。也就是说,如果加密例程产生一个解密例程可以解密的值,它总能解密它。但是,如果加密例程产生一个阻塞解密例程的值,它就会一直阻塞。所以解密例程是一致的;加密例程不是。
我怀疑我对 Unicode 编码的使用不正确,但我已经尝试过其他的结果相同。
我做错了什么?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace DataProtectionTest
{
public partial class Form1 : Form
{
private static readonly byte[] entropy = { 1, 2, 3, 4, 1, 2, 3, 4 };
private string password;
public Form1()
{
InitializeComponent();
}
private void btnEncryptIt_Click(object sender, EventArgs e)
{
Byte[] pw = Encoding.Unicode.GetBytes(textBox1.Text);
Byte[] encryptedPw = ProtectedData.Protect(pw, entropy, DataProtectionScope.LocalMachine);
password = Encoding.Unicode.GetString(encryptedPw);
}
private void btnDecryptIt_Click(object sender, EventArgs e)
{
Byte[] pwBytes = Encoding.Unicode.GetBytes(password);
try
{
Byte[] decryptedPw = ProtectedData.Unprotect(pwBytes, entropy, DataProtectionScope.LocalMachine);
string pw = Encoding.Unicode.GetString(decryptedPw);
textBox2.Text = pw;
}
catch (CryptographicException ce)
{
textBox2.Text = ce.Message;
}
}
}
}
最佳答案
在同事的建议下,我选择了 Convert.ToBase64String。效果很好。更正了下面的程序。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace DataProtectionTest
{
public partial class Form1 : Form
{
private static readonly byte[] entropy = { 1, 2, 3, 4, 1, 2, 3, 4 };
private string password;
public Form1()
{
InitializeComponent();
}
private void btnEncryptIt_Click(object sender, EventArgs e)
{
Byte[] pw = Encoding.Unicode.GetBytes(textBox1.Text);
Byte[] encryptedPw = ProtectedData.Protect(pw, entropy, DataProtectionScope.LocalMachine);
//password = Encoding.Unicode.GetString(encryptedPw);
password = Convert.ToBase64String(encryptedPw);
}
private void btnDecryptIt_Click(object sender, EventArgs e)
{
//Byte[] pwBytes = Encoding.Unicode.GetBytes(password);
Byte[] pwBytes = Convert.FromBase64String(password);
try
{
Byte[] decryptedPw = ProtectedData.Unprotect(pwBytes, entropy, DataProtectionScope.LocalMachine);
string pw = Encoding.Unicode.GetString(decryptedPw);
textBox2.Text = pw;
}
catch (CryptographicException ce)
{
textBox2.Text = ce.Message;
}
}
}
}
关于c# - ProtectedData.Protect 间歇性故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/246822/
对于密码加密,我想使用 ProtectedData。 据我所知,这是 CryptProtectData 的包装器。 MSDN 只对基于用户凭据的加密做了一些模糊的说明并且解密通常必须在同一台机器上完成
我正在考虑在我正在处理的应用程序中存储一些敏感数据。我一直在查看 Microsoft 提供的 ProtectedData 类,它看起来非常简单。但是,我有几个关于它如何工作的问题。 首先,我看到它使用
我们有一个构建过程,需要解密密码,然后使用该密码连接到数据库。我们使用数据保护 API (DPAPI) 通过 PowerShell 在构建服务器上的计算机范围内加密密码(我使用自己的域帐户登录): [
我正在编写密码加密例程。我写了下面的应用程序来说明我的问题。大约 20% 的时间,此代码按预期工作。其余时间,解密会抛出加密异常 - “数据无效”。 我认为问题出在加密部分,因为解密部分每次都一样。也
我想以一种安全的方式保存用户详细信息,所以我的目的是获取一个包含凭据的类,对其进行序列化,使用 protectedData 对其进行加密,然后将这个新的加密数据保存在隔离存储中。我有以下保存方法
假设有人可以访问我的所有硬盘,我猜弱点就是我的 Windows 密码。在不知道/无法检索的情况下,数据应该非常安全,不是吗? 我特地询问是因为 EFS entry在维基百科中指出 In Windows
假设我加密数据并将其写入如下文件: byte[] encrypted = ProtectedData.Protect(plain, null, DataProtectionScope.Curr
对于写入 txt 文件的数据的加密/解密,我尝试使用 System.Security.Cryptography.ProtectedData.Protect 和 Unprotect 方法。这些方法的参数
我想知道是否有人在网络农场环境中成功地将 DPAPI 与用户存储一起使用? 因为我们的应用程序是最近从 1.1 转换为 2.0 ASP.NET 应用程序,所以我们使用了直接调用 CryptUnprot
我需要在本地数据库中存储和检索敏感数据 - 此数据由 Web 应用程序使用。 为了保护上述数据,我选择使用 ProtectedData 类。 IIS 应用程序正在使用特定的 AD 用户运行(高级设置中
如果加密是通过 ProtectedData CurrentUser 使用的,并且我有一个使用 Forms 身份验证的站点(带有自定义成员资格模块,我认为这不会有什么不同),它可以跨多个不同的 Web
我在尝试使用 System.Security 的加密代码时收到运行时错误。我添加了对 System.Security 的引用,一切看起来都不错,但我收到此错误:“编译器错误消息:CS0103:名称‘P
所以我尝试在 System.Security.Cryptography.ProtectedData 对象中使用 Unprotect 方法并不断收到异常: cryptographicexception
我在看System.Security.Cryptography.ProtectedData.Protect @ https://docs.microsoft.com/en-gb/dotnet/api/
我正在测试使用 .net v4 的 System.Security.Cryptography.ProtectedData() 和 UnprotectData() 方法与 DataProtectionS
我已经按照 this article 中的描述引用了文件 System.Security.dll但是根据我的 IDE,行 using System.Security.Cryptography; 可以安
我正在尝试在 Windows Server 2012 Datacenter 上部署 .NET Core 应用程序。 我已经安装了.NET Core Windows Server Hosting bun
我有 windows phone8.0 (silverlight) 应用程序,我想升级到 UWP。在 Windows Phone 8.0 应用程序中,我使用“ProtectedData.Protect
我的旧 (WP-8.0-Silverlight) 应用程序使用 ProtectedData.Protect 方法以安全的方式保存数据。我想将我现有的应用程序升级到 Windows Phone 通用应用
我是一名优秀的程序员,十分优秀!