AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

DES算法详述

51自学网 2015-08-30 http://www.wanshiok.com

 

/* 16 Iterations */
for (iter = 1; iter < 17; iter++) {
for (i = 0; i < 32; i++)
buffer[i] = bufout[32+i];

/* Calculation of F(R, K) */
/* Permute - E */
worka[ 0] = buffer[31];
worka[ 1] = buffer[ 0];
worka[ 2] = buffer[ 1];
worka[ 3] = buffer[ 2];
worka[ 4] = buffer[ 3];
worka[ 5] = buffer[ 4];
worka[ 6] = buffer[ 3];
worka[ 7] = buffer[ 4];
worka[ 8] = buffer[ 5];
worka[ 9] = buffer[ 6];
worka[10] = buffer[ 7];
worka[11] = buffer[ 8];
worka[12] = buffer[ 7];
worka[13] = buffer[ 8];
worka[14] = buffer[ 9];
worka[15] = buffer[10];
worka[16] = buffer[11];
worka[17] = buffer[12];
worka[18] = buffer[11];
worka[19] = buffer[12];
worka[20] = buffer[13];
worka[21] = buffer[14];
worka[22] = buffer[15];
worka[23] = buffer[16];
worka[24] = buffer[15];
worka[25] = buffer[16];
worka[26] = buffer[17];
worka[27] = buffer[18];
worka[28] = buffer[19];
worka[29] = buffer[20];
worka[30] = buffer[19];
worka[31] = buffer[20];
worka[32] = buffer[21];
worka[33] = buffer[22];
worka[34] = buffer[23];
worka[35] = buffer[24];
worka[36] = buffer[23];
worka[37] = buffer[24];
worka[38] = buffer[25];
worka[39] = buffer[26];
worka[40] = buffer[27];
worka[41] = buffer[28];
worka[42] = buffer[27];
worka[43] = buffer[28];
worka[44] = buffer[29];
worka[45] = buffer[30];
worka[46] = buffer[31];
worka[47] = buffer[ 0];

/* KS Function Begin */
if (flg) {
nbrofshift = shift[iter-1];
for (i = 0; i < (int) nbrofshift; i++) {
temp1 = kwork[0];
temp2 = kwork[28];
for (j = 0; j < 27; j++) {
kwork[j] = kwork[j+1];
kwork[j+28] = kwork[j+29];
}
kwork[27] = temp1;
kwork[55] = temp2;
}
} else if (iter > 1) {
nbrofshift = shift[17-iter];
for (i = 0; i < (int) nbrofshift; i++) {
temp1 = kwork[27];
temp2 = kwork[55];
for (j = 27; j > 0; j--) {
kwork[j] = kwork[j-1];
kwork[j+28] = kwork[j+27];
}
kwork[0] = temp1;
kwork[28] = temp2;
}
}

/* Permute kwork - PC2 */
kn[ 0] = kwork[13];
kn[ 1] = kwork[16];
kn[ 2] = kwork[10];
kn[ 3] = kwork[23];
kn[ 4] = kwork[ 0];
kn[ 5] = kwork[ 4];
kn[ 6] = kwork[ 2];
kn[ 7] = kwork[27];
kn[ 8] = kwork[14];
kn[ 9] = kwork[ 5];
kn[10] = kwork[20];
kn[11] = kwork[ 9];
kn[12] = kwork[22];
kn[13] = kwork[18];
kn[14] = kwork[11];
kn[15] = kwork[ 3];
kn[16] = kwork[25];
kn[17] = kwork[ 7];
kn[18] = kwork[15];
kn[19] = kwork[ 6];
kn[20] = kwork[26];
kn[21] = kwork[19];
kn[22] = kwork[12];
kn[23] = kwork[ 1];
kn[24] = kwork[40];
kn[25] = kwork[51];
kn[26] = kwork[30];
kn[27] = kwork[36];
kn[28] = kwork[46];
kn[29] = kwork[54];
kn[30] = kwork[29];
kn[31] = kwork[39];
kn[32] = kwork[50];
kn[33] = kwork[44];
kn[34] = kwork[32];
kn[35] = kwork[47];
kn[36] = kwork[43];
kn[37] = kwork[48];
kn[38] = kwork[38];
kn[39] = kwork[55];
kn[40] = kwork[33];
kn[41] = kwork[52];
kn[42] = kwork[45];
kn[43] = kwork[41];
kn[44] = kwork[49];
kn[45] = kwork[35];
kn[46] = kwork[28];
kn[47] = kwork[31];
/* KS Function End */

/* worka XOR kn */
for (i = 0; i < 48; i++)
worka[i] = worka[i] ^ kn[i];

/* 8 s-functions */
valindex = s1[2*worka[ 0]+worka[ 5]]
[2*(2*(2*worka[ 1]+worka[ 2])+
worka[ 3])+worka[ 4]];
valindex = valindex * 4;
kn[ 0] = binary[0+valindex];
kn[ 1] = binary[1+valindex];
kn[ 2] = binary[2+valindex];
kn[ 3] = binary[3+valindex];
valindex = s2[2*worka[ 6]+worka[11]]
[2*(2*(2*worka[ 7]+worka[ 8])+
worka[ 9])+worka[10]];
valindex = valindex * 4;
kn[ 4] = binary[0+valindex];
kn[ 5] = binary[1+valindex];
kn[ 6] = binary[2+valindex];
kn[ 7] = binary[3+valindex];
valindex = s3[2*worka[12]+worka[17]]
[2*(2*(2*worka[13]+worka[14])+
worka[15])+worka[16]];
valindex = valindex * 4;
kn[ 8] = binary[0+valindex];
kn[ 9] = binary[1+valindex];
kn[10] = binary[2+valindex];
kn[11] = binary[3+valindex];
valindex = s4[2*worka[18]+worka[23]]
[2*(2*(2*worka[19]+worka[20])+
worka[21])+worka[22]];
valindex = valindex * 4;
kn[12] = binary[0+valindex];
kn[13] = binary[1+valindex];
kn[14] = binary[2+valindex];
kn[15] = binary[3+valindex];
valindex = s5[2*worka[24]+worka[29]]
[2*(2*(2*worka[25]+worka[26])+
worka[27])+worka[28]];
valindex = valindex * 4;
kn[16] = binary[0+valindex];
kn[17] = binary[1+valindex];
kn[18] = binary[2+valindex];
kn[19] = binary[3+valindex];
valindex = s6[2*worka[30]+worka[35]]
[2*(2*(2*worka[31]+worka[32])+
worka[33])+worka[34]];
valindex = valindex * 4;
kn[20] = binary[0+valindex];
kn[21] = binary[1+valindex];
kn[22] = binary[2+valindex];
kn[23] = binary[3+valindex];
valindex = s7[2*worka[36]+worka[41]]
[2*(2*(2*worka[37]+worka[38])+
worka[39])+worka[40]];
valindex = valindex * 4;
kn[24] = binary[0+valindex];
kn[25] = binary[1+valindex];
kn[26] = binary[2+valindex];
kn[27] = binary[3+valindex];
valindex = s8[2*worka[42]+worka[47]]
[2*(2*(2*worka[43]+worka[44])+
worka[45])+worka[46]];
valindex = valindex * 4;
kn[28] = binary[0+valindex];
kn[29] = binary[1+valindex];
kn[30] = binary[2+valindex];
kn[31] = binary[3+valindex];

/* Permute - P */
worka[ 0] = kn[15];
worka[ 1] = kn[ 6];
worka[ 2] = kn[19];
worka[ 3] = kn[20];
worka[ 4] = kn[28];
worka[ 5] = kn[11];
worka[ 6] = kn[27];
worka[ 7] = kn[16];
worka[ 8] = kn[ 0];
worka[ 9] = kn[14];
worka[10] = kn[22];
worka[11] = kn[25];
worka[12] = kn[ 4];
worka[13] = kn[17];
worka[14] = kn[30];
worka[15] = kn[ 9];
worka[16] = kn[ 1];
worka[17] = kn[ 7];
worka[18] = kn[23];
worka[19] = kn[13];
worka[20] = kn[31];
worka[21] = kn[26];
worka[22] = kn[ 2];
worka[23] = kn[ 8];
worka[24] = kn[18];
worka[25] = kn[12];
worka[26] = kn[29];
worka[27] = kn[ 5];
worka[28] = kn[21];
worka[29] = kn[10];
worka[30] = kn[ 3];
worka[31] = kn[24];

/* bufout XOR worka */
for (i = 0; i < 32; i++) {
bufout[i+32] = bufout[i] ^ worka[i];
bufout[i] = buffer[i];
}
} /* End of Iter */

/* Prepare Output */
for (i = 0; i < 32; i++) {
j = bufout[i];
bufout[i] = bufout[32+i];
bufout[32+i] = j;
}

/* Inverse Initial Permutation */
buffer[ 0] = bufout[39];
buffer[ 1] = bufout[ 7];
buffer[ 2] = bufout[47];
buffer[ 3] = bufout[15];
buffer[ 4] = bufout[55];
buffer[ 5] = bufout[23];
buffer[ 6] = bufout[63];
buffer[ 7] = bufout[31];
buffer[ 8] = bufout[38];
buffer[ 9] = bufout[ 6];
buffer[10] = bufout[46];
buffer[11] = bufout[14];
buffer[12] = bufout[54];
buffer[13] = bufout[22];
buffer[14] = bufout[62];
buffer[15] = bufout[30];
buffer[16] = bufout[37];
buffer[17] = bufout[ 5];
buffer[18] = bufout[45];
buffer[19] = bufout[13];
buffer[20] = bufout[53];
buffer[21] = bufout[21];
buffer[22] = bufout[61];
buffer[23] = bufout[29];
buffer[24] = bufout[36];
buffer[25] = bufout[ 4];
buffer[26] = bufout[44];
buffer[27] = bufout[12];
buffer[28] = bufout[52];
buffer[29] = bufout[20];
buffer[30] = bufout[60];
buffer[31] = bufout[28];
buffer[32] = bufout[35];
buffer[33] = bufout[ 3];
buffer[34] = bufout[43];
buffer[35] = bufout[11];
buffer[36] = bufout[51];
buffer[37] = bufout[19];
buffer[38] = bufout[59];
buffer[39] = bufout[27];
buffer[40] = bufout[34];
buffer[41] = bufout[ 2];
buffer[42] = bufout[42];
buffer[43] = bufout[10];
buffer[44] = bufout[50];
buffer[45] = bufout[18];
buffer[46] = bufout[58];
buffer[47] = bufout[26];
buffer[48] = bufout[33];
buffer[49] = bufout[ 1];
buffer[50] = bufout[41];
buffer[51] = bufout[ 9];
buffer[52] = bufout[49];
buffer[53] = bufout[17];
buffer[54] = bufout[57];
buffer[55] = bufout[25];
buffer[56] = bufout[32];
buffer[57] = bufout[ 0];
buffer[58] = bufout[40];
buffer[59] = bufout[ 8];
buffer[60] = bufout[48];
buffer[61] = bufout[16];
buffer[62] = bufout[56];
buffer[63] = bufout[24];

j = 0;
for (i = 0; i < 8; i++) {
*(dest + i) = 0x00;
for (k = 0; k < 7; k++)
*(dest + i) = ((*(dest + i)) + buffer[j+k]) * 2;
*(dest + i) = *(dest + i) + buffer[j+7];
j += 8;
}
}

 
 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。

上一篇:在CTreeCtrl中枚举系统中的所有窗口  下一篇:ElGamal算法