Base64编码

 /// <summary>
        /// 将字符串编码为Base64字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Base64Encode(string str)
        {
            byte[] barray;
            barray = Encoding.Default.GetBytes(str);
            return Convert.ToBase64String(barray);
        }

        /// <summary>
        /// 将Base64字符串解码为普通字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Base64Decode(string str)
        {
            byte[] barray;
            try
            {
                barray = Convert.FromBase64String(str);
                return Encoding.Default.GetString(barray);
            }
            catch
            {
                return str;
            }
        }

posted @ 2014-11-18 11:58  点点星痕  阅读(145)  评论(0编辑  收藏  举报