C#编程-ini文件读写MD5Encrypt加密、DES加密

public class Filesini
    {
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string sectionName, string key, string defaultValue, byte[] returnBuffer, int size, string filePath);
 
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string sectionName, string key, string value, string filePath);


        public bool ExistINIFile(string strinipath)
        {
            return File.Exists(strinipath);
        }

        public bool isSameSectionName(string strinipath,string name)
        {
            List<string> listName = GetSectionNames(strinipath);
            if (ExistINIFile(strinipath))
            {
                foreach (string n in listName)
                {
                    if (n == name) {
                       return true;                        
                    }
                }
            }
            return false;
        }

        #region 加密

        #region ini文件DES加密/解密使用中
        // 初始化DES加密的密钥和一个随机的、8比特的初始化向量(IV)
        private byte[] key_8 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
        private byte[] IV_8 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };

        public string DES_Encrypt(string encryptString)
        {
            try
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(key_8, IV_8), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        public string DES_Decrypt(string decryptString)
        {
            try
            {
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(key_8, IV_8), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
        #endregion

        #region ini文件MD5加密解密
        private string strKey = "KANGJINW";
        public string strinipath;

        public string MD5Encrypt(string spToEncrypt, string sKey)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(spToEncrypt);
                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();
            }
            catch(Exception ex) {

                return "";
            }
           

        }


        public string MD5Decrypt(string spToDecrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] inputByteArray = new byte[spToDecrypt.Length / 2];
            for (int x = 0; x < spToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(spToDecrypt.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());
        }
        #endregion

        #region 读取ini文件加密
        public string IniReadValue(string StrProjcetName, string skey)
        {
            byte[] temp = new byte[2048];
            int i = GetPrivateProfileString(StrProjcetName, skey, "", temp, 500, this.strinipath);
            return temp.ToString();
        }

        public string IniMd5ReadValue(string StrProjcetName, string skey = "KANGJINW")
        {
            //StringBuilder temp = new StringBuilder(500);
            byte[] temp = new byte[2048];
            int i = GetPrivateProfileString(StrProjcetName, skey, "", temp, 500, this.strinipath);
            string str = temp.ToString();

            return MD5Decrypt(str, strKey);
        }

        #endregion

        #region 写ini文件加密

        public void IniWriteValue(string StrProjcetName, string skey, string StrValue)
        {
            WritePrivateProfileString(StrProjcetName, skey, StrValue, this.strinipath);
        }

        public void IniMd5WriteValue(string StrProjcetName, string skey, string StrValue,string path)
        {
            string str = MD5Encrypt(StrValue, strKey);
            WritePrivateProfileString(StrProjcetName, skey, str, path);
        }

        #endregion
        
        #endregion

        #region 读取ini文件无加密 

        public string GetValue(string sectionName, string key, string filePath)
        {
            byte[] buffer = new byte[2048];
            int length = GetPrivateProfileString(sectionName, key, "发生错误", buffer, 999, filePath);
            string rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length);
            return rs;
        }

        public  List<string> GetSectionNames(string filePath)
        {
            byte[] buffer = new byte[2048];
            int length = GetPrivateProfileString(null, "", "", buffer, 999, filePath);
            String[] rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length).Split(new string[] { "\0" }, StringSplitOptions.RemoveEmptyEntries);
            return rs.ToList();
        }

        public static List<string> GetKeys(string sectionName, string filePath)
        {
            byte[] buffer = new byte[2048];
            int length = GetPrivateProfileString(sectionName, null, "", buffer, 999, filePath);
            String[] rs = System.Text.UTF8Encoding.Default.GetString(buffer, 0, length).Split(new string[] { "\0" }, StringSplitOptions.RemoveEmptyEntries);
            return rs.ToList();
        }

        #endregion

        #region 写ini操作文件无加密
       

        public bool SetValue(string sectionName, string key, string value, string filePath)
        {
            int rs = (int)WritePrivateProfileString(sectionName, key, value, filePath);
            return rs > 0;
        }
       

        public bool RemoveSection(string sectionName, string filePath)
        {
            int rs = (int)WritePrivateProfileString(sectionName, null, "", filePath);
            return rs > 0;
        }

        public  bool Removekey(string sectionName, string key, string filePath)
        {
            int rs = (int)WritePrivateProfileString(sectionName, key, null, filePath);
            return rs > 0;
        }

       
        #endregion

    }

猜你喜欢

转载自blog.csdn.net/yuupengsun/article/details/106246401