C# UWP CryptographicEngine.Encrypt() returns 空
C# UWP CryptographicEngine.Encrypt() returns null
我试图将输入字节 [] 加密为 AES,但最终加密缓冲区为空。
private byte[] Encrypt(byte[] data)
{
byte[] secretKey = new byte[] { 1, 2, 3 };
IBuffer key = Convert.FromBase64String(Convert.ToBase64String(secretKey.ToArray()).ToString()).AsBuffer();
Debug.WriteLine(key.Length);
SymmetricKeyAlgorithmProvider algorithmProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbc);
CryptographicKey cryptographicKey = algorithmProvider.CreateSymmetricKey(key);
IBuffer bufferEncrypt = CryptographicEngine.Encrypt(cryptographicKey, data.AsBuffer(), null);
return bufferEncrypt.ToArray();
}
调试器将局部变量显示为(名称、值、类型):
+ this {Project.Auth} Project.Auth
+ data {byte[15]} byte[]
bufferEncrypt null Windows.Storage.Streams.IBuffer
+ cryptographicKey {Windows.Security.Cryptography.Core.CryptographicKey} Windows.Security.Cryptography.Core.CryptographicKey
+ key {System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBuffer} Windows.Storage.Streams.IBuffer {System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBuffer}
+ algorithmProvider {Windows.Security.Cryptography.Core.SymmetricKeyAlgorithmProvider} Windows.Security.Cryptography.Core.SymmetricKeyAlgorithmProvider
+ SecretKey Count = 16 System.Collections.Generic.List<byte>
我错在哪里?
我什至不能 运行 你的代码片段在我这边成功,当 CreateSymmetricKey(key)
时会抛出异常 System.ArgumentException: 'Value does not fall within the expected range.
。您的密钥似乎长度不对,密钥长度应根据您需要的安全性设置一定的位数。 (AES 的 256 位很常见)。
此外,CBC 算法需要一个初始化向量,您可以为该向量分配一个随机数。更多细节请参考对称密钥
.
请尝试解决您的问题并按照 official sample or this example 实施加密功能。
我试图将输入字节 [] 加密为 AES,但最终加密缓冲区为空。
private byte[] Encrypt(byte[] data)
{
byte[] secretKey = new byte[] { 1, 2, 3 };
IBuffer key = Convert.FromBase64String(Convert.ToBase64String(secretKey.ToArray()).ToString()).AsBuffer();
Debug.WriteLine(key.Length);
SymmetricKeyAlgorithmProvider algorithmProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbc);
CryptographicKey cryptographicKey = algorithmProvider.CreateSymmetricKey(key);
IBuffer bufferEncrypt = CryptographicEngine.Encrypt(cryptographicKey, data.AsBuffer(), null);
return bufferEncrypt.ToArray();
}
调试器将局部变量显示为(名称、值、类型):
+ this {Project.Auth} Project.Auth
+ data {byte[15]} byte[]
bufferEncrypt null Windows.Storage.Streams.IBuffer
+ cryptographicKey {Windows.Security.Cryptography.Core.CryptographicKey} Windows.Security.Cryptography.Core.CryptographicKey
+ key {System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBuffer} Windows.Storage.Streams.IBuffer {System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBuffer}
+ algorithmProvider {Windows.Security.Cryptography.Core.SymmetricKeyAlgorithmProvider} Windows.Security.Cryptography.Core.SymmetricKeyAlgorithmProvider
+ SecretKey Count = 16 System.Collections.Generic.List<byte>
我错在哪里?
我什至不能 运行 你的代码片段在我这边成功,当 CreateSymmetricKey(key)
时会抛出异常 System.ArgumentException: 'Value does not fall within the expected range.
。您的密钥似乎长度不对,密钥长度应根据您需要的安全性设置一定的位数。 (AES 的 256 位很常见)。
此外,CBC 算法需要一个初始化向量,您可以为该向量分配一个随机数。更多细节请参考对称密钥 .
请尝试解决您的问题并按照 official sample or this example 实施加密功能。