欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#编写的Base64加密和解密类

程序员文章站 2023-01-22 12:51:07
这个c#类是一个基于base64的加密和解密类,用户可以可以使用默认的秘钥进行加密、解密,也可以自己设定秘钥进行加密和解密,非常实用 代码一:非常精简的代码 /...

这个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; 
    } 
 
  } 
} 

以上所述就是本文的全部内容了,希望大家能够喜欢。