?? des.c
字號:
DumpBin("input(right)", inBlk, 32); DumpBin("raw key(left )", key+28, 28); DumpBin("raw key(right)", key, 28); /* Compute the first roundkey by performing PC1 */ ComputeRoundKey(roundKey, key); DumpBin("roundKey(L)", roundKey+28, 28); DumpBin("roundKey(R)", roundKey, 28); /* Compute the initial permutation and divide the result into L and R */ ComputeIP(L,R,inBlk); DumpBin("after IP(L)", L, 32); DumpBin("after IP(R)", R, 32); for (round = 0; round < 16; round++) { if (verbose) printf("-------------- BEGIN DECRYPT ROUND %d -------------\n", round); DumpBin("round start(L)", L, 32); DumpBin("round start(R)", R, 32); /* Compute f(R, roundKey) and exclusive-OR onto the value in L */ ComputeF(fout, R, roundKey); DumpBin("f(R,key)", fout, 32); for (i = 0; i < 32; i++) L[i] ^= fout[i]; DumpBin("L^f(R,key)", L, 32); Exchange_L_and_R(L,R); /* Rotate roundKey halves right once or twice (depending on round) */ DumpBin("roundKey(L)", roundKey+28, 28); /* show keys before shift */ DumpBin("roundKey(R)", roundKey, 28); RotateRoundKeyRight(roundKey); if (round != 0 && round != 7 && round != 14 && round != 15) RotateRoundKeyRight(roundKey); DumpBin("round end(L)", L, 32); DumpBin("round end(R)", R, 32); if (verbose) printf("--------------- END ROUND %d --------------\n", round); } Exchange_L_and_R(L,R); /* Combine L and R then compute the final permutation */ ComputeFP(outBlk,L,R); DumpBin("FP out( left)", outBlk+32, 32); DumpBin("FP out(right)", outBlk, 32);}/* * ComputeRoundKey: Compute PC1 on the key and store the result in roundKey */static void ComputeRoundKey(bool roundKey[56], bool key[56]) { int i; for (i = 0; i < 56; i++) roundKey[table_DES_PC1[i]] = key[i];}/* * RotateRoundKeyLeft: Rotate each of the halves of roundKey left one bit */static void RotateRoundKeyLeft(bool roundKey[56]) { bool temp1, temp2; int i; temp1 = roundKey[27]; temp2 = roundKey[55]; for (i = 27; i >= 1; i--) { roundKey[i] = roundKey[i-1]; roundKey[i+28] = roundKey[i+28-1]; } roundKey[ 0] = temp1; roundKey[28] = temp2;}/* * RotateRoundKeyRight: Rotate each of the halves of roundKey right one bit */static void RotateRoundKeyRight(bool roundKey[56]) { bool temp1, temp2; int i; temp1 = roundKey[0]; temp2 = roundKey[28]; for (i = 0; i < 27; i++) { roundKey[i] = roundKey[i+1]; roundKey[i+28] = roundKey[i+28+1]; } roundKey[27] = temp1; roundKey[55] = temp2;}/* * ComputeIP: Compute the initial permutation and split into L and R halves. */static void ComputeIP(bool L[32], bool R[32], bool inBlk[64]) { bool output[64]; int i; /* Permute */ for (i = 63; i >= 0; i--) output[table_DES_IP[i]] = inBlk[i]; /* Split into R and L. Bits 63..32 go in L, bits 31..0 go in R. */ for (i = 63; i >= 0; i--) { if (i >= 32) L[i-32] = output[i]; else R[i] = output[i]; }}/* * ComputeFP: Combine the L and R halves and do the final permutation. */static void ComputeFP(bool outBlk[64], bool L[32], bool R[32]) { bool input[64]; int i; /* Combine L and R into input[64] */ for (i = 63; i >= 0; i--) input[i] = (i >= 32) ? L[i - 32] : R[i]; /* Permute */ for (i = 63; i >= 0; i--) outBlk[table_DES_FP[i]] = input[i];}/* * ComputeF: Compute the DES f function and store the result in fout. */static void ComputeF(bool fout[32], bool R[32], bool roundKey[56]) { bool expandedBlock[48], subkey[48], sout[32]; int i,k; /* Expand R into 48 bits using the E expansion */ ComputeExpansionE(expandedBlock, R); DumpBin("expanded E", expandedBlock, 48); /* Convert the roundKey into the subkey using PC2 */ ComputePC2(subkey, roundKey); DumpBin("subkey", subkey, 48); /* XOR the subkey onto the expanded block */ for (i = 0; i < 48; i++) expandedBlock[i] ^= subkey[i]; /* Divide expandedBlock into 6-bit chunks and do S table lookups */ for (k = 0; k < 8; k++) ComputeS_Lookup(k, sout+4*k, expandedBlock+6*k); /* To complete the f() calculation, do permutation P on the S table output */ ComputeP(fout, sout);}/* * ComputeP: Compute the P permutation on the S table outputs. */static void ComputeP(bool output[32], bool input[32]) { int i; for (i = 0; i < 32; i++) output[table_DES_P[i]] = input[i];}/* * Look up a 6-bit input in S table k and store the result as a 4-bit output. */static void ComputeS_Lookup(int k, bool output[4], bool input[6]) { int inputValue, outputValue; /* Convert the input bits into an integer */ inputValue = input[0] + 2*input[1] + 4*input[2] + 8*input[3] + 16*input[4] + 32*input[5]; /* Do the S table lookup */ outputValue = table_DES_S[k][inputValue]; /* Convert the result into binary form */ output[0] = (outputValue & 1) ? 1 : 0; output[1] = (outputValue & 2) ? 1 : 0; output[2] = (outputValue & 4) ? 1 : 0; output[3] = (outputValue & 8) ? 1 : 0;}/* * ComputePC2: Map a 56-bit round key onto a 48-bit subkey */static void ComputePC2(bool subkey[48], bool roundKey[56]) { int i; for (i = 0; i < 48; i++) subkey[i] = roundKey[table_DES_PC2[i]];}/* * ComputeExpansionE: Compute the E expansion to prepare to use S tables. */static void ComputeExpansionE(bool expandedBlock[48], bool R[32]) { int i; for (i = 0; i < 48; i++) expandedBlock[i] = R[table_DES_E[i]];}/* * Exchange_L_and_R: Swap L and R */static void Exchange_L_and_R(bool L[32], bool R[32]) { int i; for (i = 0; i < 32; i++) L[i] ^= R[i] ^= L[i] ^= R[i]; /* exchanges L[i] and R[i] */}/* * DumpBin: Display intermediate values if emableDumpBin is set. */static void DumpBin(char *str, bool *b, int bits) { int i; if ((bits % 4)!=0 || bits>48) { printf("Bad call to DumpBin (bits > 48 or bit len not a multiple of 4\n"); exit(1); } if (EnableDumpBin) { for (i = strlen(str); i < 14; i++) printf(" "); printf("%s: ", str); for (i = bits-1; i >= 0; i--) printf("%d", b[i]); printf(" "); for (i = bits; i < 48; i++) printf(" "); printf("("); for (i = bits-4; i >= 0; i-=4) printf("%X", b[i]+2*b[i+1]+4*b[i+2]+8*b[i+3]); printf(")\n"); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -