博客
关于我
改进的恺撒加密术
阅读量:393 次
发布时间:2019-03-05

本文共 912 字,大约阅读时间需要 3 分钟。

凯撒密码是一种古老的加密方法,常用于古罗马时期的通信。其工作原理基于字母的循环替代特性,将每个字母替换为后面固定位置的字母。具体而言,用户可以选择一个偏移量t,使得每个字母被替换为后面第t个字母。

以“China”为例,当t=4时,字母“C”会被替换为后面第4个字母“G”,“h”替换为“l”,“i”替换为“m”,“n”替换为“r”,“a”替换为“e”,最终“China”会被加密为“Glmre”。

在编程实现方面,可以采用如下的方法:首先确定偏移量t,然后对于每个字符,计算其在字母表中的位置,进行循环移动。对于大写字母,若当前字符为'A'到'Z',则新字符为'A' + (n - (当前字符 - 'A')) % 26。同理,小写字母采用类似方法。

以下是一个简单的C语言实现示例:

#include 
#define N 5void Operation(int n, char *c) { if (*c >= 'a' && *c <= 'z') { *c = 'a' + (n - ('z' - *c) - 1) % 26; } else if (*c >= 'A' && *c <= 'Z') { *c = 'A' + (n - ('Z' - *c) - 1) % 26; } else { *c += n; }}int main() { char S[N]; int i = 0; int n; while (i < N) { scanf("%c", &S[i]); i++; } for (i = 0; i < N; i++) { Operation(n, &S[i]); } for (i = 0; i < N; i++) { printf("%c", S[i]); } return 0;}

以上代码实现了一个凯撒密码加密功能,可以根据需要调整偏移量t。通过循环替换每个字符,可以实现类似古罗马时期的通信方式。

转载地址:http://jksg.baihongyu.com/

你可能感兴趣的文章
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>