Explore Courses Blog Tutorials Interview Questions
0 votes
in Azure by (13.1k points)
So, I am using Azure redis cache for encrypting and decrypting. While encryption, I want a to store and while decryption I want to read the values. Encryption is working fine, but was not able to decrypt the values. Can anyone help me with this?

1 Answer

0 votes
by (26.7k points)

The below will help you for decrypting the values:

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




        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;


    return View();


I hope this will help.

Want to become an Azure expert ? join Azure Training now!

Related questions

0 votes
1 answer
asked Jul 19, 2019 in Azure by Dhanangkita (5.8k points)
0 votes
1 answer
asked Dec 20, 2020 in AWS by devin (5.6k points)
0 votes
1 answer
asked Jan 31, 2020 in Azure by tusharsharma (4k points)
0 votes
1 answer

Browse Categories