Encrypt and returnJsonStringFromDataTableUtility

 using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Security.Cryptography;

using System.Text;

using System.Web;

using System.Web.Script.Serialization;


namespace RESTFulWCFService

{

    public static class Utility

    {

        public static string Encrypt(string toEncrypt, bool useHashing)

        {

            byte[] keyArray;

            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);



            // Get the key from config file


            string key = "CPPKey";

            //System.Windows.Forms.MessageBox.Show(key);

            //If hashing use get hashcode regards to your key

            if (useHashing)

            {

                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();

                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));

                //Always release the resources and flush data

                // of the Cryptographic service provide. Best Practice


                hashmd5.Clear();

            }

            else

                keyArray = UTF8Encoding.UTF8.GetBytes(key);


            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

            //set the secret key for the tripleDES algorithm

            tdes.Key = keyArray;

            //mode of operation. there are other 4 modes.

            //We choose ECB(Electronic code Book)

            tdes.Mode = CipherMode.ECB;

            //padding mode(if any extra byte added)


            tdes.Padding = PaddingMode.PKCS7;


            ICryptoTransform cTransform = tdes.CreateEncryptor();

            //transform the specified region of bytes array to resultArray

            byte[] resultArray =

              cTransform.TransformFinalBlock(toEncryptArray, 0,

              toEncryptArray.Length);

            //Release resources held by TripleDes Encryptor

            tdes.Clear();

            //Return the encrypted data into unreadable string format

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);

        }


        public static string ReturnJsonStringFromTable(DataTable Dt)

        {

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();

            Dictionary<string, object> row;

            try

            {

                foreach (DataRow dr in Dt.Rows)

                {

                    row = new Dictionary<string, object>();

                    foreach (DataColumn col in Dt.Columns)

                    {

                        row.Add(col.ColumnName, dr[col]);

                    }

                    rows.Add(row);

                }

            }

            catch (Exception)

            {

                throw;

            }

            return serializer.Serialize(rows);

        }


    }

}

Comments

Popular posts from this blog

Npgsql query and format with output parameters

Npgsql Helper class