简体   繁体   English

使用DPAPI存储RijndaelManaged密钥

[英]Using DPAPI to store RijndaelManaged key

I'm using C#. 我正在使用C#。
I have a private key with size of 256 bytes, 我有一个256个字节的私钥,

I'm trying to use DPAPI as follow: 我正在尝试使用DPAPI ,如下所示:

  RijndaelManaged key = new RijndaelManaged();
  byte[] buffer = new byte[32]
        {
                3,3,3,3,3,3,3,3,
                5,5,5,5,5,5,5,57,
                6,7,8,8,8,8,8,3,
                1,33,36,39,39,39,31,37
        };

        byte[] secret = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

        // Encrypt a copy of the data to the stream.
        byte[] output = ProtectedData.Protect(buffer, secret, DataProtectionScope.CurrentUser);
        key.Key = output;//Throw an exception

My problem that output array thats return from ProtectData.Protect is with size that key.Key isn't supported (178 bytes) and when i'm trying to insert the output into that RijndaelManaged key i'm got an exception: 我的问题是从ProtectData.Protect返回的output数组的大小是不支持key.Key大小(178个字节),当我尝试将output插入该RijndaelManaged键时,我遇到了一个例外:

'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll mscorlib.dll中发生了'System.Security.Cryptography.CryptographicException'

Additional information: Specified key is not a valid size for this algorithm. 附加信息:指定的密钥不是此算法的有效大小。

How can i solve it? 我该如何解决? or any another solution to store my RijndaelManaged key? 或任何其他解决方案来存储我的RijndaelManaged密钥?

I want also to access to my private key from another proccess 我还想从另一个过程访问我的私钥

Thanks. 谢谢。

The output of ProtectedData.Protect is encrypted (not an encryption key). ProtectedData.Protect的输出已加密(不是加密密钥)。 It grows to store whatever context and integrity checking it needs to prove that it can decrypt correctly. 它可以存储任何需要证明可以正确解密的上下文和完整性检查。 To get your original 256-bit key back you would need to call Unprotect . 要取回原始的256位密钥,您需要致电Unprotect

If you're trying to derive a key (instead of encrypt it) use a key derivation routine, like PBKDF2 (in .NET this is implemented by Rfc2898DeriveBytes ). 如果要派生密钥(而不是加密密钥),请使用密钥派生例程,例如PBKDF2(在.NET中,这是由Rfc2898DeriveBytes实现的)。

Alternatively, if you're trying to use DPAPI to protect data, it does that inherently; 另外,如果您尝试使用DPAPI保护数据,则它会内在地做到这一点。 you don't get to customize a key for it... just pass it the data to protect. 您无需为其自定义密钥...只需将其传递给数据进行保护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM