C#實(shí)現(xiàn)Base64處理的加密解密,編碼解碼示例
本文實(shí)例講述了C#實(shí)現(xiàn)Base64處理的加密解密,編碼解碼。分享給大家供大家參考,具體如下:
using System; using System.Text; namespace Common { /// <summary> /// 實(shí)現(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">解密采用的編碼方式,注意和加密時(shí)采用的方式一致</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:這里再為大家提供幾款比較實(shí)用的base64在線編碼解碼工具供大家使用:
BASE64編碼解碼工具:
http://tools.jb51.net/transcoding/base64
在線圖片轉(zhuǎn)換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
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#編碼操作技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
基于C#?實(shí)現(xiàn)?OPC?DA?Server的問題小結(jié)
這篇文章主要介紹了基于C#?實(shí)現(xiàn)?OPC?DA?Server的相關(guān)知識(shí),關(guān)于C#怎么編寫一個(gè)進(jìn)程外的DCOM組件,這里先不做介紹了,這里主要介紹下OPC?DA?Server?的第一個(gè)接口,感興趣的朋友跟隨小編一起看看吧2024-04-04Unity接入百度AI實(shí)現(xiàn)果蔬識(shí)別
本文將介紹如何利用Unity接入百度AI從而實(shí)現(xiàn)果蔬識(shí)別,可以做到識(shí)別近千種水果和蔬菜的名稱,可自定義返回識(shí)別結(jié)果數(shù)。感興趣的小伙伴可以了解一下2022-02-02C#使用泛型方法設(shè)計(jì)實(shí)現(xiàn)單向鏈表詳解
這篇文章主要為大家詳細(xì)介紹了C#如何使用泛型方法設(shè)計(jì)實(shí)現(xiàn)一個(gè)單向鏈表,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換
這篇文章主要介紹了C#中使用JSON.NET實(shí)現(xiàn)JSON、XML相互轉(zhuǎn)換的相關(guān)代碼及示例,需要的朋友可以參考下2015-11-11c#創(chuàng)建windows服務(wù)(Windows Services)詳細(xì)步驟
這篇文章主要介紹了c#創(chuàng)建windows服務(wù)(Windows Services)詳細(xì)步驟,大家參考使用吧2013-12-12