C # The base64 encryption and decryption algorithm class that can customize the code table,

Source: Internet
Author: User

C # The base64 encryption and decryption algorithm class that can customize the code table,

000

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Libraries 8 {9 public class Base64Crypt 10 {11 private string S; 12 private string K; 13 private List <char> T; 14 public Base64Crypt () 15 {16 T = new List <char> (); 17 K = "zookeeper when there are too many threads, there are too many threads, too many threads "; 18 // K = "ABCDEFGHI JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 +/"; // standard code table 19} 20 public string Token 21 {22 get 23 {24 return S = null? K: S; 25} 26 set 27 {28 T. clear (); 29 S = value; 30 if (S = null) 31 {32 foreach (var item in K) 33 {34 T. add (item); 35} 36} else if (S. length <64) 37 {38 foreach (var item in S) 39 {40 T. add (item); 41} 42 for (int I = 0; I <64-S.Length; I ++) 43 {44 T. add (K [I]); 45} 46} 47 else 48 {49 for (int I = 0; I <64; I ++) 50 {51 T. add (S [I]); 52} 53} 54} 55} 56 57 public string Encode (s Tring x) 58 {59 return string. IsNullOrEmpty (x )? X: InternalEncode (Encoding. UTF8.GetBytes (x); 60} 61 public string Decode (string x) 62 {63 return string. IsNullOrEmpty (x )? X: Encoding. UTF8.GetString (InternalDecode (x); 64} 65 66 public byte [] Encode (byte [] x) 67 {68 return x = null? Null: Encoding. UTF8.GetBytes (InternalEncode (x); 69} 70 public byte [] Decode (byte [] x) 71 {72 return x = null? Null: InternalDecode (Encoding. UTF8.GetString (x); 73} 74 private void CheckToken () 75 {76 if (T. Count! = 64) 77 {78 Token = K; 79} 80} 81 private byte [] InternalDecode (string x) 82 {83 CheckToken (); 84 byte [] r; 85 string t; 86 int p = 0; 87 int m = x. length/4; 88 int n = x. length % 4; 89 if (n = 0) 90 {91 r = new byte [3 * m]; 92} 93 else 94 {95 r = new byte [3 * m + n-1]; 96 t = string. empty; 97 98 for (int I = n; I> 0; I --) 99 {100 t + = ByteToBin (byte) T. indexOf (x [x. length-I]). substring (2); 101} 102 103 for (int I = 0; I <n-1; I ++) 104 {105 r [3 * m + I] = BinToByte (t. substring (8 * I, 8); 106} 107} 108 for (int I = 0; I <m; I ++) 109 {110 t = string. empty; 111 for (int j = 0; j <4; j ++) 112 {113 t + = ByteToBin (byte) T. indexOf (x [4 * I + j]). substring (2); 114} 115 for (int j = 0; j <t. length/8; j ++) 116 {117 r [p ++] = BinToByte (t. substring (8 * j, 8); 118} 119} 120 return r; 121} 122 private string InternalEncode (byte [] x) 123 {124 CheckToken (); 125 string r = string. empty; 126 string t; 127 int m = x. length/3; 128 int n = x. length % 3; 129 for (int I = 0; I <m; I ++) 130 {131 t = string. empty; 132 for (int j = 0; j <3; j ++) 133 {134 t + = ByteToBin (x [3 * I + j]); 135} 136 r + = base64Encode (t); 137} 138 if (n = 1) 139 {140 t = ByteToBin (x [x. length-1]). padRight (12, '0'); 142 r + = base64Encode (t); 143} 144 else if (n = 2) 145 {146 t = string. empty; 147 for (int I = n; I> 0; I --) 148 {149 t + = ByteToBin (x [x. length-I]); 150} 151 t = t. padRight (18, '0'); 152 r + = base64Encode (t); 153} 154 return r; 155} 156 private string base64Encode (string x) 157 {158 string r = string. empty; 159 for (int I = 0; I <x. length/6; I ++) 160 {161 r + = T [BinToByte (x. substring (6 * I, 6)]; 162} 163 return r; 164} 165 166 private string ByteToBin (byte x) 167 {168 return Convert. toString (x, 2 ). padLeft (8, '0'); 169} 170 private byte BinToByte (string x) 171 {172 return Convert. toByte (x, 2); 173} 174 175} 176} 177

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.