你知道我可以使用的任何数据保护 API。我的主要问题是我正在使用负载平衡和 Redis + SignalR 来共享数据,并且我已经禁用了粘性会话,所以我读到我必须相同的机器密钥,所以我计划在上面实现 Liek。我的 Asp.Net Signalr 应用程序在多个服务器上工作正常,但是当我尝试将消息发送到 asp.net 核心时它不起作用。请提出任何实现他的好方法
您似乎加密了来自 MVC 5 的消息,并希望解密来自部署在另一台机器上的 Asp.Net Core 2.2 应用程序的消息。MachineKey仅在 .NET Framework 下受支持。
要跨应用程序框架加密/解密数据,您需要使用同时支持 .NET Framework 和 .Net Core 的类。您可以从 .NET Standard(例如System.Security.Cryptography)中引用它。下面是使用Aes在 .NET Framework 和 .Net Core 之间跨控制台应用程序的演示:
.Net Framework,控制台应用加密数据
static void Main(string[] args)
{
string original = "Here is some data to encrypt!";
// Create a new instance of the Aes
// class. This generates a new key and initialization
// vector (IV).
using (Aes myAes = Aes.Create())
{
File.WriteAllBytes("key.data", myAes.Key);
File.WriteAllBytes("IV.data", myAes.IV);
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
File.WriteAllBytes("encrypted.data", encrypted);
Console.WriteLine("Original: {0}", original);
}
return;
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
.Net核心控制台应用,解密数据
static void Main(string[] args)
{
// Decrypt the bytes to a string.
//string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
var key = File.ReadAllBytes("key.data");
var iv = File.ReadAllBytes("IV.data");
var encryptedData= File.ReadAllBytes("encrypted.data");
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes_Aes(encryptedData, key, iv);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
您也可以参考下面的链接了解有关目标框架的详细信息。
SDK 风格项目中的目标框架