C# 简单实现对字符串加密和解密

  有两种方式,供大家参考

   /// 
        /// 编译字符串
        /// 
        /// 
        /// 
        public static string FromUnicodeToAscii(string strUnicode)
        {
          
            try
            {
                if (String.IsNullOrEmpty(strUnicode))
                {
                    return String.Empty;
                }
                UnicodeEncoding Unicode = new UnicodeEncoding();

                int byteCount = Unicode.GetByteCount(strUnicode);
                byte[] origbytes = new byte[byteCount];
                origbytes = Unicode.GetBytes(strUnicode);

                StringBuilder strDecoded = new StringBuilder();
                for (int i = 0; i < byteCount; i++)
                {
                    byte b = origbytes[i];
                    //translate each byte into two digits of hexa.
                    strDecoded.Append(b.ToString("X2"));
                    //chDest[i] =  Convert.ToChar(b);
                }
                return strDecoded.ToString();
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message+" "+err.StackTrace);             
                return "*********异常";
            }
        }
        /// 
        /// 反编译字符串
        /// 
        /// 
        /// 
        public static string FromAsciiToUnicode(string strAscii)
        {
       
            try
            {
                if (String.IsNullOrEmpty(strAscii))
                {
                    return String.Empty;
                }
                StringBuilder strNew = new StringBuilder();
                UnicodeEncoding Unicode = new UnicodeEncoding();
                byte[] currbytes = new byte[2];
                for (int i = 0; i < strAscii.Length; i = i + 4)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        //each two hexa digits are becoming one byte
                        currbytes[j] = Convert.ToByte(strAscii.Substring(i + j * 2, 2), 16);
                    }
                    strNew.Append(Unicode.GetString(currbytes));
                }
                return strNew.ToString();
            }
            catch (Exception err)
            {
            
               
                return "******异常";
            }
        }
/// 
    /// 字符串简单加密
    /// 
    public class EncodeAndDecode
    {
        /// 
        /// Base64加密
        /// 
        /// 加密采用的编码方式
        /// 待加密的明文
        /// 
        public static string EncodeBase64(Encoding encode, string source)
        {
            string enstring = "";
            byte[] bytes = encode.GetBytes(source);
            try
            {
                enstring = Convert.ToBase64String(bytes);
            }
            catch
            {
                enstring = source;
            }
            return enstring;
        }

        /// 
        /// Base64加密,采用utf8编码方式加密
        /// 
        /// 待加密的明文
        /// 加密后的字符串
        public static string EncodeBase64(string source)
        {
            return EncodeBase64(Encoding.UTF8, source);
        }

        /// 
        /// Base64解密
        /// 
        /// 解密采用的编码方式,注意和加密时采用的方式一致
        /// 待解密的密文
        /// 解密后的字符串
        public static string DecodeBase64(Encoding encode, string result)
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(result);
            try
            {
                decode = encode.GetString(bytes);
            }
            catch
            {
                decode = result;
            }
            return decode;
        }

        /// 
        /// Base64解密,采用utf8编码方式解密
        /// 
        /// 待解密的密文
        /// 解密后的字符串
        public static string DecodeBase64(string result)
        {
            return DecodeBase64(Encoding.UTF8, result);
        }
    }

 

 

你可能感兴趣的:(C#,C#,字符串,加密和解密)