1
var result=CryptoJS.AES.decrypt(data,name).toString(CryptoJS.enc.Utf8);

I try to decrypt json data using cryptojs. I want to use this code in c# . Which library/libraries is/are needed for this? It gives error CryptoJs does not exist in current context.

i use this one

public static void Main(string[] args) {

        Class1 class1obj = new Class1();
        string baseUrl = "http://www.whatsonindia.com";
        WebRequest request = HttpWebRequest.Create(baseUrl);
        WebResponse response = request.GetResponse();
        string cookiesVals = response.Headers[HttpResponseHeader.SetCookie];

        string url = "http://www.whatsonindia.com/appi/user?channelgenre=all&context=applicationname%3Dsourcebits%3Bheadendid%3D0&dateselected=0&mode=getTVGuideInfo&pageno=1&responseformat=json&responselanguage=English&starthour=0&totalhrdata=24&userid=-1";
        WebClient client = new WebClient();
        client.Headers.Add(HttpRequestHeader.Cookie, cookiesVals);
        var data = client.DownloadString(url);
        Console.WriteLine(data);
        Console.WriteLine("dynamic object....");
        Console.ReadLine();
         JavaScriptSerializer serializer = new JavaScriptSerializer();
         dynamic item = serializer.Deserialize<object>(data);
         string name = item["pki"];
        //JavaScriptSerializer serializer1 = new JavaScriptSerializer();

        var result=CryptoJS.AES.decrypt(data,name).toString(CryptoJS.enc.Utf8);

        var obj = (JObject)JsonConvert.DeserializeObject(data);
       // var dict = obj.First.First.Children().Cast<JProperty>().ToDictionary();
        var dt = (string)obj["data"];
        Console.WriteLine(dt);
                 Console.ReadLine();




    }

1 Answers1

1

CryptoJS is JavaScript library and can't be used in C# project. To do AES decrypt in C# you need to use .Net crypto library which is covered in this answer: Using AES encryption in C#.

In above linked answer you will see line that decrypts the string:

string decrypted = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

The 2nd parameter is where you can pass the key (I assume pki is byte array key that you used to encrypt the string). The 3rd parameter is the type of algorithm which you should make sure is in sync with CryptoJS when you encrypted the string.

Community
  • 1
  • 1
Shital Shah
  • 63,284
  • 17
  • 238
  • 185