|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.IO; |
|
|
using System.Text; |
|
|
|
|
|
namespace WpfEditor |
|
|
{ |
|
|
public static class Base64 |
|
|
{ |
|
|
|
|
|
private static readonly string Base64Chars = |
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
|
|
|
|
|
|
|
private static bool IsBase64(char c) |
|
|
{ |
|
|
return (char.IsLetterOrDigit(c) || (c == '+') || (c == '/')); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string Base64Encode(string imagePath) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
|
|
|
byte[] data = File.ReadAllBytes(imagePath); |
|
|
|
|
|
|
|
|
return Convert.ToBase64String(data); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
Console.WriteLine($"无法打开文件: {imagePath}, 错误: {ex.Message}"); |
|
|
return string.Empty; |
|
|
} |
|
|
} |
|
|
public static string Base64Encode(byte[] data) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
return Convert.ToBase64String(data); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
return string.Empty; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string Base64EncodeManual(string imagePath) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
|
|
|
byte[] data = File.ReadAllBytes(imagePath); |
|
|
|
|
|
StringBuilder ret = new StringBuilder(); |
|
|
int i = 0; |
|
|
int j = 0; |
|
|
byte[] char_array_3 = new byte[3]; |
|
|
byte[] char_array_4 = new byte[4]; |
|
|
int data_size = data.Length; |
|
|
int data_index = 0; |
|
|
|
|
|
while (data_index < data_size) |
|
|
{ |
|
|
char_array_3[i++] = data[data_index++]; |
|
|
if (i == 3) |
|
|
{ |
|
|
char_array_4[0] = (byte)((char_array_3[0] & 0xfc) >> 2); |
|
|
char_array_4[1] = (byte)(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4)); |
|
|
char_array_4[2] = (byte)(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6)); |
|
|
char_array_4[3] = (byte)(char_array_3[2] & 0x3f); |
|
|
|
|
|
for (i = 0; i < 4; i++) |
|
|
ret.Append(Base64Chars[(int)char_array_4[i]]); |
|
|
i = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
if (i > 0) |
|
|
{ |
|
|
for (j = i; j < 3; j++) |
|
|
char_array_3[j] = 0; |
|
|
|
|
|
char_array_4[0] = (byte)((char_array_3[0] & 0xfc) >> 2); |
|
|
char_array_4[1] = (byte)(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4)); |
|
|
char_array_4[2] = (byte)(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6)); |
|
|
char_array_4[3] = (byte)(char_array_3[2] & 0x3f); |
|
|
|
|
|
for (j = 0; j < i + 1; j++) |
|
|
ret.Append(Base64Chars[(int)char_array_4[j]]); |
|
|
|
|
|
while (i++ < 3) |
|
|
ret.Append('='); |
|
|
} |
|
|
|
|
|
return ret.ToString(); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
Console.WriteLine($"无法打开文件: {imagePath}, 错误: {ex.Message}"); |
|
|
return string.Empty; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static byte[] Base64Decode(string input) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
|
|
|
return Convert.FromBase64String(input); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
Console.WriteLine($"Base64解码错误: {ex.Message}"); |
|
|
return new byte[0]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static byte[] Base64DecodeManual(string input) |
|
|
{ |
|
|
|
|
|
int[] decode_table = new int[256]; |
|
|
for (int i = 0; i < 256; i++) |
|
|
decode_table[i] = -1; |
|
|
|
|
|
for (int i = 0; i < 64; i++) |
|
|
decode_table[Base64Chars[i]] = i; |
|
|
|
|
|
List<byte> decoded = new List<byte>(); |
|
|
int val = 0, bits = -8; |
|
|
|
|
|
foreach (char c in input) |
|
|
{ |
|
|
if (c == '=') break; |
|
|
if (decode_table[c] == -1) continue; |
|
|
|
|
|
val = (val << 6) + decode_table[c]; |
|
|
bits += 6; |
|
|
if (bits >= 0) |
|
|
{ |
|
|
decoded.Add((byte)((val >> bits) & 0xFF)); |
|
|
bits -= 8; |
|
|
} |
|
|
} |
|
|
|
|
|
return decoded.ToArray(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void SaveBase64ToFile(string base64Data, string outputFile) |
|
|
{ |
|
|
byte[] binaryData = Base64Decode(base64Data); |
|
|
File.WriteAllBytes(outputFile, binaryData); |
|
|
} |
|
|
} |
|
|
} |