织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>.NET(C#)>

C#实现Base64加密解密过程

jackyhwei 发布于 2011-06-10 22:35 点击:次 
传奇3外挂加密解密过程(C#)版
TAG:

加密算法1

  1. public string Base64Encode(string source) 
  2.  { 
  3.      byte[] a1,a2,a3; 
  4.      BitArray b1,b2,b3; 
  5.      a1 = Encoding.Default.GetBytes(source); 
  6.     if (a1.Length % 3 == 0) a2 = new byte[a1.Length*4/3]; 
  7.     else a2 = new byte[a1.Length*4/3+1]; 
  8.      a3 = new byte[1]; 
  9.      b1 = new BitArray(a1); 
  10.      b2 = new BitArray(6); 
  11.      b3 = new BitArray(b1.Length); 
  12.     for (int i = 0, k = b1.Length / 8; i < k; i++)  
  13.      { 
  14.         for(int j=7;j>=0;j--)b3[i*8+j]=b1[i*8+7-j]; 
  15.      } 
  16.     for (int i = 0,k=b1.Length/6; i < k; i++) 
  17.      { 
  18.         for (int j = 0; j < 6; j++) 
  19.          { 
  20.              b2[5-j] = b3[i * 6 + j]; 
  21.          } 
  22.          b2.CopyTo(a3,0); 
  23.          a2[i] = a3[0]; 
  24.      } 
  25.     if (a1.Length % 3 == 1)
  26.  a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 1] << 6) >> 2); 
  27.     else if (a1.Length % 3 == 2)
  28.  a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 1] << 4 )>> 2); 
  29.     for (int i=0; i < a2.Length; i++)
  30.  a2[i] = (byte)(a2[i] + 0x3c); 
  31.     return Encoding.Default.GetString(a2); 
  32.  } 

加密算法2

  1. public string Base64Encode(string source) 
  2.  { 
  3.      byte[] a1, a2; 
  4.      a1 = Encoding.Default.GetBytes(source); 
  5.     if (a1.Length % 3 == 0) a2 = new byte[a1.Length * 4 / 3]; 
  6.     else a2 = new byte[a1.Length * 4 / 3 + 1]; 
  7.     for (int i = 0, k = a1.Length / 3; i < k; i++)  
  8.      { 
  9.          a2[i*4] = (byte)(a1[i*3] >> 2); 
  10.          a2[i*4 + 1] = (byte)((byte)((byte)
  11. (a1[i*3] << 6) >> 2) + (byte)(a1[i*3 + 1] >> 4)); 
  12.          a2[i*4 + 2] = (byte)((byte)((byte)
  13. (a1[i*3 + 1] << 4) >> 2) + (byte)(a1[i*3 + 2] >> 6)); 
  14.          a2[i*4 + 3] = (byte)((byte)(a1[i*3 + 2] << 2) >> 2); 
  15.      } 
  16.     if (a1.Length % 3 == 1) 
  17.      { 
  18.          a2[a2.Length - 2] = (byte)(a1[a1.Length - 1] >> 2); 
  19.          a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 1] << 6) >> 2); 
  20.      } 
  21.     else 
  22.         if (a1.Length % 3 == 2) 
  23.          { 
  24.              a2[a2.Length - 3] = (byte)(a1[a1.Length - 2] >> 2); 
  25.              a2[a2.Length - 2] = (byte)((byte)((byte)
  26. (a1[a1.Length - 2] << 6) >> 2) + (byte)(a1[a1.Length - 1] >> 4));  n
  27.              a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 1] << 4) >> 2); 
  28.          } 
  29.     for (int i = 0; i < a2.Length; i++) a2[i] = (byte)(a2[i] + 0x3c); 
  30.     return Encoding.Default.GetString(a2); 
  31.  } 

 解密算法

  1. public string Base64Decode(string source)  
  2.   { 
  3.       byte[] a1, a2; 
  4.       a1 = Encoding.Default.GetBytes(source); 
  5.       a2 = new byte[a1.Length * 3 / 4]; 
  6.      for (int i = 0; i < a1.Length; i++) a1[i] = (byte)(a1[i] - 0x3c); 
  7.      for (int i = 0, k = a1.Length / 4; i < k; i++) 
  8.       { 
  9.           a2[i * 3] = (byte)((byte)
  10. (a1[i * 4] << 2) + (byte)(a1[i * 4 + 1] >> 4)); 
  11.           a2[i * 3 + 1] = (byte)((byte)
  12. (a1[i * 4 + 1] << 4) + (byte)(a1[i * 4 + 2] >> 2)); 
  13.           a2[i * 3 + 2] = (byte)((byte)
  14. (a1[i * 4 + 2] << 6) + a1[i * 4 + 3]); 
  15.       } 
  16.      if (a2.Length % 3 == 2) 
  17.       { 
  18.           a2[a2.Length - 2] = (byte)((byte)
  19. (a1[a1.Length - 3] << 2) + (byte)(a1[a1.Length - 2] >> 4)); 
  20.           a2[a2.Length - 1] = (byte)((byte)
  21. (a1[a1.Length - 2] << 4) + (byte)(a1[a1.Length - 1] >> 2)); 
  22.       } 
  23.      else 
  24.      if (a2.Length % 3 == 1) a2[a2.Length - 1] = (byte)((byte)
  25. (a1[a1.Length - 2] << 2) + (byte)(a1[a1.Length - 1] >> 4)); 
  26.      return Encoding.Default.GetString(a2); 
  27.   } 

 

(秩名)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201106/14546.html]
本文出处:网络 作者:秩名
顶一下
(4)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容