C#编写的Base64加密和解密类 C#编写的Base64加密和解密类
人气:0想了解C#编写的Base64加密和解密类的相关内容吗,在本文为您仔细讲解C#编写的Base64加密和解密类的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Base64,加密,解密,C#,下面大家一起来学习吧。
这个C#类是一个基于Base64的加密和解密类,用户可以可以使用默认的秘钥进行加密、解密,也可以自己设定秘钥进行加密和解密,非常实用
代码一:非常精简的代码
/// <summary> /// Base64加密,解密方法 /// </summary> /// <paramname="s">输入字符串</param> /// <paramname="c">true-加密,false-解密</param> static public string base64(string s, bool c) { if (c) { returnSystem.Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(s)); } else { try { returnSystem.Text.Encoding.Default.GetString(System.Convert.FromBase64String(pwdString)); } catch(Exception exp) { returnexp.Message; } } }
代码二:
using System; using System.Security.Cryptography; using System.Text; namespace DotNet.Utilities { /// <summary> /// Encrypt 的摘要说明。 /// </summary> public class DEncrypt { /// <summary> /// 构造方法 /// </summary> public DEncrypt() { } #region 使用 缺省密钥字符串 加密/解密string /// <summary> /// 使用缺省密钥字符串加密string /// </summary> /// <param name="original">明文</param> /// <returns>密文</returns> public static string Encrypt(string original) { return Encrypt(original,"sharejs.com"); } /// <summary> /// 使用缺省密钥字符串解密string /// </summary> /// <param name="original">密文</param> /// <returns>明文</returns> public static string Decrypt(string original) { return Decrypt(original,"sharejs.com",System.Text.Encoding.Default); } #endregion #region 使用 给定密钥字符串 加密/解密string /// <summary> /// 使用给定密钥字符串加密string /// </summary> /// <param name="original">原始文字</param> /// <param name="key">密钥</param> /// <param name="encoding">字符编码方案</param> /// <returns>密文</returns> public static string Encrypt(string original, string key) { byte[] buff = System.Text.Encoding.Default.GetBytes(original); byte[] kb = System.Text.Encoding.Default.GetBytes(key); return Convert.ToBase64String(Encrypt(buff,kb)); } /// <summary> /// 使用给定密钥字符串解密string /// </summary> /// <param name="original">密文</param> /// <param name="key">密钥</param> /// <returns>明文</returns> public static string Decrypt(string original, string key) { return Decrypt(original,key,System.Text.Encoding.Default); } /// <summary> /// 使用给定密钥字符串解密string,返回指定编码方式明文 /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密钥</param> /// <param name="encoding">字符编码方案</param> /// <returns>明文</returns> public static string Decrypt(string encrypted, string key,Encoding encoding) { byte[] buff = Convert.FromBase64String(encrypted); byte[] kb = System.Text.Encoding.Default.GetBytes(key); return encoding.GetString(Decrypt(buff,kb)); } #endregion #region 使用 缺省密钥字符串 加密/解密/byte[] /// <summary> /// 使用缺省密钥字符串解密byte[] /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密钥</param> /// <returns>明文</returns> public static byte[] Decrypt(byte[] encrypted) { byte[] key = System.Text.Encoding.Default.GetBytes("sharejs.com"); return Decrypt(encrypted,key); } /// <summary> /// 使用缺省密钥字符串加密 /// </summary> /// <param name="original">原始数据</param> /// <param name="key">密钥</param> /// <returns>密文</returns> public static byte[] Encrypt(byte[] original) { byte[] key = System.Text.Encoding.Default.GetBytes("sharejs.com"); return Encrypt(original,key); } #endregion #region 使用 给定密钥 加密/解密/byte[] /// <summary> /// 生成MD5摘要 /// </summary> /// <param name="original">数据源</param> /// <returns>摘要</returns> public static byte[] MakeMD5(byte[] original) { MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); byte[] keyhash = hashmd5.ComputeHash(original); hashmd5 = null; return keyhash; } /// <summary> /// 使用给定密钥加密 /// </summary> /// <param name="original">明文</param> /// <param name="key">密钥</param> /// <returns>密文</returns> public static byte[] Encrypt(byte[] original, byte[] key) { TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.Key = MakeMD5(key); des.Mode = CipherMode.ECB; return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length); } /// <summary> /// 使用给定密钥解密数据 /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密钥</param> /// <returns>明文</returns> public static byte[] Decrypt(byte[] encrypted, byte[] key) { TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.Key = MakeMD5(key); des.Mode = CipherMode.ECB; return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length); } #endregion } }
代码三:
using System; using System.Collections.Generic; using System.Text; namespace MyLibs.encrypt { public class Base64 { private static string base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//编码后的字符集 private static int[] base64DecodeChars = new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };//对应ASICC字符的位置 public static string base64encode(string str) { //加密 string Out = ""; int i = 0, len = str.Length; char c1, c2, c3; while (i < len) { c1 = Convert.ToChar(str[i++] & 0xff); if (i == len) { Out += base64EncodeChars[c1 >> 2]; Out += base64EncodeChars[(c1 & 0x3) << 4]; Out += "=="; break; } c2 = str[i++]; if (i == len) { Out += base64EncodeChars[c1 >> 2]; Out += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]; Out += base64EncodeChars[(c2 & 0xF) << 2]; Out += "="; break; } c3 = str[i++]; Out += base64EncodeChars[c1 >> 2]; Out += base64EncodeChars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]; Out += base64EncodeChars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)]; Out += base64EncodeChars[c3 & 0x3F]; } return Out; } public string utf16to8(string str) { string Out = ""; int i, len; char c;//char为16位Unicode字符,范围0~0xffff,感谢vczh提醒 len = str.Length; for (i = 0; i < len; i++) {//根据字符的不同范围分别转化 c = str[i]; if ((c >= 0x0001) && (c <= 0x007F)) { Out += str[i]; } else if (c > 0x07FF) { Out += (char)(0xE0 | ((c >> 12) & 0x0F)); Out += (char)(0x80 | ((c >> 6) & 0x3F)); Out += (char)(0x80 | ((c >> 0) & 0x3F)); } else { Out += (char)(0xC0 | ((c >> 6) & 0x1F)); Out += (char)(0x80 | ((c >> 0) & 0x3F)); } } return Out; } public static string base64decode(string str) {//解密 int c1, c2, c3, c4; int i, len; string Out; len = str.Length; i = 0; Out = ""; while (i < len) { do { c1 = base64DecodeChars[str[i++] & 0xff]; } while (i < len && c1 == -1); if (c1 == -1) break; do { c2 = base64DecodeChars[str[i++] & 0xff]; } while (i < len && c2 == -1); if (c2 == -1) break; Out += (char)((c1 << 2) | ((c2 & 0x30) >> 4)); do { c3 = str[i++] & 0xff; if (c3 == 61) return Out; c3 = base64DecodeChars[c3]; } while (i < len && c3 == -1); if (c3 == -1) break; Out += (char)(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); do { c4 = str[i++] & 0xff; if (c4 == 61) return Out; c4 = base64DecodeChars[c4]; } while (i < len && c4 == -1); if (c4 == -1) break; Out += (char)(((c3 & 0x03) << 6) | c4); } return Out; } public string utf8to16(string str) { string Out = ""; int i, len; char c, char2, char3; len = str.Length; i = 0; while (i < len) { c = str[i++]; switch (c >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: Out += str[i - 1]; break; case 12: case 13: char2 = str[i++]; Out += (char)(((c & 0x1F) << 6) | (char2 & 0x3F)); break; case 14: char2 = str[i++]; char3 = str[i++]; Out += (char)(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); break; } } return Out; } } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
加载全部内容