此程序分两部分,第一部分为主程序,另一部分为函数库
本程序中用int[]表示char的2进制形式,如a=int[8]{0,1,1,0,0,0,0,1}
下面副程序 本程序在c# 2005下通过。。
#region using directives
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.windows.forms;
using system.io;
#endregion
namespace des_2nd
{
partial class form1 : form
{
public form1()
{
initializecomponent();
}
func.func f = new func.func(); // 初始化函数库
private void button1_click(object sender, eventargs e)
{
char[] tempmchar = mbox.text.tochararray(); //原始明文
int mod = tempmchar.length % 8; //扩展明文为8的倍数
char[] mchar = new char[tempmchar.length + (8 - mod)];
for (int i = 0; i < tempmchar.length; i++)
{
mchar[i] = tempmchar[i];
}
for (int i = tempmchar.length; i < mchar.length; i++)
{
mchar[i] = ;
}
char[] kchar = kbox.text.tochararray();//获得64位密钥
char[] cchar = new char[mchar.length];//初始化密文数组
int[][] keys = f.makekey(kchar);//由makekey函数通过64位密钥计算16个48位密钥
int div = mchar.length / 8;//每8个为一组对明文分组
if ((mchar.length % 8) != 0) div += 1;
char[][] divmchar = new char[div][];//存放分组明文
for (int i = 0; i < div; i++)
{
divmchar[i] = new char[8];
divmchar[i][0] = mchar[i * 8 + 0];
divmchar[i][1] = mchar[i * 8 + 1];
divmchar[i][2] = mchar[i * 8 + 2];
divmchar[i][3] = mchar[i * 8 + 3];
divmchar[i][4] = mchar[i * 8 + 4];
divmchar[i][5] = mchar[i * 8 + 5];
divmchar[i][6] = mchar[i * 8 + 6];
divmchar[i][7] = mchar[i * 8 + 7];
}
int[][] divcint = new int[div][];//获得分组加密后的分组密文2进制形式
for (int i = 0; i < div; i++)
{
divcint[i] = new int[64];
divcint[i] = f.des(keys, divmchar[i]);
}
int[] cint = new int[div*64];//合并密文2进制形式
for (int i = 0; i < div; i++)
{
for (int j = 0; j < 64; j++)
{
cint[i * 64 + j] = divcint[i][j];
}
}
//初始序列化
filestream fs = new filestream(target.text, filemode.openorcreate, fileaccess.write);
system.runtime.serialization.formatters.binary.binaryformatter br = new system.runtime.serialization.formatters.binary.binaryformatter();
//将2进制密文转化为char密文
for (int i = 0; i < cchar.length; i++)
{
cchar[i] = (char)(cchar[i] & 0x0000);
}
for (int i = 0; i < cchar.length; i++)
{
if (cint[i * 8 + 0] == 1) cchar[i] = (char)(cchar[i] | 0x80);
if (cint[i * 8 + 1] == 1) cchar[i] = (char)(cchar[i] | 0x40);
if (cint[i * 8 + 2] == 1) cchar[i] = (char)(cchar[i] | 0x20);
if (cint[i * 8 + 3] == 1) cchar[i] = (char)(cchar[i] | 0x10);
if (cint[i * 8 + 4] == 1) cchar[i] = (char)(cchar[i] | 0x8);
if (cint[i * 8 + 5] == 1) cchar[i] = (char)(cchar[i] | 0x4);
if (cint[i * 8 + 6] == 1) cchar[i] = (char)(cchar[i] | 0x2);
if (cint[i * 8 + 7] == 1) cchar[i] = (char)(cchar[i] | 0x1);
}
//显示密文
cbox.text = system.text.encoding.default.getstring(system.text.encoding.default.getbytes(cchar));
//序列化密文到文件
br.serialize(fs, cchar);
fs.close();
}
private void button2_click(object sender, eventargs e)
{
//初始化
int[] cint = new int[64];
char[] kchar = kbox.text.tochararray();
int[] kint = new int[56];
//或得16个密钥
int[][] keys = f.makekey(kchar);
//从文件反序列化或得密文char形式
filestream fs = new filestream(sourse.text, filemode.openorcreate, fileaccess.read);
system.runtime.serialization.formatters.binary.binaryformatter bf = new system.runtime.serialization.formatters.binary.binaryformatter();
char[] cchar = (char[])bf.deserialize(fs);
char[] mchar = new char[cchar.length];
//每8位一组分组密文
int div = cchar.length / 8;
if ((cchar.length % 8) != 0) div += 1;
char[][] divcchar = new char[div][];
for (int i = 0; i < div; i++)
{
divcchar[i] = new char[8];
divcchar[i][0] = cchar[i * 8 + 0];
divcchar[i][1] = cchar[i * 8 + 1];
divcchar[i][2] = cchar[i * 8 + 2];
divcchar[i][3] = cchar[i * 8 + 3];
divcchar[i][4] = cchar[i * 8 + 4];
divcchar[i][5] = cchar[i * 8 + 5];
divcchar[i][6] = cchar[i * 8 + 6];
divcchar[i][7] = cchar[i * 8 + 7];
}
//获得分组后明文2进制形式
int[][] divmint = new int[div][];
for (int i = 0; i < div; i++)
{
divmint[i] = new int[64];
divmint[i] = f.des_1(keys, divcchar[i]);
}
//合并明文2进制形式
int[] mint = new int[div * 64];
for (int i = 0; i < div; i++)
{
for (int j = 0; j < 64; j++)
{
mint[i * 64 + j] = divmint[i][j];
}
}
//由明文2进制形式转化为char形式
for (int i = 0; i < mchar.length; i++)
{
mchar[i] = (char)(mchar[i] & 0x0000);
}
for (int i = 0; i < mchar.length; i++)
{
if (mint[i * 8 + 0] == 1) mchar[i] = (char)(mchar[i] | 0x80);
if (mint[i * 8 + 1] == 1) mchar[i] = (char)(mchar[i] | 0x40);
if (mint[i * 8 + 2] == 1) mchar[i] = (char)(mchar[i] | 0x20);
if (mint[i * 8 + 3] == 1) mchar[i] = (char)(mchar[i] | 0x10);
if (mint[i * 8 + 4] == 1) mchar[i] = (char)(mchar[i] | 0x8);
if (mint[i * 8 + 5] == 1) mchar[i] = (char)(mchar[i] | 0x4);
if (mint[i * 8 + 6] == 1) mchar[i] = (char)(mchar[i] | 0x2);
if (mint[i * 8 + 7] == 1) mchar[i] = (char)(mchar[i] | 0x1);
}
//显示明文
mbox.text = system.text.encoding.default.getstring(system.text.encoding.default.getbytes(mchar));
fs.close();
}
}
}
以下为函数库
#region using directives
using system;
using system.collections.generic;
using system.text;
#endregion
namespace func //函数库
{
public class func
{
//ip置换
public int[] ip(char[] mchar)
{
int[] mint = new int[64];
int[] mreturn = new int[64];
for (int i=0;i<mint.length;i++)
{
mint[i] = 0;
}
//将char明文转化为2进制明文
for (int i =0;i<mchar.length;i++)
{
if ((mchar[i] & 0x80)!=0) mint[i*8+0] =1;
if ((mchar[i] & 0x40)!=0) mint[i*8+1] =1;
if ((mchar[i] & 0x20)!=0) mint[i*8+2] =1;
if ((mchar[i] & 0x10)!=0) mint[i*8+3] =1;
if ((mchar[i] & 0x8)!=0) mint[i*8+4] =1;
if ((mchar[i] & 0x4)!=0) mint[i*8+5] =1;
if ((mchar[i] & 0x2)!=0) mint[i*8+6] =1;
if ((mchar[i] & 0x1)!=0) mint[i*8+7] =1;
}
//置换表
mreturn[0] = mint[57];mreturn[1] = mint[49];mreturn[2] = mint[41];
mreturn[3] = mint[33];mreturn[4] = mint[25];mreturn[5] = mint[17];
mreturn[6] = mint[9]; mreturn[7] = mint[1]; mreturn[8] = mint[59];
mreturn[9] = mint[51];mreturn[10] = mint[43];mreturn[11] = mint[35];
mreturn[12] = mint[27];mreturn[13] = mint[19];mreturn[14] = mint[11];
mreturn[15] = mint[3];mreturn[16] = mint[61];mreturn[17] = mint[53];
mreturn[18] = mint[45];mreturn[19] = mint[37];mreturn[20] = mint[29];
mreturn[21] = mint[21];mreturn[22] = mint[13];mreturn[23] = mint[5];
mreturn[24] = mint[63];mreturn[25] = mint[55];mreturn[26] = mint[47];
mreturn[27] = mint[39];mreturn[28] = mint[31];mreturn[29] = mint[23];
mreturn[30] = mint[15];mreturn[31] = mint[7];mreturn[32] = mint[56];
mreturn[33] = mint[48];mreturn[34] = mint[40];mreturn[35] = mint[32];
mreturn[36] = mint[24];mreturn[37] = mint[16];mreturn[38] = mint[8];
mreturn[39] = mint[0];mreturn[40] = mint[58];mreturn[41] = mint[50];
mreturn[42] = mint[42];mreturn[43] = mint[34];mreturn[44] = mint[26];
mreturn[45] = mint[18];mreturn[46] = mint[10];mreturn[47] = mint[2];
mreturn[48] = mint[60];mreturn[49] = mint[52];mreturn[50] = mint[44];
mreturn[51] = mint[36];mreturn[52] = mint[28];mreturn[53] = mint[20];
mreturn[54] = mint[12];mreturn[55] = mint[4];mreturn[56] = mint[62];
mreturn[57] = mint[54];mreturn[58] = mint[46];mreturn[59] = mint[38];
mreturn[60] = mint[30];mreturn[61] = mint[22];mreturn[62] = mint[14];
mreturn[63] = mint[6];
return mreturn;
}
//ip-1置换
public int[] ip_1(int[] mchar)
{
//置换表
int[] ipreturn = new int[64];
ipreturn[0] = mchar[39];
ipreturn[1] = mchar[7];
ipreturn[2] = mchar[47];
ipreturn[3] = mchar[15];
ipreturn[4] = mchar[55];
ipreturn[5] = mchar[23];
ipreturn[6] = mchar[63];
ipreturn[7] = mchar[31];
ipreturn[8] = mchar[38];
ipreturn[9] = mchar[6];
ipreturn[10] = mchar[46];
ipreturn[11] = mchar[14];
ipreturn[12] = mchar[54];
ipreturn[13] = mchar[22];
ipreturn[14] = mchar[62];
ipreturn[15] = mchar[30];
ipreturn[16] = mchar[37];
ipreturn[17] = mchar[5];
ipreturn[18] = mchar[45];
ipreturn[19] = mchar[13];
ipreturn[20] = mchar[53];
ipreturn[21] = mchar[21];
ipreturn[22] = mchar[61];
ipreturn[23] = mchar[29];
ipreturn[24] = mchar[36];
ipreturn[25] = mchar[4];
ipreturn[26] = mchar[44];
ipreturn[27] = mchar[12];
ipreturn[28] = mchar[52];
ipreturn[29] = mchar[20];
ipreturn[30] = mchar[60];
ipreturn[31] = mchar[28];
ipreturn[32] = mchar[35];
ipreturn[33] = mchar[3];
ipreturn[34] = mchar[43];
ipreturn[35] = mchar[11];
ipreturn[36] = mchar[51];
ipreturn[37] = mchar[19];
ipreturn[38] = mchar[59];
ipreturn[39] = mchar[27];
ipreturn[40] = mchar[34];
ipreturn[41] = mchar[2];
ipreturn[42] = mchar[42];
ipreturn[43] = mchar[10];
ipreturn[44] = mchar[50];
ipreturn[45] = mchar[18];
ipreturn[46] = mchar[58];
ipreturn[47] = mchar[26];
ipreturn[48] = mchar[33];
ipreturn[49] = mchar[1];
ipreturn[50] = mchar[41];
ipreturn[51] = mchar[9];
ipreturn[52] = mchar[49];
ipreturn[53] = mchar[17];
ipreturn[54] = mchar[57];
ipreturn[55] = mchar[25];
ipreturn[56] = mchar[32];
ipreturn[57] = mchar[0];
ipreturn[58] = mchar[40];
ipreturn[59] = mchar[8];
ipreturn[60] = mchar[48];
ipreturn[61] = mchar[16];
ipreturn[62] = mchar[56];
ipreturn[63] = mchar[24];
return ipreturn;
}
//由64位原始密钥计算16个48位密钥
public int[][] makekey(char[] kchar)
{
//16位数组存放16个输出密钥
int[][] keyreturn = new int[16][];
int[] temp = new int[64];
int[] kint = new int[56];
int[] c0 = new int[28]; int[] d0 = new int[28];
int[] c16 = new int[28]; int[] d1 = new int[28];
int[] c1 = new int[28]; int[] d2 = new int[28];
int[] c2 = new int[28]; int[] d3 = new int[28];
int[] c3 = new int[28]; int[] d4 = new int[28];
int[] c4 = new int[28]; int[] d5 = new int[28];
int[] c5 = new int[28]; int[] d6 = new int[28];
int[] c6 = new int[28]; int[] d7 = new int[28];
int[] c7 = new int[28]; int[] d8 = new int[28];
int[] c8 = new int[28]; int[] d9 = new int[28];
int[] c9 = new int[28]; int[] d10 = new int[28];
int[] c10 = new int[28]; int[] d11 = new int[28];
int[] c11 = new int[28]; int[] d12 = new int[28];
int[] c12 = new int[28]; int[] d13 = new int[28];
int[] c13 = new int[28]; int[] d14 = new int[28];
int[] c14 = new int[28]; int[] d15 = new int[28];
int[] c15 = new int[28]; int[] d16 = new int[28];
int[] k1 = new int[48]; int[] k2 = new int[48]; int[] k3 = new int[48];
int[] k4 = new int[48]; int[] k5 = new int[48]; int[] k6 = new int[48];
int[] k7 = new int[48]; int[] k8 = new int[48]; int[] k9 = new int[48];
int[] k10 = new int[48]; int[] k11 = new int[48]; int[] k12 = new int[48];
int[] k13 = new int[48]; int[] k14 = new int[48]; int[] k15 = new int[48];
int[] k16 = new int[48];
//获得原始密钥2进制形式
for (int i=0;i<kchar.length;i++)
{
if ((kchar[i] & 0x80)!=0) temp[i*8 +0] =1;
if ((kchar[i] & 0x40)!=0) temp[i*8 +1] =1;
if ((kchar[i] & 0x20)!=0) temp[i*8 +2] =1;
if ((kchar[i] & 0x10)!=0) temp[i*8 +3] =1;
if ((kchar[i] & 0x8)!=0) temp[i*8 +4] =1;
if ((kchar[i] & 0x4)!=0) temp[i*8 +5] =1;
if ((kchar[i] & 0x2)!=0) temp[i*8 +6] =1;
if ((kchar[i] & 0x1)!=0) temp[i*8 +7] =1;
}
//去掉7。。15。。。。。63的奇校检位并打乱
kint[0] = temp[56];
kint[1] = temp[48];
kint[2] = temp[40];
kint[3] = temp[32];
kint[4] = temp[24];
kint[5] = temp[16];
kint[6] = temp[8];
kint[7] = temp[0];
kint[8] = temp[57];
kint[9] = temp[49];
kint[10] = temp[41];
kint[11] = temp[33];
kint[12] = temp[25];
kint[13] = temp[17];
kint[14] = temp[9];
kint[15] = temp[1];
kint[16] = temp[58];
kint[17] = temp[50];
kint[18] = temp[42];
kint[19] = temp[34];
kint[20] = temp[26];
kint[21] = temp[18];
kint[22] = temp[10];
kint[23] = temp[2];
kint[24] = temp[59];
kint[25] = temp[51];
kint[26] = temp[43];
kint[27] = temp[35];
kint[28] = temp[62];
kint[29] = temp[54];
kint[30] = temp[46];
kint[31] = temp[38];
kint[32] = temp[30];
kint[33] = temp[22];
kint[34] = temp[14];
kint[35] = temp[6];
kint[36] = temp[61];
kint[37] = temp[53];
kint[38] = temp[45];
kint[39] = temp[37];
kint[40] = temp[29];
kint[41] = temp[21];
kint[42] = temp[13];
kint[43] = temp[5];
kint[44] = temp[60];
kint[45] = temp[52];
kint[46] = temp[44];
kint[47] = temp[36];
kint[48] = temp[28];
kint[49] = temp[20];
kint[50] = temp[12];
kint[51] = temp[4];
kint[52] = temp[27];
kint[53] = temp[19];
kint[54] = temp[11];
kint[55] = temp[3];
//分组56位密钥为2部分
for (int i=0;i<28;i++)
{
c0[i] = kint[i];
d0[i] = kint[i+28];
}
//循环左移1位
for (int i = 1; i < 28; i++)
{
c1[i - 1] = c0[i];
d1[i - 1] = d0[i];
}
c1[27] = c0[0];
d1[27] = d0[0];
//循环左移1位
for (int i = 1; i < 28; i++)
{
c2[i - 1] = c1[i];
d2[i - 1] = d1[i];
}
c2[27] = c1[0];
d2[27] = d1[0];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c3[i - 2] = c2[i];
d3[i - 2] = d2[i];
}
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[i];
}
c4[26] = c3[0];
c4[27] = c3[1];
for (int i = 2; i < 28; i++)
{
d4[i - 2] = d3[i];
}
d4[26] = d3[0];
d4[27] = d3[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c5[i - 2] = c4[i];
}
c5[26] = c4[0];
c5[27] = c4[1];
for (int i = 2; i < 28; i++)
{
d5[i - 2] = d4[i];
}
d5[26] = d4[0];
d5[27] = d4[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c6[i - 2] = c5[i];
}
c6[26] = c5[0];
c6[27] = c5[1];
for (int i = 2; i < 28; i++)
{
d6[i - 2] = d5[i];
}
d6[26] = d5[0];
d6[27] = d5[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c7[i - 2] = c6[i];
}
c7[26] = c6[0];
c7[27] = c6[1];
for (int i = 2; i < 28; i++)
{
d7[i - 2] = d6[i];
}
d7[26] = d6[0];
d7[27] = d6[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c8[i - 2] = c7[i];
}
c8[26] = c7[0];
c8[27] = c7[1];
for (int i = 2; i < 28; i++)
{
d8[i - 2] = d7[i];
}
d8[26] = d7[0];
d8[27] = d7[1];
//循环左移1位
for (int i = 1; i < 28; i++)
{
c9[i - 1] = c8[i];
}
c9[27] = c8[0];
for (int i = 1; i < 28; i++)
{
d9[i - 1] = d8[i];
}
d9[27] = d8[0];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c10[i - 2] = c9[i];
}
c10[26] = c9[0];
c10[27] = c9[1];
for (int i = 2; i < 28; i++)
{
d10[i - 2] = d9[i];
}
d10[26] = d9[0];
d10[27] = d9[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c11[i - 2] = c10[i];
}
c11[26] = c10[0];
c11[27] = c10[1];
for (int i = 2; i < 28; i++)
{
d11[i - 2] = d10[i];
}
d11[26] = d10[0];
d11[27] = d10[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c12[i - 2] = c11[i];
}
c12[26] = c11[0];
c12[27] = c11[1];
for (int i = 2; i < 28; i++)
{
d12[i - 2] = d11[i];
}
d12[26] = d11[0];
d12[27] = d11[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c13[i - 2] = c12[i];
}
c13[26] = c12[0];
c13[27] = c12[1];
for (int i = 2; i < 28; i++)
{
d13[i - 2] = d12[i];
}
d13[26] = d12[0];
d13[27] = d12[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c14[i - 2] = c13[i];
}
c14[26] = c13[0];
c14[27] = c13[1];
for (int i = 2; i < 28; i++)
{
d14[i - 2] = d13[i];
}
d14[26] = d13[0];
d14[27] = d13[1];
//循环左移2位
for (int i = 2; i < 28; i++)
{
c15[i - 2] = c14[i];
}
c15[26] = c14[0];
c15[27] = c14[1];
for (int i = 2; i < 28; i++)
{
d15[i - 2] = d14[i];
}
d15[26] = d14[0];
d15[27] = d14[1];
//循环左移1位
for (int i = 1; i < 28; i++)
{
c16[i - 1] = c15[i];
}
c16[27] = c15[0];
for (int i = 1; i < 28; i++)
{
d16[i - 1] = d15[i];
}
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[i] = c[i];
temp[i + 28] = d[i];
}
//置换表
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[i] = r[i - 1];
}
for (i = 6; i <= 11; i++)
{
aftere[i] = r[i - 3];
}
for (i = 12; i <= 17; i++)
{
aftere[i] = r[i - 5];
}
for (i = 18; i <= 23; i++)
{
aftere[i] = r[i - 7];
}
for (i = 24; i <= 29; i++)
{
aftere[i] = r[i - 9];
}
for (i = 30; i <= 35; i++)
{
aftere[i] = r[i - 11];
}
for (i = 36; i <= 41; i++)
{
aftere[i] = r[i - 13];
}
for (i = 42; i <= 46; i++)
{
aftere[i] = 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[i] != k[i]) aftere[i] = 1; else aftere[i] = 0;
}
//将明文分组
for (int i = 0; i < 6; i++)
{
m1[i] = aftere[i];
m2[i] = aftere[i + 6];
m3[i] = aftere[i + 12];
m4[i] = aftere[i + 18];
m5[i] = aftere[i + 24];
m6[i] = aftere[i + 30];
m7[i] = aftere[i + 36];
m8[i] = 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[i] = afters1[i];
afters[i + 4] = afters2[i];
afters[i + 8] = afters3[i];
afters[i + 12] = afters4[i];
afters[i + 16] = afters5[i];
afters[i + 20] = afters6[i];
afters[i + 24] = afters7[i];
afters[i + 28] = afters8[i];
}
//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];
greturn[19] = afters[13];
greturn[20] = afters[31];
greturn[21] = afters[26];
greturn[22] = afters[2];
greturn[23] = afters[8];
greturn[24] = afters[18];
greturn[25] = afters[12];
greturn[26] = afters[29];
greturn[27] = afters[5];
greturn[28] = afters[21];
greturn[29] = afters[10];
greturn[30] = afters[3];
greturn[31] = afters[24];
return greturn;
}
//获得s盒的值
public int gets(int[] mi, int i)
{
//初始化8个s盒
int[,] s1 = new int[4, 16]; int[,] s2 = new int[4, 16];
int[,] s3 = new int[4, 16]; int[,] s4 = new int[4, 16];
int[,] s5 = new int[4, 16]; int[,] s6 = new int[4, 16];
int[,] s7 = new int[4, 16]; int[,] s8 = new int[4, 16];
int[] output = new int[8];
int j, k;
//8个s盒
s1[0, 0] = 14;
s1[0, 1] = 4;
s1[0, 2] = 13;
s1[0, 3] = 1;
s1[0, 4] = 2;
s1[0, 5] = 15;
s1[0, 6] = 11;
s1[0, 7] = 8;
s1[0, 8] = 3;
s1[0, 9] = 10;
s1[0, 10] = 6;
s1[0, 11] = 12;
s1[0, 12] = 5;
s1[0, 13] = 9;
s1[0, 14] = 0;
s1[0, 15] = 7;
s1[1, 0] = 0;
s1[1, 1] = 15;
s1[1, 2] = 7;
s1[1, 3] = 4;
s1[1, 4] = 14;
s1[1, 5] = 2;
s1[1, 6] = 13;
s1[1, 7] = 1;
s1[1, 8] = 10;
s1[1, 9] = 6;
s1[1, 10] = 12;
s1[1, 11] = 11;
s1[1, 12] = 9;
s1[1, 13] = 5;
s1[1, 14] = 3;
s1[1, 15] = 8;
s1[2, 0] = 4;
s1[2, 1] = 1;
s1[2, 2] = 14;
s1[2, 3] = 8;
s1[2, 4] = 13;
s1[2, 5] = 6;
s1[2, 6] = 2;
s1[2, 7] = 11;
s1[2, 8] = 15;
s1[2, 9] = 12;
s1[2, 10] = 9;
s1[2, 11] = 7;
s1[2, 12] = 3;
s1[2, 13] = 10;
s1[2, 14] = 5;
s1[2, 15] = 0;
s1[3, 0] = 15;
s1[3, 1] = 12;
s1[3, 2] = 8;
s1[3, 3] = 2;
s1[3, 4] = 4;
s1[3, 5] = 9;
s1[3, 6] = 1;
s1[3, 7] = 7;
s1[3, 8] = 5;
s1[3, 9] = 11;
s1[3, 10] = 3;
s1[3, 11] = 14;
s1[3, 12] = 10;
s1[3, 13] = 0;
s1[3, 14] = 6;
s1[3, 15] = 13;
s2[0, 0] = 15;
s2[0, 1] = 1;
s2[0, 2] = 8;
s2[0, 3] = 14;
s2[0, 4] = 6;
s2[0, 5] = 11;
s2[0, 6] = 3;
s2[0, 7] = 4;
s2[0, 8] = 9;
s2[0, 9] = 7;
s2[0, 10] = 2;
s2[0, 11] = 13;
s2[0, 12] = 12;
s2[0, 13] = 0;
s2[0, 14] = 5;
s2[0, 15] = 10;
s2[1, 0] = 3;
s2[1, 1] = 13;
s2[1, 2] = 4;
s2[1, 3] = 7;
s2[1, 4] = 15;
s2[1, 5] = 2;
s2[1, 6] = 8;
s2[1, 7] = 14;
s2[1, 8] = 12;
s2[1, 9] = 0;
s2[1, 10] = 1;
s2[1, 11] = 10;
s2[1, 12] = 6;
s2[1, 13] = 9;
s2[1, 14] = 11;
s2[1, 15] = 5;
s2[2, 0] = 0;
s2[2, 1] = 14;
s2[2, 2] = 7;
s2[2, 3] = 11;
s2[2, 4] = 10;
s2[2, 5] = 4;
s2[2, 6] = 13;
s2[2, 7] = 1;
s2[2, 8] = 5;
s2[2, 9] = 8;
s2[2, 10] = 12;
s2[2, 11] = 6;
s2[2, 12] = 9;
s2[2, 13] = 3;
s2[2, 14] = 2;
s2[2, 15] = 15;
s2[3, 0] = 13;
s2[3, 1] = 8;
s2[3, 2] = 10;
s2[3, 3] = 1;
s2[3, 4] = 3;
s2[3, 5] = 15;
s2[3, 6] = 4;
s2[3, 7] = 2;
s2[3, 8] = 11;
s2[3, 9] = 6;
s2[3, 10] = 7;
s2[3, 11] = 12;
s2[3, 12] = 0;
s2[3, 13] = 5;
s2[3, 14] = 14;
s2[3, 15] = 9;
s3[0, 0] = 10;
s3[0, 1] = 0;
s3[0, 2] = 9;
s3[0, 3] = 14;
s3[0, 4] = 6;
s3[0, 5] = 3;
s3[0, 6] = 15;
s3[0, 7] = 5;
s3[0, 8] = 1;
s3[0, 9] = 13;
s3[0, 10] = 12;
s3[0, 11] = 7;
s3[0, 12] = 11;
s3[0, 13] = 4;
s3[0, 14] = 2;
s3[0, 15] = 8;
s3[1, 0] = 13;
s3[1, 1] = 7;
s3[1, 2] = 0;
s3[1, 3] = 9;
s3[1, 4] = 3;
s3[1, 5] = 4;
s3[1, 6] = 6;
s3[1, 7] = 10;
s3[1, 8] = 2;
s3[1, 9] = 8;
s3[1, 10] = 5;
s3[1, 11] = 14;
s3[1, 12] = 12;
s3[1, 13] = 11;
s3[1, 14] = 15;
s3[1, 15] = 1;
s3[2, 0] = 13;
s3[2, 1] = 6;
s3[2, 2] = 4;
s3[2, 3] = 9;
s3[2, 4] = 8;
s3[2, 5] = 15;
s3[2, 6] = 3;
s3[2, 7] = 0;
s3[2, 8] = 11;
s3[2, 9] = 1;
s3[2, 10] = 2;
s3[2, 11] = 12;
s3[2, 12] = 5;
s3[2, 13] = 10;
s3[2, 14] = 14;
s3[2, 15] = 7;
s3[3, 0] = 1;
s3[3, 1] = 10;
s3[3, 2] = 13;
s3[3, 3] = 0;
s3[3, 4] = 6;
s3[3, 5] = 9;
s3[3, 6] = 8;
s3[3, 7] = 7;
s3[3, 8] = 4;
s3[3, 9] = 15;
s3[3, 10] = 14;
s3[3, 11] = 3;
s3[3, 12] = 11;
s3[3, 13] = 5;
s3[3, 14] = 2;
s3[3, 15] = 12;
s4[0, 0] = 7;
s4[0, 1] = 13;
s4[0, 2] = 14;
s4[0, 3] = 3;
s4[0, 4] = 0;
s4[0, 5] = 6;
s4[0, 6] = 9;
s4[0, 7] = 10;
s4[0, 8] = 1;
s4[0, 9] = 2;
s4[0, 10] = 8;
s4[0, 11] = 5;
s4[0, 12] = 11;
s4[0, 13] = 12;
s4[0, 14] = 4;
s4[0, 15] = 15;
s4[1, 0] = 13;
s4[1, 1] = 8;
s4[1, 2] = 11;
s4[1, 3] = 5;
s4[1, 4] = 6;
s4[1, 5] = 15;
s4[1, 6] = 0;
s4[1, 7] = 3;
s4[1, 8] = 4;
s4[1, 9] = 7;
s4[1, 10] = 2;
s4[1, 11] = 12;
s4[1, 12] = 1;
s4[1, 13] = 10;
s4[1, 14] = 14;
s4[1, 15] = 9;
s4[2, 0] = 10;
s4[2, 1] = 6;
s4[2, 2] = 9;
s4[2, 3] = 0;
s4[2, 4] = 12;
s4[2, 5] = 11;
s4[2, 6] = 7;
s4[2, 7] = 13;
s4[2, 8] = 15;
s4[2, 9] = 1;
s4[2, 10] = 3;
s4[2, 11] = 14;
s4[2, 12] = 5;
s4[2, 13] = 2;
s4[2, 14] = 8;
s4[2, 15] = 4;
s4[3, 0] = 3;
s4[3, 1] = 15;
s4[3, 2] = 0;
s4[3, 3] = 6;
s4[3, 4] = 10;
s4[3, 5] = 1;
s4[3, 6] = 13;
s4[3, 7] = 8;
s4[3, 8] = 9;
s4[3, 9] = 4;
s4[3, 10] = 5;
s4[3, 11] = 11;
s4[3, 12] = 12;
s4[3, 13] = 7;
s4[3, 14] = 2;
s4[3, 15] = 14;
s5[0, 0] = 2;
s5[0, 1] = 12;
s5[0, 2] = 4;
s5[0, 3] = 1;
s5[0, 4] = 7;
s5[0, 5] = 10;
s5[0, 6] = 11;
s5[0, 7] = 6;
s5[0, 8] = 8;
s5[0, 9] = 5;
s5[0, 10] = 3;
s5[0, 11] = 15;
s5[0, 12] = 13;
s5[0, 13] = 0;
s5[0, 14] = 14;
s5[0, 15] = 9;
s5[1, 0] = 14;
s5[1, 1] = 11;
s5[1, 2] = 2;
s5[1, 3] = 12;
s5[1, 4] = 4;
s5[1, 5] = 7;
s5[1, 6] = 13;
s5[1, 7] = 1;
s5[1, 8] = 5;
s5[1, 9] = 0;
s5[1, 10] = 15;
s5[1, 11] = 10;
s5[1, 12] = 3;
s5[1, 13] = 9;
s5[1, 14] = 8;
s5[1, 15] = 6;
s5[2, 0] = 4;
s5[2, 1] = 2;
s5[2, 2] = 1;
s5[2, 3] = 11;
s5[2, 4] = 10;
s5[2, 5] = 13;
s5[2, 6] = 7;
s5[2, 7] = 8;
s5[2, 8] = 15;
s5[2, 9] = 9;
s5[2, 10] = 12;
s5[2, 11] = 5;
s5[2, 12] = 6;
s5[2, 13] = 3;
s5[2, 14] = 0;
s5[2, 15] = 14;
s5[3, 0] = 11;
s5[3, 1] = 8;
s5[3, 2] = 12;
s5[3, 3] = 7;
s5[3, 4] = 1;
s5[3, 5] = 14;
s5[3, 6] = 2;
s5[3, 7] = 13;
s5[3, 8] = 6;
s5[3, 9] = 15;
s5[3, 10] = 0;
s5[3, 11] = 9;
s5[3, 12] = 10;
s5[3, 13] = 4;
s5[3, 14] = 5;
s5[3, 15] = 3;
s6[0, 0] = 12;
s6[0, 1] = 1;
s6[0, 2] = 10;
s6[0, 3] = 15;
s6[0, 4] = 9;
s6[0, 5] = 2;
s6[0, 6] = 6;
s6[0, 7] = 8;
s6[0, 8] = 0;
s6[0, 9] = 13;
s6[0, 10] = 3;
s6[0, 11] = 4;
s6[0, 12] = 14;
s6[0, 13] = 7;
s6[0, 14] = 5;
s6[0, 15] = 11;
s6[1, 0] = 10;
s6[1, 1] = 15;
s6[1, 2] = 4;
s6[1, 3] = 2;
s6[1, 4] = 7;
s6[1, 5] = 12;
s6[1, 6] = 9;
s6[1, 7] = 5;
s6[1, 8] = 6;
s6[1, 9] = 1;
s6[1, 10] = 13;
s6[1, 11] = 14;
s6[1, 12] = 0;
s6[1, 13] = 11;
s6[1, 14] = 3;
s6[1, 15] = 8;
s6[2, 0] = 9;
s6[2, 1] = 14;
s6[2, 2] = 15;
s6[2, 3] = 5;
s6[2, 4] = 2;
s6[2, 5] = 8;
s6[2, 6] = 12;
s6[2, 7] = 3;
s6[2, 8] = 7;
s6[2, 9] = 0;
s6[2, 10] = 4;
s6[2, 11] = 10;
s6[2, 12] = 1;
s6[2, 13] = 13;
s6[2, 14] = 11;
s6[2, 15] = 6;
s6[3, 0] = 4;
s6[3, 1] = 3;
s6[3, 2] = 2;
s6[3, 3] = 12;
s6[3, 4] = 9;
s6[3, 5] = 5;
s6[3, 6] = 15;
s6[3, 7] = 10;
s6[3, 8] = 11;
s6[3, 9] = 14;
s6[3, 10] = 1;
s6[3, 11] = 7;
s6[3, 12] = 6;
s6[3, 13] = 0;
s6[3, 14] = 8;
s6[3, 15] = 13;
s7[0, 0] = 4;
s7[0, 1] = 11;
s7[0, 2] = 2;
s7[0, 3] = 14;
s7[0, 4] = 15;
s7[0, 5] = 0;
s7[0, 6] = 8;
s7[0, 7] = 13;
s7[0, 8] = 3;
s7[0, 9] = 12;
s7[0, 10] = 9;
s7[0, 11] = 7;
s7[0, 12] = 5;
s7[0, 13] = 10;
s7[0, 14] = 6;
s7[0, 15] = 1;
s7[1, 0] = 13;
s7[1, 1] = 0;
s7[1, 2] = 11;
s7[1, 3] = 7;
s7[1, 4] = 4;
s7[1, 5] = 9;
s7[1, 6] = 1;
s7[1, 7] = 10;
s7[1, 8] = 14;
s7[1, 9] = 3;
s7[1, 10] = 5;
s7[1, 11] = 12;
s7[1, 12] = 2;
s7[1, 13] = 15;
s7[1, 14] = 8;
s7[1, 15] = 6;
s7[2, 0] = 1;
s7[2, 1] = 4;
s7[2, 2] = 11;
s7[2, 3] = 13;
s7[2, 4] = 12;
s7[2, 5] = 3;
s7[2, 6] = 7;
s7[2, 7] = 14;
s7[2, 8] = 10;
s7[2, 9] = 15;
s7[2, 10] = 6;
s7[2, 11] = 8;
s7[2, 12] = 0;
s7[2, 13] = 5;
s7[2, 14] = 9;
s7[2, 15] = 2;
s7[3, 0] = 6;
s7[3, 1] = 11;
s7[3, 2] = 13;
s7[3, 3] = 8;
s7[3, 4] = 1;
s7[3, 5] = 4;
s7[3, 6] = 10;
s7[3, 7] = 7;
s7[3, 8] = 9;
s7[3, 9] = 5;
s7[3, 10] = 0;
s7[3, 11] = 15;
s7[3, 12] = 14;
s7[3, 13] = 2;
s7[3, 14] = 3;
s7[3, 15] = 12;
s8[0, 0] = 13;
s8[0, 1] = 2;
s8[0, 2] = 8;
s8[0, 3] = 4;
s8[0, 4] = 6;
s8[0, 5] = 15;
s8[0, 6] = 11;
s8[0, 7] = 1;
s8[0, 8] = 10;
s8[0, 9] = 9;
s8[0, 10] = 3;
s8[0, 11] = 14;
s8[0, 12] = 5;
s8[0, 13] = 0;
s8[0, 14] = 12;
s8[0, 15] = 7;
s8[1, 0] = 1;
s8[1, 1] = 15;
s8[1, 2] = 13;
s8[1, 3] = 8;
s8[1, 4] = 10;
s8[1, 5] = 3;
s8[1, 6] = 7;
s8[1, 7] = 4;
s8[1, 8] = 12;
s8[1, 9] = 5;
s8[1, 10] = 6;
s8[1, 11] = 11;
s8[1, 12] = 0;
s8[1, 13] = 14;
s8[1, 14] = 9;
s8[1, 15] = 2;
s8[2, 0] = 7;
s8[2, 1] = 11;
s8[2, 2] = 4;
s8[2, 3] = 1;
s8[2, 4] = 9;
s8[2, 5] = 12;
s8[2, 6] = 14;
s8[2, 7] = 2;
s8[2, 8] = 0;
s8[2, 9] = 6;
s8[2, 10] = 10;
s8[2, 11] = 13;
s8[2, 12] = 15;
s8[2, 13] = 3;
s8[2, 14] = 5;
s8[2, 15] = 8;
s8[3, 0] = 2;
s8[3, 1] = 1;
s8[3, 2] = 14;
s8[3, 3] = 7;
s8[3, 4] = 4;
s8[3, 5] = 10;
s8[3, 6] = 8;
s8[3, 7] = 13;
s8[3, 8] = 15;
s8[3, 9] = 12;
s8[3, 10] = 9;
s8[3, 11] = 0;
s8[3, 12] = 3;
s8[3, 13] = 5;
s8[3, 14] = 6;
s8[3, 15] = 11;
//从明文的输入获得经过s盒的值
j = mi[0] * 2 + mi[5];
k = mi[1] * 8 + mi[2] * 4 + mi[3] * 2 + mi[4];
output[0] = s1[j, k];
output[1] = s2[j, k];
output[2] = s3[j, k];
output[3] = s4[j, k];
output[4] = s5[j, k];
output[5] = s6[j, k];
output[6] = s7[j, k];
output[7] = s8[j, k];
return output[i - 1];
}
//输入16个密钥和明文计算des
public int[] des(int[][] keys, char[] r)
{
//2进制明文分组li ri
int[] l0 = new int[32]; int[] r0 = new int[32];
int[] l1 = new int[32]; int[] r1 = new int[32];
int[] l2 = new int[32]; int[] r2 = new int[32];
int[] l3 = new int[32]; int[] r3 = new int[32];
int[] l4 = new int[32]; int[] r4 = new int[32];
int[] l5 = new int[32]; int[] r5 = new int[32];
int[] l6 = new int[32]; int[] r6 = new int[32];
int[] l7 = new int[32]; int[] r7 = new int[32];
int[] l8 = new int[32]; int[] r8 = new int[32];
int[] l9 = new int[32]; int[] r9 = new int[32];
int[] l10 = new int[32]; int[] r10 = new int[32];
int[] l11 = new int[32]; int[] r11 = new int[32];
int[] l12 = new int[32]; int[] r12 = new int[32];
int[] l13 = new int[32]; int[] r13 = new int[32];
int[] l14 = new int[32]; int[] r14 = new int[32];
int[] l15 = new int[32]; int[] r15 = new int[32];
int[] l16 = new int[32]; int[] r16 = new int[32];
int[] afterg = new int[32];
//获得2进制明文经过ip置换后结果
int[] afterip = ip(r);
int[] final = new int[64];
int[] temp = new int[64];
//获得l0 r0
for (int i = 0; i < 32; i++)
{
l0[i] = afterip[i];
r0[i] = afterip[i + 32];
}
//用r0和第一个密钥进行g函数
afterg = g(r0, keys[0]);
for (int i=0;i<32;i++)
{
if (l0[i]!=afterg[i]) r1[i]=1;//l0与r1异或
}
l1 = r0;
//第一重
afterg = g(r1, keys[1]);
for (int i = 0; i < 32; i++)
{
if (l1[i] != afterg[i]) r2[i] = 1;
}
l2 = r1;
//第二重
afterg = g(r2, keys[2]);
for (int i = 0; i < 32; i++)
{
if (l2[i] != afterg[i]) r3[i] = 1;
}
l3 = r2;
//第3重
afterg = g(r3, keys[3]);
for (int i = 0; i < 32; i++)
{
if (l3[i] != afterg[i]) r4[i] = 1;
}
l4 = r3;
//第4重
afterg = g(r4, keys[4]);
for (int i = 0; i < 32; i++)
{
if (l4[i] != afterg[i]) r5[i] = 1;
}
l5 = r4;
//第5重
afterg = g(r5, keys[5]);
for (int i = 0; i < 32; i++)
{
if (l5[i] != afterg[i]) r6[i] = 1;
}
l6 = r5;
//第6重
afterg = g(r6, keys[6]);
for (int i = 0; i < 32; i++)
{
if (l6[i] != afterg[i]) r7[i] = 1;
}
l7 = r6;
//第7重
afterg = g(r7, keys[7]);
for (int i = 0; i < 32; i++)
{
if (l7[i] != afterg[i]) r8[i] = 1;
}
l8 = r7;
//第8重
afterg = g(r8, keys[8]);
for (int i = 0; i < 32; i++)
{
if (l8[i] != afterg[i]) r9[i] = 1;
}
l9 = r8;
//第9重
afterg = g(r9, keys[9]);
for (int i = 0; i < 32; i++)
{
if (l9[i] != afterg[i]) r10[i] = 1;
}
l10 = r9;
//第10重
afterg = g(r10, keys[10]);
for (int i = 0; i < 32; i++)
{
if (l10[i] != afterg[i]) r11[i] = 1;
}
l11 = r10;
//第11重
afterg = g(r11, keys[11]);
for (int i = 0; i < 32; i++)
{
if (l11[i] != afterg[i]) r12[i] = 1;
}
l12 = r11;
//第12重
afterg = g(r12, keys[12]);
for (int i = 0; i < 32; i++)
{
if (l12[i] != afterg[i]) r13[i] = 1;
}
l13 = r12;
//第13重
afterg = g(r13, keys[13]);
for (int i = 0; i < 32; i++)
{
if (l13[i] != afterg[i]) r14[i] = 1;
}
l14 = r13;
//第14重
afterg = g(r14, keys[14]);
for (int i = 0; i < 32; i++)
{
if (l14[i] != afterg[i]) r15[i] = 1;
}
l15 = r14;
//第15重
afterg = g(r15, keys[15]);
for (int i = 0; i < 32; i++)
{
if (l15[i] != afterg[i]) r16[i] = 1;
}
l16 = r15;
//第16重
//合并l16和r16
for (int i = 0; i < 32; i++)
{
temp[i] = r16[i];
temp[i + 32] = l16[i];
}
//明文经过ip-1置换输出
final = ip_1(temp);
return final;
}
//16个密钥倒向输入计算des反函数,原理大致与des相同
public int[] des_1(int[][] keys, char[] r)
{
int[] l0 = new int[32]; int[] r0 = new int[32];
int[] l1 = new int[32]; int[] r1 = new int[32];
int[] l2 = new int[32]; int[] r2 = new int[32];
int[] l3 = new int[32]; int[] r3 = new int[32];
int[] l4 = new int[32]; int[] r4 = new int[32];
int[] l5 = new int[32]; int[] r5 = new int[32];
int[] l6 = new int[32]; int[] r6 = new int[32];
int[] l7 = new int[32]; int[] r7 = new int[32];
int[] l8 = new int[32]; int[] r8 = new int[32];
int[] l9 = new int[32]; int[] r9 = new int[32];
int[] l10 = new int[32]; int[] r10 = new int[32];
int[] l11 = new int[32]; int[] r11 = new int[32];
int[] l12 = new int[32]; int[] r12 = new int[32];
int[] l13 = new int[32]; int[] r13 = new int[32];
int[] l14 = new int[32]; int[] r14 = new int[32];
int[] l15 = new int[32]; int[] r15 = new int[32];
int[] l16 = new int[32]; int[] r16 = new int[32];
int[] afterg = new int[32];
int[] afterip = ip(r);
int[] final = new int[64];
int[] temp = new int[64];
for (int i = 0; i < 32; i++)
{
l0[i] = afterip[i];
r0[i] = afterip[i + 32];
}
afterg = g(r0, keys[15]);
for (int i=0;i<32;i++)
{
if (l0[i]!=afterg[i]) r1[i]=1;
}
l1 = r0;
//第一重
afterg = g(r1, keys[14]);
for (int i = 0; i < 32; i++)
{
if (l1[i] != afterg[i]) r2[i] = 1;
}
l2 = r1;
//第二重
afterg = g(r2, keys[13]);
for (int i = 0; i < 32; i++)
{
if (l2[i] != afterg[i]) r3[i] = 1;
}
l3 = r2;
//第3重
afterg = g(r3, keys[12]);
for (int i = 0; i < 32; i++)
{
if (l3[i] != afterg[i]) r4[i] = 1;
}
l4 = r3;
//第4重
afterg = g(r4, keys[11]);
for (int i = 0; i < 32; i++)
{
if (l4[i] != afterg[i]) r5[i] = 1;
}
l5 = r4;
//第5重
afterg = g(r5, keys[10]);
for (int i = 0; i < 32; i++)
{
if (l5[i] != afterg[i]) r6[i] = 1;
}
l6 = r5;
//第6重
afterg = g(r6, keys[9]);
for (int i = 0; i < 32; i++)
{
if (l6[i] != afterg[i]) r7[i] = 1;
}
l7 = r6;
//第7重
afterg = g(r7, keys[8]);
for (int i = 0; i < 32; i++)
{
if (l7[i] != afterg[i]) r8[i] = 1;
}
l8 = r7;
//第8重
afterg = g(r8, keys[7]);
for (int i = 0; i < 32; i++)
{
if (l8[i] != afterg[i]) r9[i] = 1;
}
l9 = r8;
//第9重
afterg = g(r9, keys[6]);
for (int i = 0; i < 32; i++)
{
if (l9[i] != afterg[i]) r10[i] = 1;
}
l10 = r9;
//第10重
afterg = g(r10, keys[5]);
for (int i = 0; i < 32; i++)
{
if (l10[i] != afterg[i]) r11[i] = 1;
}
l11 = r10;
//第11重
afterg = g(r11, keys[4]);
for (int i = 0; i < 32; i++)
{
if (l11[i] != afterg[i]) r12[i] = 1;
}
l12 = r11;
//第12重
afterg = g(r12, keys[3]);
for (int i = 0; i < 32; i++)
{
if (l12[i] != afterg[i]) r13[i] = 1;
}
l13 = r12;
//第13重
afterg = g(r13, keys[2]);
for (int i = 0; i < 32; i++)
{
if (l13[i] != afterg[i]) r14[i] = 1;
}
l14 = r13;
//第14重
afterg = g(r14, keys[1]);
for (int i = 0; i < 32; i++)
{
if (l14[i] != afterg[i]) r15[i] = 1;
}
l15 = r14;
//第15重
afterg = g(r15, keys[0]);
for (int i = 0; i < 32; i++)
{
if (l15[i] != afterg[i]) r16[i] = 1;
}
l16 = r15;
//第16重
for (int i = 0; i < 32; i++)
{
temp[i] = r16[i];
temp[i + 32] = l16[i];
}
final = ip_1(temp);
return final;
}
}
}