加密和解密 Azure redis 缓存中的值 [英] Encrypting and decrypting values in Azure redis cache

查看:117
本文介绍了加密和解密 Azure redis 缓存中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在存储时加密,在读取 redis 缓存中的值时解密.什么是最好的安全方式来做到这一点.由于符合 GDPR,我无法直接存储用户别名.

以下是我将其转换为字节并将字节数组存储在 redis 中的方法.

隐私

您可以

public ActionResult RedisCache(){ViewBag.Message = ASP.NET 上的 Azure Redis 缓存的简单示例.";var lazyConnection = new Lazy(() =>{string cacheConnection = _configuration.GetSection("CacheConnection").Value;返回 ConnectionMultiplexer.Connect(cacheConnection);});//Connection 指的是一个返回 ConnectionMultiplexer 的属性//如前面的例子所示.IDatabase 缓存 = lazyConnection.Value.GetDatabase();//使用缓存对象执行缓存操作...string original = "这里有一些数据要加密!";string guid = Guid.NewGuid().ToString();字节[] myRijndaelKey;字节[] myRijndaelIV;使用 (RijndaelManaged myRijndael = new RijndaelManaged()){myRijndael.GenerateKey();myRijndael.GenerateIV();myRijndaelKey = myRijndael.Key;myRijndaelIV = myRijndael.IV;}byte[] encrypted_original = EncryptandDecrypt.EncryptStringToBytes(original, myRijndaelKey, myRijndaelIV);ViewBag.command6 = 原件;ViewBag.command6Result = encrypted_original;//设置原始数据cache.StringSet(guid, encrypted_original);//设置key和ivcache.StringSet(guid+Key", myRijndaelKey);cache.StringSet(guid+IV", myRijndaelIV);//从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");字符串decrypted_originalString = EncryptandDecrypt.DecryptStringFromBytes(get_encrypted_originalByte, get_Key, get_IV);ViewBag.command7 = 从 Redis 获取:"+ get_encrypted_originalByte;ViewBag.command7Result =解密数据:"+ 解密原始字符串;lazyConnection.Value.Dispose();返回视图();}

I want to encrypt while storing and decrypt while reading the values in redis cache. What can be best secured way to do it. Due to GDPR compliance i can't directly store the user aliases.

Following is my approach of converting it to Bytes and storing the byte array in redis.

https://docs.microsoft.com/es-es/dotnet/api/system.security.cryptography.rijndaelmanaged.generatekey?view=netcore-3.1

But i am not able to decrypt the byte array from redis.

解决方案

UPDATE

PRIVIOUS

You can download my sample code.

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();
}

这篇关于加密和解密 Azure redis 缓存中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆