int QDes_keygen(const unsigned char *key) { unsigned char dupbuf[8]; unsigned char lkey[4], rkey[4]; int index, j; if(key == NULL) return QLIB_ERR_INVAL; memcpy(dupbuf, key, 8); permute(dupbuf, keyTransTable, 56); memset(lkey, 0, 4); memset(rkey, 0, 4); for(index = 0; index < 28; index++) bit_set(lkey, index, bit_get(dupbuf, index)); for(index = 0; index < 28; index++) bit_set(rkey, index, bit_get(dupbuf, index+28)); memset(subkeys, 0, sizeof(subkeys)); for(index = 0; index < 16; index++) { bit_rot_left(lkey, 28, keyShiftTable[index]); bit_rot_left(rkey, 28, keyShiftTable[index]); for(j = 0; j < 28; j++) bit_set(subkeys[index], j, bit_get(lkey, j)); for(j = 0; j < 28; j++) bit_set(subkeys[index], j+28, bit_get(rkey, j)); permute(subkeys[index], keyPermuteTable, 48); } return QLIB_SUCCEEDED; }
int main() { int pos, size = CHAR_BIT; unsigned char bits1[] = "1"; unsigned char bits2[] = "0"; unsigned char bits3[] = "10"; unsigned char bitsx[CHAR_BIT]; printf("bits in char = %d\n\n", CHAR_BIT * sizeof(char)); printf("bits1 = \"%s\" = ", bits1); for (pos = 0; pos < CHAR_BIT; pos++) printf("%d", bit_get(bits1, pos)); bit_set(bits1, 6, 1); printf("\n\nAfter setting seventh bit to 1:\nbits1 = \"%s\" = ", bits1); for (pos = 0; pos < CHAR_BIT; pos++) printf("%d", bit_get(bits1, pos)); bit_xor(bits1, bits2, bitsx, CHAR_BIT); printf("\n\n\"%s\" XOR \"%s\" = ", bits1, bits2); for (pos = 0; pos < CHAR_BIT; pos++) printf("%d", bit_get(bitsx, pos)); printf("\n\nbits3 = \"%s\" = ", bits3); for (pos = 0; pos < CHAR_BIT * 2; pos++) { if ((pos % 4) == 0) printf(" "); printf("%d", bit_get(bits3, pos)); } bit_rot_left(bits3, CHAR_BIT * 2, 3); printf("\nbit_rot_left(bits3, %d, %d) = ", CHAR_BIT * 2, 3); for (pos = 0; pos < CHAR_BIT * 2; pos++) { if ((pos % 4) == 0) printf(" "); printf("%d", bit_get(bits3, pos)); } return 0; }
static int des_main(const unsigned char *source, unsigned char *target, const unsigned char *key, DesEorD direction) { static unsigned char subkeys[16][7]; unsigned char temp[8], lkey[4], rkey[4], lblk[6], rblk[6], fblk[6], xblk[6], sblk; int row, col, i, j, k, p; /***************************************************************************** * * * If key is NULL, use the subkeys as computed in a previous call. * * * *****************************************************************************/ if (key != NULL) { /************************************************************************** * * * Make a local copy of the key. * * * **************************************************************************/ memcpy(temp, key, 8); /************************************************************************** * * * Permute and compress the key into 56 bits. * * * **************************************************************************/ permute(temp, DesTransform, 56); /************************************************************************** * * * Split the key into two 28-bit blocks. * * * **************************************************************************/ memset(lkey, 0, 4); memset(rkey, 0, 4); for (j = 0; j < 28; j++) bit_set(lkey, j, bit_get(temp, j)); for (j = 0; j < 28; j++) bit_set(rkey, j, bit_get(temp, j + 28)); /************************************************************************** * * * Compute the subkeys for each round. * * * **************************************************************************/ for (i = 0; i < 16; i++) { /*********************************************************************** * * * Rotate each block according to its round. * * * ***********************************************************************/ bit_rot_left(lkey, 28, DesRotations[i]); bit_rot_left(rkey, 28, DesRotations[i]); /*********************************************************************** * * * Concatenate the blocks into a single subkey. * * * ***********************************************************************/ for (j = 0; j < 28; j++) bit_set(subkeys[i], j, bit_get(lkey, j)); for (j = 0; j < 28; j++) bit_set(subkeys[i], j + 28, bit_get(rkey, j)); /*********************************************************************** * * * Do the permuted choice permutation. * * * ***********************************************************************/ permute(subkeys[i], DesPermuted, 48); } } /***************************************************************************** * * * Make a local copy of the source text. * * * *****************************************************************************/ memcpy(temp, source, 8); /***************************************************************************** * * * Do the initial permutation. * * * *****************************************************************************/ permute(temp, DesInitial, 64); /***************************************************************************** * * * Split the source text into a left and right block of 32 bits. * * * *****************************************************************************/ memcpy(lblk, &temp[0], 4); memcpy(rblk, &temp[4], 4); /***************************************************************************** * * * Encipher or decipher the source text. * * * *****************************************************************************/ for (i = 0; i < 16; i++) { /************************************************************************** * * * Begin the computation of f. * * * **************************************************************************/ memcpy(fblk, rblk, 4); /************************************************************************** * * * Permute and expand the copy of the right block into 48 bits. * * * **************************************************************************/ permute(fblk, DesExpansion, 48); /************************************************************************** * * * Apply the appropriate subkey for the round. * * * **************************************************************************/ if (direction == encipher) { /*********************************************************************** * * * For enciphering, subkeys are applied in increasing order. * * * ***********************************************************************/ bit_xor(fblk, subkeys[i], xblk, 48); memcpy(fblk, xblk, 6); } else { /*********************************************************************** * * * For deciphering, subkeys are applied in decreasing order. * * * ***********************************************************************/ bit_xor(fblk, subkeys[15 - i], xblk, 48); memcpy(fblk, xblk, 6); } /************************************************************************** * * * Do the S-box substitutions. * * * **************************************************************************/ p = 0; for (j = 0; j < 8; j++) { /*********************************************************************** * * * Compute a row and column into the S-box tables. * * * ***********************************************************************/ row = (bit_get(fblk, (j * 6)+0) * 2) + (bit_get(fblk, (j * 6)+5) * 1); col = (bit_get(fblk, (j * 6)+1) * 8) + (bit_get(fblk, (j * 6)+2) * 4) + (bit_get(fblk, (j * 6)+3) * 2) + (bit_get(fblk, (j * 6)+4) * 1); /*********************************************************************** * * * Do the S-box substitution for the current six-bit block. * * * ***********************************************************************/ sblk = (unsigned char)DesSbox[j][row][col]; for (k = 4; k < 8; k++) { bit_set(fblk, p, bit_get(&sblk, k)); p++; } } /************************************************************************** * * * Do the P-box permutation to complete f. * * * **************************************************************************/ permute(fblk, DesPbox, 32); /************************************************************************** * * * Compute the XOR of the left block and f. * * * **************************************************************************/ bit_xor(lblk, fblk, xblk, 32); /************************************************************************** * * * Set the left block for the round. * * * **************************************************************************/ memcpy(lblk, rblk, 4); /************************************************************************** * * * Set the right block for the round. * * * **************************************************************************/ memcpy(rblk, xblk, 4); } /***************************************************************************** * * * Set the target text to the rejoined final right and left blocks. * * * *****************************************************************************/ memcpy(&target[0], rblk, 4); memcpy(&target[4], lblk, 4); /***************************************************************************** * * * Do the final permutation. * * * *****************************************************************************/ permute(target, DesFinal, 64); return 0; }
/** * 应用DES加密解密的主要方法 * @param source : * @param target : * @param key : * @param derection : 表示是加密还是解密 * */ static int des_main(const unsigned char *source, unsigned char *target, const unsigned char *key, DesEorD direction){ static unsigned char subkeys[16][17]; unsigned char temp[8], lkey[4], rkey[4], lblk[6], rblk[6], fblk[6], xblk[6], sblk; int row, col, i, j, k, p; /** * if key is NULL, use the subkeys as computed in a previous call. */ if (key != NULL) { //对密钥做一下备份,在密钥的备份上做操作 memcpy(temp, key, 8); //将密钥初始转换,得到一组56位的密钥 permute(temp, DesTransform, 56); //将密钥分成两组28位的数据, //先清零两组密钥 memset(lkey, 0, 4); memset(rkey, 0, 4); //0~27位放入到lkey中, 28~57位放入到rkey中 for (j = 0; j < 28; ++j) { bit_set(lkey, j, bit_get(temp, j)); bit_set(rkey, j, bit_get(temp, j + 28)); } //compute the subkeys for each round for (i = 0; i < 16; ++i) { //根据DesRotations数组中的值分别对lkey和rkey进行翻转操作 bit_rot_left(lkey, 28, DesRotations[i]); bit_rot_left(rkey, 28, DesRotations[i]); //对lkey 和rkey分别进行遍历 //取出其每一位的状态值, //放入到subkeys[i]中,lkey的位的值放入到前28位,rkey的位的值放入到后28位 for (j = 0; j < 28; ++j) { int status_lkey = bit_get(lkey, j); int status_rkey = bit_get(rkey, j); bit_set(subkeys[i], j, status_lkey); bit_set(subkeys[i], j + 28, status_rkey); } //对subkeys[i]进行转换,由56位转成48位,转换表由数组DesPermuted指定 permute(subkeys[i], DesPermuted, 48); } }//对key的预处理操作完成 //备份source到temp中,然后对temp进行操作,只备份8个字节的数据,也就是只处理64位数据 memcpy(temp, source, 8); //对源进行初始化转换,转换表由DesInitial数组指定,位数不变,依然是64位 permute(temp, DesInitial, 64); //将转换之后的源,分成两组, 每组32位,lblk前32位, rblk后32位 memcpy(lblk, &temp[0], 4); memcpy(rblk, &temp[4], 4); for (i = 0; i < 16; ++i) { //将rblk的32位复制到fblk中 memcpy(fblk, rblk, 4); //将fblk中的32位转换并扩展到48位 permute(fblk, DesExpansion, 48); //如果是加密 if (direction == encipher) { //将fblk与subkeys[i]进行异或处理,结果放置在xblk中 bit_xor(fblk, subkeys[i], xblk, 48); } else/*如果是解密*/ { //将fblk与subkeys[15 - i]进行异或处理,结果放置在xblk中 bit_xor(fblk, subkeys[15 - i], xblk, 48); } //将异或之后的结果复制到fblk中 memcpy(fblk, xblk, 6); //开始S盒转换, 将每6位转换成4位,这样48位就又转成成32位 p = 0; for (j = 0; j < 8; ++j) { //分割成6位6位的去取,转换成S盒中对应的行数列数,找到对应的S盒中的一个4位的值 int r0 = bit_get(fblk, (j * 6) + 0); int r5 = bit_get(fblk, (j * 6) + 5); row = 2 * r0 + 1 * r5; int r1 = bit_get(fblk, (j * 6) + 1); int r2 = bit_get(fblk, (j * 6) + 2); int r3 = bit_get(fblk, (j * 6) + 3); int r4 = bit_get(fblk, (j * 6) + 4); col = 8 * r1 + 4 * r2 + 2 * r3 + 1 * r4; //根据获取的行数列数,从DesSbox的第j个盒子中取出一个4位的值, sblk = (unsigned char)DesSbox[j][row][col]; for (k = 4; k < 8; ++k) { //这个值在低4位,所以遍历低4位,从左往右数,右边是低位,左边是高位 int status = bit_get(&sblk, k); //将状态值复制到fblk中的第p位,循环完成之后,fblk就是总共32位的值 bit_set(fblk, p, status); p++; } } //进行p盒转换,位数不变 permute(fblk, DesPbox, 32); //上面对fblk的操作都是对rblk的一系列的操作, //这里,将rblk与lblk进行异或操作,结果放置在xblk中, bit_xor(lblk, fblk, xblk, 32); //将异或之后的结果放入到rblk中,上一步的rblk的值放入到lblk中 memcpy(lblk, rblk, 4); memcpy(rblk, xblk, 4); //进入下一轮的循环,如此操作16次 } //将如上进过16次处理的rblk和lblk的值放入到target的前4字节和后4字节中 memcpy(&target[0], rblk, 4); memcpy(&target[4], lblk, 4); //最终转换 permute(target, DesFinal, 64); return 0; }
int main(int argc, char **argv) { unsigned char bits1[8], bits2[8], bits3[8]; int i; /***************************************************************************** * Perform some bit operations using 64-bit buffers. * *****************************************************************************/ for (i = 0; i < 8; i++) { bits1[i] = 0x00; bits2[i] = 0x00; bits3[i] = 0x00; } fprintf(stdout, "Initially\n"); fprintf(stdout, "bits1: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits1[0], bits1[1], bits1[2], bits1[3], bits1[4], bits1[5], bits1[6], bits1[7]); fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0], bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]); bit_set(bits1, 15, 1); bit_set(bits1, 16, 1); bit_set(bits1, 32, 1); bit_set(bits1, 63, 1); bit_set(bits2, 0, 1); bit_set(bits2, 15, 1); fprintf(stdout, "After setting bits 15, 16, 32, 63 of bits1 and bits 00, 15 " "of bits2\n"); fprintf(stdout, "bits1: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits1[0], bits1[1], bits1[2], bits1[3], bits1[4], bits1[5], bits1[6], bits1[7]); fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0], bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]); fprintf(stdout, "Bit 63 of bits1 is %d\n", bit_get(bits1, 63)); fprintf(stdout, "Bit 62 of bits1 is %d\n", bit_get(bits1, 62)); fprintf(stdout, "Bit 00 of bits2 is %d\n", bit_get(bits2, 0)); fprintf(stdout, "Bit 01 of bits2 is %d\n", bit_get(bits2, 1)); bit_xor(bits1, bits2, bits3, 32); fprintf(stdout, "bits3 is bits1 XOR bits2 (32 bits)\n"); fprintf(stdout, "bits3: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits3[0], bits3[1], bits3[2], bits3[3], bits3[4], bits3[5], bits3[6], bits3[7]); bit_xor(bits1, bits2, bits3, 64); fprintf(stdout, "bits3 is bits1 XOR bits2 (64 bits)\n"); fprintf(stdout, "bits3: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits3[0], bits3[1], bits3[2], bits3[3], bits3[4], bits3[5], bits3[6], bits3[7]); bit_rot_left(bits1, 64, 1); fprintf(stdout, "After rotating bits1 left x 1 (64 bits)\n"); fprintf(stdout, "bits1: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits1[0], bits1[1], bits1[2], bits1[3], bits1[4], bits1[5], bits1[6], bits1[7]); bit_rot_left(bits2, 64, 1); fprintf(stdout, "After rotating bits2 left x 1 (64 bits)\n"); fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0], bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]); bit_rot_left(bits2, 16, 7); fprintf(stdout, "After rotating bits2 left x 7 (16 bits)\n"); fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0], bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]); bit_rot_left(bits2, 8, 2); fprintf(stdout, "After rotating bits2 left x 2 (8 bits)\n"); fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0], bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]); for (i = 0; i < 8; i++) { bits2[i] = 0x00; } bit_set(bits2, 0, 1); bit_set(bits2, 3, 1); bit_set(bits2, 8, 1); bit_set(bits2, 27, 1); fprintf(stdout, "After clearing and setting bits 0, 3, 8, 27 of bits2\n"); fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0], bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]); bit_rot_left(bits2, 11, 6); fprintf(stdout, "After rotating bits2 left x 6 (11 bits)\n"); fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0], bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]); for (i = 0; i < 8; i++) { bits2[i] = 0x00; } bit_set(bits2, 0, 1); bit_set(bits2, 3, 1); bit_set(bits2, 8, 1); bit_set(bits2, 27, 1); fprintf(stdout, "After clearing and setting bits 0, 3, 8, 27 of bits2\n"); fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0], bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]); bit_rot_left(bits2, 28, 4); fprintf(stdout, "After rotating bits2 left x 4 (28 bits)\n"); fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0], bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]); return 0; }