using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
Libraries
{
public
class
Crypt
{
public
Crypt()
{
this
.T =
new
List<
char
>();
this
.K =
"あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやよらりるれろわをぐげござじずぞだぢづでばびぶべぱぴぷぺぽ"
;
}
public
string
Token
{
get
{
if
(
this
.S !=
null
)
{
return
this
.S;
}
return
this
.K;
}
set
{
this
.T.Clear();
this
.S = value;
if
(
this
.S ==
null
)
{
foreach
(
char
item
in
this
.K)
{
this
.T.Add(item);
}
return
;
}
if
(
this
.S.Length < 64)
{
foreach
(
char
item2
in
this
.S)
{
this
.T.Add(item2);
}
for
(
int
j = 0; j < 64 -
this
.S.Length; j++)
{
this
.T.Add(
this
.K[j]);
}
return
;
}
for
(
int
k = 0; k < 64; k++)
{
this
.T.Add(
this
.S[k]);
}
}
}
public
string
Encode(
string
x)
{
if
(!
string
.IsNullOrEmpty(x))
{
return
this
.InternalEncode(Encoding.UTF8.GetBytes(x));
}
return
x;
}
public
string
Decode(
string
x)
{
if
(!
string
.IsNullOrEmpty(x))
{
return
Encoding.UTF8.GetString(
this
.InternalDecode(x));
}
return
x;
}
public
byte
[] Encode(
byte
[] x)
{
if
(x !=
null
)
{
return
Encoding.UTF8.GetBytes(
this
.InternalEncode(x));
}
return
null
;
}
public
byte
[] Decode(
byte
[] x)
{
if
(x !=
null
)
{
return
this
.InternalDecode(Encoding.UTF8.GetString(x));
}
return
null
;
}
private
void
CheckToken()
{
if
(
this
.T.Count != 64)
{
this
.Token =
this
.K;
}
}
private
byte
[] InternalDecode(
string
x)
{
this
.CheckToken();
int
num = 0;
int
num2 = x.Length / 4;
int
num3 = x.Length % 4;
byte
[] array;
if
(num3 == 0)
{
array =
new
byte
[3 * num2];
}
else
{
array =
new
byte
[3 * num2 + num3 - 1];
string
text =
string
.Empty;
for
(
int
i = num3; i > 0; i--)
{
text +=
this
.ByteToBin((
byte
)
this
.T.IndexOf(x[x.Length - i])).Substring(2);
}
for
(
int
j = 0; j < num3 - 1; j++)
{
array[3 * num2 + j] =
this
.BinToByte(text.Substring(8 * j, 8));
}
}
for
(
int
k = 0; k < num2; k++)
{
string
text =
string
.Empty;
for
(
int
l = 0; l < 4; l++)
{
text +=
this
.ByteToBin((
byte
)
this
.T.IndexOf(x[4 * k + l])).Substring(2);
}
for
(
int
m = 0; m < text.Length / 8; m++)
{
array[num++] =
this
.BinToByte(text.Substring(8 * m, 8));
}
}
return
array;
}
private
string
InternalEncode(
byte
[] x)
{
this
.CheckToken();
string
text =
string
.Empty;
int
num = x.Length / 3;
int
num2 = x.Length % 3;
for
(
int
i = 0; i < num; i++)
{
string
text2 =
string
.Empty;
for
(
int
j = 0; j < 3; j++)
{
text2 +=
this
.ByteToBin(x[3 * i + j]);
}
text +=
this
.cryptEncode(text2);
}
if
(num2 == 1)
{
string
text2 =
this
.ByteToBin(x[x.Length - 1]).PadRight(12,
'0'
);
text +=
this
.cryptEncode(text2);
}
else
if
(num2 == 2)
{
string
text2 =
string
.Empty;
for
(
int
k = num2; k > 0; k--)
{
text2 +=
this
.ByteToBin(x[x.Length - k]);
}
text2 = text2.PadRight(18,
'0'
);
text +=
this
.cryptEncode(text2);
}
return
text;
}
private
string
cryptEncode(
string
x)
{
string
text =
string
.Empty;
for
(
int
i = 0; i < x.Length / 6; i++)
{
text +=
this
.T[(
int
)
this
.BinToByte(x.Substring(6 * i, 6))].ToString();
}
return
text;
}
private
string
ByteToBin(
byte
x)
{
return
Convert.ToString(x, 2).PadLeft(8,
'0'
);
}
private
byte
BinToByte(
string
x)
{
return
Convert.ToByte(x, 2);
}
private
string
S;
private
string
K;
private
List<
char
> T;
}
}