C加密/解密算法-将位位置2设置为当前位位置左侧的两个位

问题描述

我给出了关于该函数可能执行的功能的大量指令,但是我对如何执行1的(1)和(2)感到困惑。请我将位置2设置在当前位置的左边。我不太了解是否应该创建新函数来实现此目标,或者是否可以使用已经完成的setBit()函数?而且,如果确实需要创建新函数,我仍然对如何设置位以及回圈指令的含义感到困惑。

如果我能理解第一组指令并习惯于弄清楚如何设置位位置,我会更轻松地完成其余指令。我知道这是一个非常棘手的问题,但是我很迷失,不胜感激。如果有任何帮助或提示,或者朝正确的方向推进,我将不胜感激。

注意:只有主要功能才给我。我写了getBit,setBit和clearBit函数,但我觉得它们都不适用于问题1)。

我正在苦苦挣扎的说明:

(1) process the counter value,using the key value,as follows:
    (a) make a copy of the counter value into a temp counter
    (b) for every bit position,starting at bit position 7:
        (i) compute two bit positions (position 1 and position 2) that you will use to perform an xor
            between two of the temp counter bits: position 1 is set to the current bit position,and 
            position 2 is computed as follows:
            (1) if the key bit at the current bit position is 1,then position 2 is set to one bit 
                position to the left of the current bit position,assuming we circle back to the 
                least significant bits (for example,we consider bit 0 to be to the left of bit 7)
            (2) if the key bit at the current bit position is 0,then position 2 is set to two bit 
                positions to the left of the current bit position,assuming we circle back
        (ii) xor the two temp counter bits found at positions 1 and 2

main.c

int main()
{
  char str[8];
  int  choice;

  printf("\nYou may:\n");
  printf("  (1) Encrypt a message \n");
  printf("  (2) Decrypt a message \n");
  printf("\n  what is your selection: ");
  fgets(str,sizeof(str),stdin);
  sscanf(str,"%d",&choice);

  switch (choice) {
    case 1:
      break;
    case 2:
      break;
    default:
      break;
  }
  return 0;
}

unsigned char swapBitsOne(unsigned char counter,int bitOne,int bitTwo){

/* if the key bit at the current bit position is 1,then position 2 is set to one bit position to
the left of the current bit position,assuming we circle back to the least significant bits (for
example,we consider bit 0 to be to the left of bit 7)
*/

}

unsigned char swapBitsTwo(unsigned char counter,int bitTwo){
/* 
if the key bit at the current bit position is 0,then position 2 is set to two bit positions to
the left of the current bit position,assuming we circle back
*/  

}

unsigned char processCounter(unsigned char key,unsigned char counter){
    unsigned char tempCounter;
    strcpy(counter,tempCounter);
    int i;
    for(i = 7; i >= 0; --i){
        if(key == 1){ // if the key bit at the current bit position is 1
            counter = setBit(i,1
        }
        if(key == 0){
            counter = swapBitsTwo(); 
        }

    } 
    return tempCounter;
}


unsigned char getBit(unsigned char c,int n){
    return(c & 1 << n) >> n;
}


unsigned char setBit(unsigned char c,int n){ 
    return c | (1 << n);
}


unsigned char clearBit(unsigned char c,int n){ 
    return c & (~(1 << n));
}

解决方法

您有太多功能,使解决方案有点复杂。

首先,您不能在简单的strcpy参数上使用char。您需要传递char *参数。但是,只需使用简单的分配即可。

processCounter中...

您需要从key中提取给定/当前位位置的位(例如unsigned char keybit = getBit(key,i);)。

“位置1”始终是当前位置(即i)。 “位置2”基于keybit是什么。

您需要根据cpos1两个值,从pos2获取给定的位。

您需要根据这两位的XOR值来设置/清除输出中的结果位。


无论如何,这里有一些重构的代码可以满足您的需求。

请注意,我已经颠倒了ckey参数的顺序,以使其更加惯用(并且与其他[helper]函数的c参数一致)。 / p>

此外,您实际上不需要“复制”参数c,只需将其他变量用作返回值即可。那是因为结果的所有八位都被替换了。

unsigned char
getBit(unsigned char c,int n)
{
#if ORIG
    return (c & 1 << n) >> n;
#else
    return (c >> n) & 1;
#endif
}

unsigned char
setBit(unsigned char c,int n)
{
    return c | (1 << n);
}

unsigned char
clearBit(unsigned char c,int n)
{
#if ORIG
    return c & (~(1 << n));
#else
    return c & ~(1 << n);
#endif
}

unsigned char
processCounter(unsigned char c,unsigned char key)
{
    int curpos;
    int pos1;
    int pos2;
    unsigned char bit1;
    unsigned char bit2;
    unsigned char xor;
    unsigned char out = 0;

    for (curpos = 7;  curpos >= 0;  --curpos) {
        // 1.b.i
        pos1 = curpos;

        // 1.b.i.1
        if (getBit(key,curpos))
            pos2 = (curpos + 1) % 8;

        // 1.b.i.2
        else
            pos2 = (curpos + 2) % 8;

        // 1.b.ii
        bit1 = getBit(c,pos1);
        bit2 = getBit(c,pos2);
        xor = (bit1 ^ bit2) & 1;

        if (xor)
            out = setBit(out,curpos);
        else
            out = clearBit(out,curpos);
    }

    return out;
}

请注意,您可以将最终的if/else替换为:

out |= xor << curpos;

而且,您可以将1.b.i.1和1.b.i.2的if/else替换为:

pos2 = ((curpos + 2) - getBit(key,curpos)) % 8;