azure - 在 Azure redis 缓存中加密和解密值
问题描述
我想在存储时加密,在读取 redis 缓存中的值时解密。什么是最好的安全方式来做到这一点。由于 GDPR 合规性,我无法直接存储用户别名。
以下是我将其转换为字节并将字节数组存储在 redis 中的方法。
但我无法从 redis 解密字节数组。
解决方案
更新
私人的
您可以下载我的示例代码。
public ActionResult RedisCache()
{
ViewBag.Message = "A simple example with Azure Cache for Redis on ASP.NET.";
var lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
string cacheConnection = _configuration.GetSection("CacheConnection").Value;
return ConnectionMultiplexer.Connect(cacheConnection);
});
// Connection refers to a property that returns a ConnectionMultiplexer
// as shown in the previous example.
IDatabase cache = lazyConnection.Value.GetDatabase();
// Perform cache operations using the cache object...
string original = "Here is some data to encrypt!";
string guid = Guid.NewGuid().ToString();
byte[] myRijndaelKey;
byte[] myRijndaelIV;
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
myRijndael.GenerateKey();
myRijndael.GenerateIV();
myRijndaelKey = myRijndael.Key;
myRijndaelIV = myRijndael.IV;
}
byte[] encrypted_original = EncryptandDecrypt.EncryptStringToBytes(original, myRijndaelKey, myRijndaelIV);
ViewBag.command6 = original;
ViewBag.command6Result = encrypted_original;
//set orginal data
cache.StringSet(guid, encrypted_original);
//set key and iv
cache.StringSet(guid+"Key", myRijndaelKey);
cache.StringSet(guid+"IV", myRijndaelIV);
//get data from redis
byte[] get_encrypted_originalByte = (byte[])cache.StringGet(guid);
byte[] get_Key = (byte[])cache.StringGet(guid+"Key");
byte[] get_IV = (byte[])cache.StringGet(guid+"IV");
string decrypted_originalString = EncryptandDecrypt.DecryptStringFromBytes(get_encrypted_originalByte, get_Key, get_IV);
ViewBag.command7 = "Get From Redis:"+ get_encrypted_originalByte;
ViewBag.command7Result = "decrypted data:" + decrypted_originalString;
lazyConnection.Value.Dispose();
return View();
}
推荐阅读
- cassandra - Cassandra 是否支持 PITR(时间点恢复)?
- design-patterns - 为场景设计类
- c++ - 此代码不比较输入。其中有什么问题?
- java - 向量循环的 Java 输入错误,直到正确为止
- typescript - 如何根据传递的参数获得函数响应?
- java - 如何检测 SELECT * 何时相同?
- apache - 如何使用 htaccess 将 PDF 链接重定向到具有相同 PDF 文件的不同域
- excel - 将行设置为设置数据源
- php - Paytm支付网关,启动交易模式php中的系统错误
- python - 尝试使用 python 读取多个数据帧以分析我的论文