如何用python编写凯撒密码 (凯撒密码编程实现)

2023-03-13 21:39:15 密码用途 思思

凯撒密码是对字母表整体进行偏移的一种变换加密。因此,建立一个字母表,对明文中每个字母,在这个字母表中偏移固定的长度即可得到对应的密文字母。

最基本的实现如下:

def caesarcipher(s: str,rot: int=3) -str:

    _ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    encode = ''

    i = 0

    for c in s:

        try:

            encode += _[(_.index(c.upper()) + rot) % len(_)]

        except (Exception,) as e:

            encode += c

    return encode

print(caesarcipher('hellow'))

print(caesarcipher('KHOORZ', -3))

如果要求解密后保持大小写,那么,字母表_还需要包含所有小写字母并且index时不对c做upper处理.

同样的,也可以在字母表中追加数字,各种符号,空格等.

凯撒密码用C++编写

#include stdio.h

#include stdlib.h

#include string.h

const int MAX_N=200;

int main(int argc, char *argv[])

{

int i,j,p;

char text[MAX_N];

char alphabet[30];

char op[10];

while(1)

{

printf("1---输入密码表 2---退出\n");

gets(op);

if(strcmp(op,"1")==0)

{

printf("密码表:");

gets(alphabet);

while(1)

{

printf("1---加密 2---解密 3---返回\n");

gets(op);

if(strcmp(op,"1")==0 ||strcmp(op,"2")==0 )

{

printf("输入文本:");

gets(text);

for(i=0;text[i]!='\0';i++)

{

if((text[i]='a'text[i]='z') || (text[i]='A'text[i]='Z') )

{

if(strcmp(op,"1")==0)

{

p=text[i]='a'? (text[i]-'a'):(text[i]-'A');

text[i]=text[i]+ alphabet[p]-(p+'A');

}

else

{

for(j=0;;j++)

if(alphabet[j]==text[i]||alphabet[j]==(text[i]-('a'-'A')))

break;

text[i]= text[i]='a' ? (j+'a') :(j+'A');

}

}

}//for(i)

if(strcmp(op,"1")==0)

printf("加密后的文本为:" );

else

printf("解密后的文本为:");

printf("%s\n\n",text);

}

else if(strcmp(op,"3")==0)

{

printf("\n");

break;

}

else

{

printf("选择有误!请重新选择!\n");

}

}//while(1)

}

else if(strcmp(op,"2")==0)

{

exit(1);

}

else

{

printf("选择有误!请重新选择!\n");

}

}

return 0;

}

/*

输入样例

QWERTYUIOPASDFGHJKLZXCVBNM

Welcome to ZZSY2009!

输出样例

Vtsegdt zg MMLN2009!

*/

凯撒密码的算法c语言的怎么实现啊?

凯撒密码是一种非常古老的加密方法,相传当年凯撒大地行军打仗时为了保证自己的命令不被敌军知道,就使用这种特殊的方法进行通信,以确保信息传递的安全。他的原理很简单,说到底就是字母于字母之间的替换。下面让我们看一个简单的例子:“baidu”用凯撒密码法加密后字符串变为“edlgx”,它的原理是什么呢?把“baidu”中的每一个字母按字母表顺序向后移3位,所得的结果就是刚才我们所看到的密文。

#include stdio.h

main()

{

char M[100];

char C[100];

int K=3,i;

printf("请输入明文M(注意不要输入空白串)\n");

gets(M);

for(i=0;M[i]!='\0';i++)

C[i]=(M[i]-'a'+K)%26+'a';

C[i]='\0';

printf("结果是:\n%s\n",C);

}

凯撒密码 C语言

#includestdio.h

#includestring.h

void main ()

{

char str[100];

char str1[100];

printf("输入字符串:");

scanf("%s",str);

int len;

len=strlen(str);

for(int i=0;i<len;i++)

{

str1[i]=(str[i]-97+3)%26+97;

}

str1[len]='\0';

printf ("密文为:%s\n",str1);

}

python凯撒密码实现

# codinng=utf-8

x = 'a b c d e f g h i j k l m n o p q r s t u v w x y z'.split(' ')

y = 'n o p q r s t u v w x y z a b c d e f g h i j k l m'.split(' ')

X = map(lambda x: x.upper(), x)

Y = map(lambda x: x.upper(), y)

dict_kaisa = dict(zip(x + X, y + Y))  # 创建一个字典, 键为原字符串, 值为加密字符串

# 定义凯撒加密函数, 输入字符串, 输出凯撒加密后字符串

def kaisa(string):

    result = []

    for i in range(len(string)):

        if string[i] in dict_kaisa.keys():

            result.append(dict_kaisa[string[i]])

        else:

            result.append(string[i])

    return ''.join(result)

print(kaisa('The Zen of Python'))  # 结果为Gur Mra bs Clguba