C#實現(xiàn)Base64處理的加密解密,編碼解碼示例
本文實例講述了C#實現(xiàn)Base64處理的加密解密,編碼解碼。分享給大家供大家參考,具體如下:
using System; using System.Text; namespace Common { /// <summary> /// 實現(xiàn)Base64加密解密 /// 作者:周公 /// </summary> public sealed class Base64 { /// <summary> /// Base64加密 /// </summary> /// <param name="codeName">加密采用的編碼方式</param> /// <param name="source">待加密的明文</param> /// <returns></returns> public static string EncodeBase64(Encoding encode, string source) { byte[] bytes = encode.GetBytes(source); try { encode = Convert.ToBase64String(bytes); } catch { encode = source; } return encode; } /// <summary> /// Base64加密,采用utf8編碼方式加密 /// </summary> /// <param name="source">待加密的明文</param> /// <returns>加密后的字符串</returns> public static string EncodeBase64(string source) { return EncodeBase64(Encoding.UTF8, source); } /// <summary> /// Base64解密 /// </summary> /// <param name="codeName">解密采用的編碼方式,注意和加密時采用的方式一致</param> /// <param name="result">待解密的密文</param> /// <returns>解密后的字符串</returns> 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; } /// <summary> /// Base64解密,采用utf8編碼方式解密 /// </summary> /// <param name="result">待解密的密文</param> /// <returns>解密后的字符串</returns> public static string DecodeBase64(string result) { return DecodeBase64(Encoding.UTF8, result); } } }
PS:這里再為大家提供幾款比較實用的base64在線編碼解碼工具供大家使用:
BASE64編碼解碼工具:
http://tools.jb51.net/transcoding/base64
在線圖片轉換BASE64工具:
http://tools.jb51.net/transcoding/img2base64
Base64在線編碼解碼 UTF-8版:
http://tools.jb51.net/tools/base64_decode-utf8.php
Base64在線編碼解碼 gb2312版:
http://tools.jb51.net/tools/base64_decode-gb2312.php
更多關于C#相關內(nèi)容感興趣的讀者可查看本站專題:《C#編碼操作技巧總結》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數(shù)據(jù)結構與算法教程》、《C#面向?qū)ο蟪绦蛟O計入門教程》及《C#程序設計之線程使用技巧總結》
希望本文所述對大家C#程序設計有所幫助。
相關文章
基于C#?實現(xiàn)?OPC?DA?Server的問題小結
這篇文章主要介紹了基于C#?實現(xiàn)?OPC?DA?Server的相關知識,關于C#怎么編寫一個進程外的DCOM組件,這里先不做介紹了,這里主要介紹下OPC?DA?Server?的第一個接口,感興趣的朋友跟隨小編一起看看吧2024-04-04C#中使用JSON.NET實現(xiàn)JSON、XML相互轉換
這篇文章主要介紹了C#中使用JSON.NET實現(xiàn)JSON、XML相互轉換的相關代碼及示例,需要的朋友可以參考下2015-11-11c#創(chuàng)建windows服務(Windows Services)詳細步驟
這篇文章主要介紹了c#創(chuàng)建windows服務(Windows Services)詳細步驟,大家參考使用吧2013-12-12