1 /// <summary>自定义包含指定字符的base64工具</summary> 2 internal static class Base64Helper 3 { 4 static readonly string base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"; 5 static readonly int[] base64Index = new int[] 6 { 7 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 8 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 9 -1,63,-1,-1,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1, 10 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, 11 -1,-1,-1,62,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42, 12 43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,-1 13 }; 14 public static byte[] FromBase64String(string inData) 15 { 16 int inDataLength = inData.Length; 17 int lengthmod4 = inDataLength % 4; 18 int calcLength = (inDataLength - lengthmod4); 19 byte[] outData = new byte[inDataLength / 4 * 3 + 3]; 20 int j = 0; 21 int i; 22 int num1, num2, num3, num4; 23 24 for (i = 0; i < calcLength; i += 4, j += 3) 25 { 26 num1 = base64Index[inData[i]]; 27 num2 = base64Index[inData[i + 1]]; 28 num3 = base64Index[inData[i + 2]]; 29 num4 = base64Index[inData[i + 3]]; 30 31 outData[j] = (byte)((num1 << 2) | (num2 >> 4)); 32 outData[j + 1] = (byte)(((num2 << 4) & 0xf0) | (num3 >> 2)); 33 outData[j + 2] = (byte)(((num3 << 6) & 0xc0) | (num4 & 0x3f)); 34 } 35 i = calcLength; 36 switch (lengthmod4) 37 { 38 case 3: 39 num1 = base64Index[inData[i]]; 40 num2 = base64Index[inData[i + 1]]; 41 num3 = base64Index[inData[i + 2]]; 42 43 outData[j] = (byte)((num1 << 2) | (num2 >> 4)); 44 outData[j + 1] = (byte)(((num2 << 4) & 0xf0) | (num3 >> 2)); 45 j += 2; 46 break; 47 case 2: 48 num1 = base64Index[inData[i]]; 49 num2 = base64Index[inData[i + 1]]; 50 51 outData[j] = (byte)((num1 << 2) | (num2 >> 4)); 52 j += 1; 53 break; 54 } 55 Array.Resize(ref outData, j); 56 return outData; 57 } 58 public static string ToBase64String(byte[] inData) 59 { 60 int inDataLength = inData.Length; 61 int outDataLength = (int)(inDataLength / 3 * 4) + 4; 62 char[] outData = new char[outDataLength]; 63 64 int lengthmod3 = inDataLength % 3; 65 int calcLength = (inDataLength - lengthmod3); 66 int j = 0; 67 int i; 68 69 for (i = 0; i < calcLength; i += 3, j += 4) 70 { 71 outData[j] = base64Table[inData[i] >> 2]; 72 outData[j + 1] = base64Table[((inData[i] & 0x03) << 4) | (inData[i + 1] >> 4)]; 73 outData[j + 2] = base64Table[((inData[i + 1] & 0x0f) << 2) | (inData[i + 2] >> 6)]; 74 outData[j + 3] = base64Table[(inData[i + 2] & 0x3f)]; 75 } 76 77 i = calcLength; 78 switch (lengthmod3) 79 { 80 case 2: 81 outData[j] = base64Table[inData[i] >> 2]; 82 outData[j + 1] = base64Table[((inData[i] & 0x03) << 4) | (inData[i + 1] >> 4)]; 83 outData[j + 2] = base64Table[(inData[i + 1] & 0x0f) << 2]; 84 j += 3; 85 break; 86 case 1: 87 outData[j] = base64Table[inData[i] >> 2]; 88 outData[j + 1] = base64Table[(inData[i] & 0x03) << 4]; 89 j += 2; 90 break; 91 } 92 return new string(outData, 0, j); 93 } 94 public static string Base64Encode(string source) 95 { 96 byte[] barray = Encoding.Default.GetBytes(source); 97 return Base64Helper.ToBase64String(barray); 98 } 99 public static string Base64Decode(string source) 100 { 101 byte[] barray = Base64Helper.FromBase64String(source); 102 return Encoding.Default.GetString(barray); 103 } 104 }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/2141.html