📄 des加密算法在c#下的实现.txt
字号:
//循环左移1位
for (int i = 1; i < 28; i++)
{
c1[i - 1] = c0;
d1[i - 1] = d0;
}
c1[27] = c0[0];
d1[27] = d0[0];
//循环左移1位
for (int i = 1; i < 28; i++)
{
c2[i - 1] = c1;
d2[i - 1] = d1;
}
c2[27] = c1[0];
d2[27] = d1[0];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c3[i - 2] = c2;
d3[i - 2] = d2;
}
c3[26] = c2[0];
c3[27] = c2[1];
d3[26] = d2[0];
d3[27] = d2[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c4[i - 2] = c3;
}
c4[26] = c3[0];
c4[27] = c3[1];
for (int i = 2; i < 28; i++)
{
d4[i - 2] = d3;
}
d4[26] = d3[0];
d4[27] = d3[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c5[i - 2] = c4;
}
c5[26] = c4[0];
c5[27] = c4[1];
for (int i = 2; i < 28; i++)
{
d5[i - 2] = d4;
}
d5[26] = d4[0];
d5[27] = d4[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c6[i - 2] = c5;
}
c6[26] = c5[0];
c6[27] = c5[1];
for (int i = 2; i < 28; i++)
{
d6[i - 2] = d5;
}
d6[26] = d5[0];
d6[27] = d5[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c7[i - 2] = c6;
}
c7[26] = c6[0];
c7[27] = c6[1];
for (int i = 2; i < 28; i++)
{
d7[i - 2] = d6;
}
d7[26] = d6[0];
d7[27] = d6[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c8[i - 2] = c7;
}
c8[26] = c7[0];
c8[27] = c7[1];
for (int i = 2; i < 28; i++)
{
d8[i - 2] = d7;
}
d8[26] = d7[0];
d8[27] = d7[1];
//循环左移1位
for (int i = 1; i < 28; i++)
{
c9[i - 1] = c8;
}
c9[27] = c8[0];
for (int i = 1; i < 28; i++)
{
d9[i - 1] = d8;
}
d9[27] = d8[0];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c10[i - 2] = c9;
}
c10[26] = c9[0];
c10[27] = c9[1];
for (int i = 2; i < 28; i++)
{
d10[i - 2] = d9;
}
d10[26] = d9[0];
d10[27] = d9[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c11[i - 2] = c10;
}
c11[26] = c10[0];
c11[27] = c10[1];
for (int i = 2; i < 28; i++)
{
d11[i - 2] = d10;
}
d11[26] = d10[0];
d11[27] = d10[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c12[i - 2] = c11;
}
c12[26] = c11[0];
c12[27] = c11[1];
for (int i = 2; i < 28; i++)
{
d12[i - 2] = d11;
}
d12[26] = d11[0];
d12[27] = d11[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c13[i - 2] = c12;
}
c13[26] = c12[0];
c13[27] = c12[1];
for (int i = 2; i < 28; i++)
{
d13[i - 2] = d12;
}
d13[26] = d12[0];
d13[27] = d12[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c14[i - 2] = c13;
}
c14[26] = c13[0];
c14[27] = c13[1];
for (int i = 2; i < 28; i++)
{
d14[i - 2] = d13;
}
d14[26] = d13[0];
d14[27] = d13[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c15[i - 2] = c14;
}
c15[26] = c14[0];
c15[27] = c14[1];
for (int i = 2; i < 28; i++)
{
d15[i - 2] = d14;
}
d15[26] = d14[0];
d15[27] = d14[1];
//循环左移1位
for (int i = 1; i < 28; i++)
{
c16[i - 1] = c15;
}
c16[27] = c15[0];
for (int i = 1; i < 28; i++)
{
d16[i - 1] = d15;
}
d16[27] = d15[0];
//合并Ci,Di并由PC2置换计算最终16个密钥
k1 = PC2(c1, d1);
k2 = PC2(c2, d2);
k3 = PC2(c3, d3);
k4 = PC2(c4, d4);
k5 = PC2(c5, d5);
k6 = PC2(c6, d6);
k7 = PC2(c7, d7);
k8 = PC2(c8, d8);
k9 = PC2(c9, d9);
k10 = PC2(c10, d10);
k11 = PC2(c11, d11);
k12 = PC2(c12, d12);
k13 = PC2(c13, d13);
k14 = PC2(c14, d14);
k15 = PC2(c15, d15);
k16 = PC2(c16, d16);
keyreturn[0] = (int[])k1.Clone();
keyreturn[1] = (int[])k2.Clone();
keyreturn[2] = (int[])k3.Clone();
keyreturn[3] = (int[])k4.Clone();
keyreturn[4] = (int[])k5.Clone();
keyreturn[5] = (int[])k6.Clone();
keyreturn[6] = (int[])k7.Clone();
keyreturn[7] = (int[])k8.Clone();
keyreturn[8] = (int[])k9.Clone();
keyreturn[9] = (int[])k10.Clone();
keyreturn[10] = (int[])k11.Clone();
keyreturn[11] = (int[])k12.Clone();
keyreturn[12] = (int[])k13.Clone();
keyreturn[13] = (int[])k14.Clone();
keyreturn[14] = (int[])k15.Clone();
keyreturn[15] = (int[])k16.Clone();
return keyreturn;
}
//PC2置换
public int[] PC2(int[] c, int[] d)
{
int[] temp = new int[56];
int[] k = new int[48];
//合并Ci和Di
for (int i = 0; i < 28; i++)
{
temp = c;
temp[i + 28] = d;
}
//置换表
k[0] = temp[13];
k[1] = temp[16];
k[2] = temp[10];
k[3] = temp[23];
k[4] = temp[0];
k[5] = temp[4];
k[6] = temp[2];
k[7] = temp[27];
k[8] = temp[14];
k[9] = temp[5];
k[10] = temp[20];
k[11] = temp[9];
k[12] = temp[22];
k[13] = temp[18];
k[14] = temp[11];
k[15] = temp[3];
k[16] = temp[25];
k[17] = temp[7];
k[18] = temp[15];
k[19] = temp[6];
k[20] = temp[26];
k[21] = temp[19];
k[22] = temp[12];
k[23] = temp[1];
k[24] = temp[40];
k[25] = temp[51];
k[26] = temp[30];
k[27] = temp[36];
k[28] = temp[46];
k[29] = temp[54];
k[30] = temp[29];
k[31] = temp[39];
k[32] = temp[50];
k[33] = temp[44];
k[34] = temp[32];
k[35] = temp[47];
k[36] = temp[43];
k[37] = temp[48];
k[38] = temp[38];
k[39] = temp[55];
k[40] = temp[33];
k[41] = temp[52];
k[42] = temp[45];
k[43] = temp[41];
k[44] = temp[49];
k[45] = temp[35];
k[46] = temp[28];
k[47] = temp[31];
return k;
}
//E拓展 将32位明文2进制形式拓展为48位
public int[] E(int[] r)
{
int i;
int[] AfterE = new int[48];
AfterE[0] = r[31];
for (i = 1; i <= 5; i++)
{
AfterE = r[i - 1];
}
for (i = 6; i <= 11; i++)
{
AfterE = r[i - 3];
}
for (i = 12; i <= 17; i++)
{
AfterE = r[i - 5];
}
for (i = 18; i <= 23; i++)
{
AfterE = r[i - 7];
}
for (i = 24; i <= 29; i++)
{
AfterE = r[i - 9];
}
for (i = 30; i <= 35; i++)
{
AfterE = r[i - 11];
}
for (i = 36; i <= 41; i++)
{
AfterE = r[i - 13];
}
for (i = 42; i <= 46; i++)
{
AfterE = r[i - 15];
}
AfterE[47] = r[0];
return AfterE;
}
//g函数
public int[] g(int[] r, int[] k)
{
int[] afterE = E(r);//将明文进行E拓展
//E拓展后明文分6组的初始化
int[] m1 = new int[6]; int[] m4 = new int[6]; int[] m7 = new int[6];
int[] m2 = new int[6]; int[] m5 = new int[6]; int[] m8 = new int[6];
int[] m3 = new int[6]; int[] m6 = new int[6];
//明文经过S盒后存放数组初始化
int[] afterS1 = new int[4]; int[] afterS5 = new int[4];
int[] afterS2 = new int[4]; int[] afterS6 = new int[4];
int[] afterS3 = new int[4]; int[] afterS7 = new int[4];
int[] afterS4 = new int[4]; int[] afterS8 = new int[4];
int[] afterS = new int[32];
int[] greturn = new int[32];
int temp;
//E拓展后明文与密钥异或
for (int i = 0; i < 48; i++)
{
if (afterE != k) afterE = 1; else afterE = 0;
}
//将明文分组
for (int i = 0; i < 6; i++)
{
m1 = afterE;
m2 = afterE[i + 6];
m3 = afterE[i + 12];
m4 = afterE[i + 18];
m5 = afterE[i + 24];
m6 = afterE[i + 30];
m7 = afterE[i + 36];
m8 = afterE[i + 42];
}
//6组明文分别经过S盒后各自输出为afterS1...afterS8
temp = getS(m1, 1);
if ((temp & 0x8) != 0) afterS1[0] = 1;
if ((temp & 0x4) != 0) afterS1[1] = 1;
if ((temp & 0x2) != 0) afterS1[2] = 1;
if ((temp & 0x1) != 0) afterS1[3] = 1;
temp = getS(m2, 2);
if ((temp & 0x8) != 0) afterS2[0] = 1;
if ((temp & 0x4) != 0) afterS2[1] = 1;
if ((temp & 0x2) != 0) afterS2[2] = 1;
if ((temp & 0x1) != 0) afterS2[3] = 1;
temp = getS(m3, 3);
if ((temp & 0x8) != 0) afterS3[0] = 1;
if ((temp & 0x4) != 0) afterS3[1] = 1;
if ((temp & 0x2) != 0) afterS3[2] = 1;
if ((temp & 0x1) != 0) afterS3[3] = 1;
temp = getS(m4, 4);
if ((temp & 0x8) != 0) afterS4[0] = 1;
if ((temp & 0x4) != 0) afterS4[1] = 1;
if ((temp & 0x2) != 0) afterS4[2] = 1;
if ((temp & 0x1) != 0) afterS4[3] = 1;
temp = getS(m5, 5);
if ((temp & 0x8) != 0) afterS5[0] = 1;
if ((temp & 0x4) != 0) afterS5[1] = 1;
if ((temp & 0x2) != 0) afterS5[2] = 1;
if ((temp & 0x1) != 0) afterS5[3] = 1;
temp = getS(m6, 6);
if ((temp & 0x8) != 0) afterS6[0] = 1;
if ((temp & 0x4) != 0) afterS6[1] = 1;
if ((temp & 0x2) != 0) afterS6[2] = 1;
if ((temp & 0x1) != 0) afterS6[3] = 1;
temp = getS(m7, 7);
if ((temp & 0x8) != 0) afterS7[0] = 1;
if ((temp & 0x4) != 0) afterS7[1] = 1;
if ((temp & 0x2) != 0) afterS7[2] = 1;
if ((temp & 0x1) != 0) afterS7[3] = 1;
temp = getS(m8, 8);
if ((temp & 0x8) != 0) afterS8[0] = 1;
if ((temp & 0x4) != 0) afterS8[1] = 1;
if ((temp & 0x2) != 0) afterS8[2] = 1;
if ((temp & 0x1) != 0) afterS8[3] = 1;
//合并经过S盒后的明文
for (int i = 0; i < 4; i++)
{
afterS = afterS1;
afterS[i + 4] = afterS2;
afterS[i + 8] = afterS3;
afterS[i + 12] = afterS4;
afterS[i + 16] = afterS5;
afterS[i + 20] = afterS6;
afterS[i + 24] = afterS7;
afterS[i + 28] = afterS8;
}
//P置换
greturn[0] = afterS[15];
greturn[1] = afterS[6];
greturn[2] = afterS[19];
greturn[3] = afterS[20];
greturn[4] = afterS[28];
greturn[5] = afterS[11];
greturn[6] = afterS[27];
greturn[7] = afterS[16];
greturn[8] = afterS[0];
greturn[9] = afterS[14];
greturn[10] = afterS[22];
greturn[11] = afterS[25];
greturn[12] = afterS[4];
greturn[13] = afterS[17];
greturn[14] = afterS[30];
greturn[15] = afterS[9];
greturn[16] = afterS[1];
greturn[17] = afterS[7];
greturn[18] = afterS[23];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -