📄 des.cs
字号:
for (i = 0; i < num; i++)
{
ints = nints;
nints = char2ints(str[i], ints, i * 8);
}
return nints;
}
private string ints2str(int[] oints, int num)
{
int tem = 0;
string str = null;
for (int i = 0; i < num; i++)
{
if (i % 8 == 0 && i != 0)
{
str += (char)tem;
tem = 0;
}
else
tem = tem * 2 + oints[i];
}
str += (char)tem;
return str;
}
private int[] char2ints(char ch, int[] oints, int num)
{//convert a char to a certain form int[];
int i, n = (int)ch;
int[] nints = oints;
for (i = num + 7; i >= num; i--)
{
nints[i] = n % 2;
n = n / 2;
}
return nints;
}
private int[] odd(int[] oints)
{
int[] nints = new int[64];//store the new 64 bit ints
int cnt;//count the number of 1
int inc;//the increase of nints
int i;//for the loop
for (i = 1, inc = 0, cnt = 0; i <= oints.Length; i++)
{
if (i != 0 && i % 8 == 0)
{
if (cnt % 2 == 1)
nints[inc++] = 0;
else
nints[inc++] = 1;
nints[inc++] = oints[i - 1];
cnt = 0;
}
else
nints[inc++] = oints[i - 1];
if (oints[i - 1] == 1)
cnt++;
}
return nints;
}
private int[][] Division(int[] ints, int Dnum, int Dsize)//ints:large ints;Dnum:block number;Dsize:block size;
{//convert the large ints to small blocks
int[][] Dpart = new int[Dnum][];//to store the small block
int i, j, inc = 0;
for (i = 0; i < Dnum; i++)
{
Dpart[i] = new int[Dsize];
for (j = 0; j < Dsize && inc < ints.Length; j++)
Dpart[i][j] = ints[inc++];
}
return Dpart;
}
private int[] Permutation(int[] oints, int[] Prule)//oints:old ints for permutation;Prule:the rule for permutation;
{//permutate a ints to what we want;
int[] nints = new int[Prule.Length];// to store the result of permutation
for (int i = 0; i < Prule.Length; i++)
{
nints[i] = oints[Prule[i] - 1];
}
return nints;
}
private int[] LeftShift(int[] oints, int offset) //oints:the ints for shifting;offset:the bit to left shift
{//left shift proc
int tem, i, j;
tem = 0;
for (i = 0; i < offset; i++)
{
for (j = 0; j < oints.Length; j++)
{
if (j == 0)
{
tem = oints[j];
continue;
}
else
oints[j - 1] = oints[j];
}
oints[j - 1] = tem;
}
return oints;
}
private int[] intsAdd(int[][] ints, int num)//the ints in the int[][] added to one ints;
{
int i, j;
for (i = 0, j = 0; i < num; i++)
j += ints[i].Length;
int[] sum = new int[j];
int inc = 0;
//int l = ints.Length;
for (i = 0; i < num; i++)
for (j = 0; j < ints[i].Length; j++)
sum[inc++] = ints[i][j];
return sum;
}
private int[] xor(int[] ints1, int[] ints2)//ints1 xor ints2 to ints1;
{//XOR
if (ints1.Length != ints2.Length)
{
MessageBox.Show("wrong in xor");
return null;
}
for (int i = 0; i < ints1.Length; i++)
ints1[i] = (ints1[i] == ints2[i] ? 0 : 1);
return ints1;
}
private int[] sBox(int[] oints)
{//sbox func
int[][] dints;
dints = Division(oints, sBoxNum, sBoxSize);
for (int i = 0; i < sBoxNum; i++)
{
dints[i] = Substitution(dints[i], sbox[i]);
}
oints = intsAdd(dints, dints.Length);
return oints;
}
private int[] Substitution(int[] oints, int[,] sBox)//
{//Substitution fuc
if (oints.Length != 6)
{
MessageBox.Show("wrong at Substitution!");
return null;
}
int tem = 0;
int[] nints = new int[SubResult];
tem = sBox[oints[0] * 2 + oints[5], oints[1] * 8 + oints[2] * 4 + oints[3] * 2 + oints[4]];
for (int i = SubResult - 1; i >= 0; i--)
{
nints[i] = tem % 2;
tem = tem / 2;
}
return nints;
}
private string bints2str( int[] oints)
{
string str=null;
for ( int i = 0 ; i < oints.Length ; i++ )
str += oints[i].ToString();
return str;
}
private int[] bstr2ints( string str)
{
int[] ints = new int[str.Length];
for ( int i = 0 ; i<str.Length ; i++ )
ints[i] = (str[i] == '0' ? 0 : 1 );
return ints;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -