110,538
社区成员
发帖
与我相关
我的任务
分享
public static void Base64Code()
{
char[] Base64Code = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
'8','9','+','/','='};
string str = "林赵钱孙李A";
List<byte> listTwo = Encoding.Default.GetBytes(str).ToList<byte>();
string strThree = string.Empty, strFour = string.Empty, result = string.Empty;
int yushu = listTwo.Count % 3;
if (yushu > 0)
{
for (int i = 0; i < 3 - yushu; i++)
listTwo.Add(0);
}
for (int i = 0; i < listTwo.Count; i++)
{
strThree += Convert.ToString(listTwo[i], 2).PadLeft(8, '0');
}
for (int i = 0; i < strThree.Length; i += 6)
{
strFour = "00" + strThree.Substring(i, 6);
int index = Convert.ToInt32(strFour, 2);
result += Base64Code[index];
}
Console.WriteLine(result);
}
public static void StringToBase64()
{
byte[] bytes = Encoding.Default.GetBytes("林赵钱孙李A");
Console.WriteLine(Convert.ToBase64String(bytes));
}
public static string StringToBase64(string str)
{
string base64Arr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
byte[] bytes = Encoding.Default.GetBytes(str);
int PrevBitCount = 0, NextBitCount = 6;
int BitCount = bytes.Length * 8;
int i = 0, n = 0;
byte b=0;
StringBuilder sb = new StringBuilder(BitCount / 6 + 1);
while (true)
{
if (PrevBitCount > 0 && NextBitCount > 0)
{
b = (byte)((bytes[i] << NextBitCount) | (bytes[i + 1] >> (8 - NextBitCount)));
b = (byte)(b & 0x3f);
PrevBitCount = 8 - NextBitCount;
NextBitCount = 6 - PrevBitCount;
}
else if (PrevBitCount == 0)
{
b = (byte)(bytes[i] >> 2);
PrevBitCount = 2;
NextBitCount = 4;
}
else if (NextBitCount == 0)
{
b = (byte)(bytes[i] & 0x3f);
PrevBitCount = 0;
NextBitCount = 6;
}
sb.Append(base64Arr[b]);
n += 6;
i = n / 8;
int SurplusBit = BitCount - n;
if (SurplusBit < 6)
{
if (SurplusBit > 0)
{
b = bytes[bytes.Length - 1];
b = (byte)(b << (6 - SurplusBit) & 0x3f);
sb.Append(base64Arr[b]);
}
break;
}
}
n = BitCount % 3;
for (i = 0; i < n; i++)
{
sb.Append("=");
}
return sb.ToString();
}
outstr[0] = instr[0] >> 2;
outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >> 4);
if (!instr[1].Equals(empty))
outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >> 6);
else
outstr[2] = 64;
if (!instr[2].Equals(empty))
outstr[3] = (instr[2] & 0x3f);
else
outstr[3] = 64;
public static void Base64Code(string Message)
{
char[] Base64Code = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
'8','9','+','/','='};
byte empty = (byte)0;
System.Collections.ArrayList byteMessage = new System.Collections.ArrayList(System.Text.Encoding.Default.GetBytes(Message));
System.Text.StringBuilder outmessage;
int messageLen = byteMessage.Count;
int page = messageLen / 3;
int use = 0;
if ((use = messageLen % 3) > 0)
{
for (int i = 0; i < 3 - use; i++)
byteMessage.Add(empty);
page++;
}
outmessage = new System.Text.StringBuilder(page * 4);
for (int i = 0; i < page; i++)
{
byte[] instr = new byte[3];
instr[0] = (byte)byteMessage[i * 3];
instr[1] = (byte)byteMessage[i * 3 + 1];
instr[2] = (byte)byteMessage[i * 3 + 2];
int[] outstr = new int[4];
outstr[0] = instr[0] >> 2;
outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >> 4);
if (!instr[1].Equals(empty))
outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >> 6);
else
outstr[2] = 64;
if (!instr[2].Equals(empty))
outstr[3] = (instr[2] & 0x3f);
else
outstr[3] = 64;
outmessage.Append(Base64Code[outstr[0]]);
outmessage.Append(Base64Code[outstr[1]]);
outmessage.Append(Base64Code[outstr[2]]);
outmessage.Append(Base64Code[outstr[3]]);
}
Console.WriteLine(outmessage.ToString());
}