C#常用加密解密方法(Base64加密解密)

您所在的位置:网站首页 常见的加密解密方式 C#常用加密解密方法(Base64加密解密)

C#常用加密解密方法(Base64加密解密)

2024-07-15 17:21:51| 来源: 网络整理| 查看: 265

在日常开发过程中,总会遇到需要加密解密的需求,这里我整理了C#常用的加密解密方法分享给大家。

先看看加密的基本概念:

"加密",是一种限制对网络上传输数据的访问权的技术。原始数据(也称为明文,plaintext)被加密设备(硬件或软件)和密钥加密而产生的经过编码的数据称为密文(ciphertext)。将密文还原为原始明文的过程称为解密,它是加密的反向处理,但解密者必须利用相同类型的加密设备和密钥对密文进行解密。

加密的基本功能包括:

1. 防止不速之客查看机密的数据文件;

2. 防止机密数据被泄露或篡改;

3. 防止特权用户(如系统管理员)查看私人数据文件;

4. 使入侵者不能轻易地查找一个系统的文件。

一、本节摘要

本节主要分享Base64加密解密:

        Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。

        Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。采用Base64编码具有不可读性,需要解码后才能阅读。

        Base64由于以上优点被广泛应用于计算机的各个领域,然而由于输出内容中包括两个以上“符号类”字符(+, /, =),不同的应用场景又分别研制了Base64的各种“变种”。为统一和规范化Base64的输出,Base62x被视为无符号化的改进版本。

二、源码分享

1、主方法

protected void Test() { //m_codeTable = @"STUVWXYZbacdefghivklABCDEFGHIJKLMNOPQRmnopqrstu!wxyz0123456789+/"; //m_pad = "j"; this.InitDict(); string test = "abc ABC 你好!◎#¥%……!@#$%^"; string encode = this.Encode("false"); string decode = this.Decode(encode); Console.WriteLine(encode); Console.WriteLine(test == decode); }

2、Base64加密解密类

using System; using System.Collections.Generic; using System.Text; namespace Common { /// /// 基于Base64的加密编码, /// 可以设置不同的密码表来获取不同的编码合解码 /// public class Base64Util { public Base64Util() { this.InitDict(); } protected static Base64Util s_b64 = new Base64Util(); #region Base64加密解密 /// /// Base64是一種使用64基的位置計數法。它使用2的最大次方來代表僅可列印的ASCII 字元。 /// 這使它可用來作為電子郵件的傳輸編碼。在Base64中的變數使用字元A-Z、a-z和0-9 , /// 這樣共有62個字元,用來作為開始的64個數字,最後兩個用來作為數字的符號在不同的 /// 系統中而不同。 /// Base64加密 /// /// /// public static string Base64Encrypt(string str) { byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str); return Convert.ToBase64String(encbuff); } /// /// Base64解密 /// /// /// public static string Base64Decrypt(string str) { byte[] decbuff = Convert.FromBase64String(str); return System.Text.Encoding.UTF8.GetString(decbuff); } #endregion /// /// 使用默认的密码表加密字符串 /// /// /// public static string Encrypt(string input) { return s_b64.Encode(input); } /// /// 使用默认的密码表解密字符串 /// /// /// public static string Decrypt(string input) { return s_b64.Decode(input); } /// /// 获取具有标准的Base64密码表的加密类 /// /// public static Base64Util GetStandardBase64() { Base64Util b64 = new Base64Util(); b64.Pad = "="; b64.CodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; return b64; } protected string m_codeTable = @"ABCDEFGHIJKLMNOPQRSTUVWXYZbacdefghijklmnopqrstu_wxyz0123456789*-"; protected string m_pad = "v"; protected Dictionary m_t1 = new Dictionary(); protected Dictionary m_t2 = new Dictionary(); /// /// 密码表 /// public string CodeTable { get { return m_codeTable; } set { if (value == null) { throw new Exception("密码表不能为null"); } else if (value.Length < 64) { throw new Exception("密码表长度必须至少为64"); } else { this.ValidateRepeat(value); this.ValidateEqualPad(value, m_pad); m_codeTable = value; this.InitDict(); } } } /// /// 补码 /// public string Pad { get { return m_pad; } set { if (value == null) { throw new Exception("密码表的补码不能为null"); } else if (value.Length != 1) { throw new Exception("密码表的补码长度必须为1"); } else { this.ValidateEqualPad(m_codeTable, value); m_pad = value; this.InitDict(); } } } /// /// 返回编码后的字符串 /// /// /// public string Encode(string source) { if (source == null || source == "") { return ""; } else { StringBuilder sb = new StringBuilder(); byte[] tmp = System.Text.UTF8Encoding.UTF8.GetBytes(source); int remain = tmp.Length % 3; int patch = 3 - remain; if (remain != 0) { Array.Resize(ref tmp, tmp.Length + patch); } int cnt = (int)Math.Ceiling(tmp.Length * 1.0 / 3); for (int i = 0; i < cnt; i++) { sb.Append(this.EncodeUnit(tmp[i * 3], tmp[i * 3 + 1], tmp[i * 3 + 2])); } if (remain != 0) { sb.Remove(sb.Length - patch, patch); for (int i = 0; i < patch; i++) { sb.Append(m_pad); } } return sb.ToString(); } } protected string EncodeUnit(params byte[] unit) { int[] obj = new int[4]; obj[0] = (unit[0] & 0xfc) >> 2; obj[1] = ((unit[0] & 0x03) > 4); obj[2] = ((unit[1] & 0x0f) > 6); obj[3] = unit[2] & 0x3f; StringBuilder sb = new StringBuilder(); for (int i = 0; i < obj.Length; i++) { sb.Append(this.GetEC((int)obj[i])); } return sb.ToString(); } protected char GetEC(int code) { return m_t1[code];//m_codeTable[code]; } /// /// 获得解码字符串 /// /// /// public string Decode(string source) { if (source == null || source == "") { return ""; } else { List list = new List(); char[] tmp = source.ToCharArray(); int remain = tmp.Length % 4; if (remain != 0) { Array.Resize(ref tmp, tmp.Length - remain); } int patch = source.IndexOf(m_pad); if (patch != -1) { patch = source.Length - patch; } int cnt = tmp.Length / 4; for (int i = 0; i < cnt; i++) { this.DecodeUnit(list, tmp[i * 4], tmp[i * 4 + 1], tmp[i * 4 + 2], tmp[i * 4 + 3]); } for (int i = 0; i < patch; i++) { list.RemoveAt(list.Count - 1); } return System.Text.Encoding.UTF8.GetString(list.ToArray()); } } protected void DecodeUnit(List byteArr, params char[] chArray) { int[] res = new int[3]; byte[] unit = new byte[chArray.Length]; for (int i = 0; i < chArray.Length; i++) { unit[i] = this.FindChar(chArray[i]); } res[0] = (unit[0] > 4); res[1] = ((unit[1] & 0xf) > 2); res[2] = ((unit[2] & 0x3) i) { throw new Exception("密码表中含有重复字符:" + input[i]); } } } /// /// 检查字符串是否包含补码字符 /// /// /// protected void ValidateEqualPad(string input, string pad) { if (input.IndexOf(pad) > -1) { throw new Exception("密码表中包含了补码字符:" + pad); } } protected void Test() { this.InitDict(); string test = "abc ABC 你好!◎#¥%……!@#$%^"; string encode = this.Encode("false"); string decode = this.Decode(encode); Console.WriteLine(encode); Console.WriteLine(test == decode); } } }



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭