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

如何解决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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-