Newer
Older
IRIS_COLLECT_GA / IOM_cs / EncryptionHelper.cs
yangqianqian on 1 Jun 2021 3 KB first commit
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;


namespace IOM_cs 
{
    public enum EncryptionKeyEnum  
	{  
	    KeyA,  
	    KeyB  
	}  
           
    public class EncryptionHelper  
	{  
        string encryptionKeyA = "casic_is";
        string encryptionKeyB = "TestIris";
        string md5Begin = "Merry";
        string md5End = "Happy";  
	    string encryptionKey = string.Empty;  

	    public EncryptionHelper()  
	    {  
	        this.InitKey();  
	    }  

	    public EncryptionHelper(EncryptionKeyEnum key)  
	    {  
	        this.InitKey(key);  
        }  


	    private void InitKey(EncryptionKeyEnum key = EncryptionKeyEnum.KeyA)  
	    {  
	        switch (key)  
            {  
                case EncryptionKeyEnum.KeyA:  
	                encryptionKey = encryptionKeyA;  
	                break;  
	            case EncryptionKeyEnum.KeyB:  
	                encryptionKey = encryptionKeyB;  
	                break;  
	        }  
	    }

        public string EncryptString(string str)
        {
            return Encrypt(str, encryptionKey);
        }  

        public string DecryptString(string str)  
	    {  
	        return Decrypt(str, encryptionKey);  
	    }  


	    public string GetMD5String(string str)  
	    {  
	        str = string.Concat(md5Begin, str, md5End);  
	        MD5 md5 = new MD5CryptoServiceProvider();  
	        byte[] inputData = Encoding.Unicode.GetBytes(str);  
	        byte[] targetData = md5.ComputeHash(inputData);  
	        string md5String = string.Empty;  
	        foreach (var b in targetData)  
	            md5String += b.ToString("x2");  
	        return md5String;  
        }


        public string Encrypt(string str, string sKey)  
	    {  
	        DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
	        byte[] inputByteArray = Encoding.Default.GetBytes(str);  
	        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
	        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
            MemoryStream ms = new MemoryStream();  
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);  
	        cs.Write(inputByteArray, 0, inputByteArray.Length);  
	        cs.FlushFinalBlock();  
	        StringBuilder ret = new StringBuilder();  
	        foreach (byte b in ms.ToArray())  
	        {  
	            ret.AppendFormat("{0:X2}", b);  
	        }  
	        ret.ToString();  
	        return ret.ToString();  
	    }  


	    public static string Decrypt(string pToDecrypt, string sKey)  
	    {  
	        DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
	        byte[] inputByteArray = new byte[pToDecrypt.Length / 2];  
            
            for (int x = 0; x < pToDecrypt.Length / 2; x++)  
	        {  
	            int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));  
	            inputByteArray[x] = (byte)i;  
            }  
	        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
	        MemoryStream ms = new MemoryStream();  
	        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);  
	        cs.Write(inputByteArray, 0, inputByteArray.Length);  
	        cs.FlushFinalBlock();  
	        StringBuilder ret = new StringBuilder();  
	        return System.Text.Encoding.Default.GetString(ms.ToArray());  
	    }  
	}  



    
}