using System;
using System.Collections.Generic;
using Library;
using System.Text;
using System.Linq;

namespace MySystem
{
    public class EncryptHelper
    {

        public static string Encrypt1(SortedList<string, string> obj, string key)
        {
            SortedList<string, string> objnew = new SortedList<string, string>();
            foreach(string field in obj.Keys)
            {
                if(!string.IsNullOrEmpty(obj[field])) objnew.Add(field, obj[field]);
            }
            string signstr = function.BuildQueryString(objnew) + key;
            string sign = function.MD532(signstr).ToUpper();
            obj.Add("sign", sign);
            return AesEncrypt(Newtonsoft.Json.JsonConvert.SerializeObject(obj), key);
        }

        public static string AesEncrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(key),
                Mode = System.Security.Cryptography.CipherMode.ECB,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };
            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        
    }
}
