Cencryption/decryption算法-将位位置2设置为当前位位置左侧的两位位置
C encryption/decryption algorithm - set bit position 2 two bit positions to the left of the current bit position
我得到了大量关于算法可以执行哪些功能等方面的说明,但我对如何执行 (1) 和 (2) of 1.b 感到非常困惑) 它要求我将位置 2 设置到当前位置的左侧一些位。我不太明白我是否应该创建新函数来实现这一点,或者我是否可以使用我已经创建的 setBit() 函数?如果我确实需要创建新功能,我仍然对如何设置这些位以及绕回指令的含义感到困惑。
我觉得如果我能理解如何执行第一组指令并习惯弄清楚如何设置位位置,我就会更轻松地完成其余指令。我知道这是一个非常繁琐的问题,但我很感激任何帮助或提示,或者在正确的方向上推动,因为我很迷茫。
注意:给我的只是main函数。我写了 getBit、setBit 和 clearBit 函数,但我觉得其中 none 个适用于问题 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 bitOne, 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));
}
你的函数太多了,使解决方案有点太复杂了。
首先,您不能对简单的 char
参数使用 strcpy
。您需要传递 char *
个参数。但是,只需使用一个简单的赋值。
在processCounter
...
您需要从 key
中提取 given/current 位的位(例如 unsigned char keybit = getBit(key,i);
)。
“位置 1”始终 当前位置(即 i
)。
“位置 2”是基于 keybit
的。
您需要根据 pos1
和 pos2
的 两个 值从 c
中获取给定的位。
您需要根据这两个位的异或值 set/clear 输出中的结果位。
无论如何,这里有一些重构代码可以满足您的需求。
请注意,我已经颠倒了 c
和 key
参数的顺序,以使其更加惯用(并且与其他 [helper] 函数的 c
参数一致) .
此外,您实际上不需要“复制”参数 c
,只需为 return 值使用不同的变量即可。那是因为结果的所有八位都被替换了。
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;
我得到了大量关于算法可以执行哪些功能等方面的说明,但我对如何执行 (1) 和 (2) of 1.b 感到非常困惑) 它要求我将位置 2 设置到当前位置的左侧一些位。我不太明白我是否应该创建新函数来实现这一点,或者我是否可以使用我已经创建的 setBit() 函数?如果我确实需要创建新功能,我仍然对如何设置这些位以及绕回指令的含义感到困惑。
我觉得如果我能理解如何执行第一组指令并习惯弄清楚如何设置位位置,我就会更轻松地完成其余指令。我知道这是一个非常繁琐的问题,但我很感激任何帮助或提示,或者在正确的方向上推动,因为我很迷茫。
注意:给我的只是main函数。我写了 getBit、setBit 和 clearBit 函数,但我觉得其中 none 个适用于问题 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 bitOne, 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));
}
你的函数太多了,使解决方案有点太复杂了。
首先,您不能对简单的 char
参数使用 strcpy
。您需要传递 char *
个参数。但是,只需使用一个简单的赋值。
在processCounter
...
您需要从 key
中提取 given/current 位的位(例如 unsigned char keybit = getBit(key,i);
)。
“位置 1”始终 当前位置(即 i
)。
“位置 2”是基于 keybit
的。
您需要根据 pos1
和 pos2
的 两个 值从 c
中获取给定的位。
您需要根据这两个位的异或值 set/clear 输出中的结果位。
无论如何,这里有一些重构代码可以满足您的需求。
请注意,我已经颠倒了 c
和 key
参数的顺序,以使其更加惯用(并且与其他 [helper] 函数的 c
参数一致) .
此外,您实际上不需要“复制”参数 c
,只需为 return 值使用不同的变量即可。那是因为结果的所有八位都被替换了。
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;