凯撒密码程序(凯撒密码程序设计题)

2023-02-12 18:33:21 密码用途 思思

凯撒密码用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!

*/

凯撒密码程序(凯撒密码程序设计题) 第1张

凯撒密码用VB如何编写程序

这个很简单嘛,用ASC 和 CHR 转换一下嘛

打开Vb,添加2个label控件,2个text控件 1个command按钮

代码如下:

Private Function f(ByVal a As String, k As Integer, n As Integer) As String

If ((Asc(a) = 65 And Asc(a) = 97) Or (Asc(a) = 97 And Asc(a) = 122)) And Len(a) = 1 Then '判断是否为一个字母

'利用公式计算

If Asc(a) = 65 And Asc(a) = 97 Then f = Chr((Asc(a) - 64 + k) Mod n + 64) '当为大写的时候

If Asc(a) = 97 And Asc(a) = 122 Then f = Chr((Asc(a) - 96 + k) Mod n + 96) '当为小写的时候

Else

f = "error" '若不满足要求,则返回错误

End If

End Function

Private Sub Command1_Click()

Dim strold As String

Dim strnew As String

Dim k As Integer

Dim n As Integer

Dim i As Long

Dim tmp As String

k = 3

n = 26

strold = Text1.Text '要加密的字符串

For i = 1 To Len(strold)

tmp = Mid(strold, i, 1)

tmp = f(tmp, k, n)

If tmp "error" Then

strnew = strnew + tmp

Else

MsgBox "字符串中含有非法字符"

Exit Sub

End If

Next i

Text2.Text = strnew

End Sub

Private Sub Form_Load()

Text1.Text = "PROGRAM"

Text2.Text = ""

Command1.Caption = "加密"

Label1.Caption = "源字符串:"

Label2.Caption = "加密字符串:"

End Sub

开源项目:凯撒密码转换器

几天前一次和朋友聊天谈到了加密聊天,于是想到了凯撒密码,随之就是想用自己的一点烂技术写点好玩的:

下面是成品说明

本程序可以将用户输入的内容通过 非固定的凯撒密码字典 进行加/解密,是一种好用加密的加密聊天方式。

Caesar cipher,发明者Caesar(凯撒),罗马人。

根据苏维托尼乌斯的记载,恺撒曾用此方法对重要的军事信息进行加密:

如果需要保密,信中便用暗号,也即是改变字母顺序,使局外人无法组成一个单词。如果想要读懂和理解它们的意思,得用第4个字母置换第一个字母,即以D代A,余此类推。

同样,奥古斯都也使用过类似方式,只不过他是把字母向右移动一位,而且末尾不折回。每当他用密语写作时,他都用B代表A,C代表B,其余的字母也依同样的规则;他用A代表Z。

另外,有证据表明,恺撒曾经使用过更为复杂的密码系统:文法学家普罗布斯曾经写过一份独具创新的手稿,研究恺撒书信中包含有秘密信息的字母。

已经无法弄清恺撒密码在当时有多大的效果,但是有理由相信它是安全的。因为恺撒大部分敌人都是目不识丁的,而其余的则可能将这些消息当作是某个未知的外语。即使有某个敌人获取了恺撒的加密信息,根据现有的记载,当时也没有任何技术能够解决这一最基本、最简单的替换密码。现存最早的破解方法记载在公元9世纪阿拉伯的阿尔·肯迪的有关发现频率分析的著作中。

这是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以罗马共和时期凯撒的名字命名的,当年凯撒曾用此方法与其将军们进行联系。

进入程序后,会有像下面这样的提示:

随便输入一段英文(此程序仅支持英文和数字,原因看原理):例如 Fuck you

然后回车:

我们在这输入的是可看的原文,所以我们要加密,输入1,回车:

我们以位移+3(这是当年的默认值,输入其他值可以为负,但是必须加负号,正数的正号加不加无所谓)为例,输入并回车:

程序里面用了循环,所以输出完后会自动进行下一次,这适合正在加密聊天的折腾者。

同样,我们把加密好的文本复制完后重新输入,选择2进行反加密(解密),位移了多少仍然填3:

这样解密就成功了,是不是真的很适合爱折腾的你?赶紧发给你的朋友,约定一个固定的位移数(加密密钥)进行愉快的无限制聊天吧!

存放主程序及图标文件

英文版主程序(在en-ww文件夹内)

程序图标,透明底的

中文版主程序

日志文件,主要通过自编模块 keeplog 生成,内容为“日期(Y/M/D)+时间(h/m/s)+加密密钥+输入/输出内容+分割线”

凯撒密码实现英文短句的加解密

1. 将“We are students.”这个英文词句用k=4的凯萨密码翻译成密码

1. 恺撒密码,

作为一种最为古老的对称加密体制,他的基本思想是:

通过把字母移动一定的位数来实现加密和解密。

例如,如果密匙是把明文字母的位数向后移动三位,那么明文字母B就变成了密文的E,依次类推,X将变成A,Y变成B,Z变成C,由此可见,位数就是凯撒密码加密和解密的密钥。

如:ZHDUHVWXGHQWV(后移三位)

2. 凯撒密码,

是计算机C语言编程实现加密和解密。挺复杂的。你可以研究一下哦。

2. 将凯撒密码(K=7)的加密、解密过程用C语言编程实现

/*

声明:MSVC++6.0环境测试通过

*/

#includestdio.h

#includectype.h

#define maxlen 100

#define K 7

char *KaisaEncode(char *str)//加密

{

char *d0;

d0=str;

for(;*str!='\0';str++)

{

if(isupper(*str))

*str=(*str-'A'+K)%26+'A';

else if(islower(*str))

*str=(*str-'a'+K)%26+'a';

else

continue;

}

return d0;

}

char *KaisaDecode(char *str)//解密

{

char *d0;

d0=str;

for(;*str!='\0';str++)

{

if(isupper(*str))

*str=(*str-'A'-K+26)%26+'A';

else if(islower(*str))

*str=(*str-'a'-K+26)%26+'a';

else

continue;

}

return d0;

}

int main(void)

{

char s[maxlen];

gets(s);

puts(KaisaEncode(s));

puts(KaisaDecode(s));

return 0;

}

3. 将凯撒密码X的加密、解密过程用C语言编程实现

(2)kaiser加密算法 具体程序:#include #include char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/ { while(ch='A'ch='a'ch='z') { return ('a'+(ch-'a'+n)%26); } return ch; } void menu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/ { clrscr(); printf("\n========================================================="); printf("\n1.Encrypt the file"); printf("\n2.Decrypt the file"); printf("\n3.Force decrypt file"); printf("\n4.Quit\n"); printf("=========================================================\n"); printf("Please select a item:"); return; } main() { int i,n; char ch0,ch1; FILE *in,*out; char infile[20],outfile[20]; textbackground(BLACK); textcolor(LIGHTGREEN); clrscr(); sleep(3);/*等待3秒*/ menu(); ch0=getch(); while(ch0!='4') { if(ch0=='1') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要加密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the key:"); scanf("%d",n);/*输入加密密码*/ printf("Please input the outfile:"); scanf("%s",outfile);/*输入加密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } while(!feof(in))/*加密*/ { fputc(encrypt(fgetc(in),n),out); } printf("\nEncrypt is over!\n"); fclose(in); fclose(out); sleep(1); } if(ch0=='2') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要解密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the key:"); scanf("%d",n);/*输入解密密码(可以为加密时候的密码)*/ n=26-n; printf("Please input the outfile:"); scanf("%s",outfile);/*输入解密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } while(!feof(in)) { fputc(encrypt(fgetc(in),n),out); } printf("\nDecrypt is over!\n"); fclose(in); fclose(out); sleep(1); } if(ch0=='3') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要解密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the outfile:"); scanf("%s",outfile);/*输入解密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } for(i=1;i=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/ { rewind(in); rewind(out); clrscr(); printf("==========================================================\n"); printf("The outfile is:\n"); printf("==========================================================\n"); while(!feof(in)) { ch1=encrypt(fgetc(in),26-i); putch(ch1); fputc(ch1,out); } printf("\n========================================================\n"); printf("The current key is: %d \n",i);/*显示当前破解所用密码*/ printf("Press 'Q' to quit and other key to continue。

\n"); printf("==========================================================\n"); ch1=getch(); if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/ { clrscr(); printf("\nGood Bye!\n"); fclose(in); fclose(out); sleep(3); exit(0); } } printf("\nForce decrypt is over!\n"); fclose(in); fclose(out); sleep(1); } menu(); ch0=getch(); } clrscr(); printf("\nGood Bye!\n"); sleep(3); }。

4. 怎样编写程序:实现恺撒密码加密单词"julus"

用下面程序:新建个txt,放进去任意单词,设置#define N 5中的值,实现字母移位,达到加密目的。

本程序提供解密功能/************************************************************************//* 版权所有:信息工程学院 王明 使用时请注明出处!! *//* 算法:凯撒密码体制 e799bee5baa6e4b893e5b19e31333264643062 *//************************************************************************/#include #define N 5void jiami(char namea[256]) { FILE *fp_jiami,*fp_file2; char c; fp_jiami=fopen(namea,"rb"); fp_file2=fopen("file2.txt","wb"); while(EOF!=(fscanf(fp_jiami,"%c",c))) { if((c='A'c='a'c='A'c='a'c='a'c='A'c='a'c='A'c='a'c='A'c='Z')c=c+32; } fprintf(fp_file3,"%c",c); } fclose(fp_file3); fclose(fp_jiemi); }int main(){ char name[256]; int n; printf("输入你要操作的TXT文本:"); gets(name); printf("\n请选择需要进行的操作:\n"); printf(" 1:加密 2:解密 \n"); printf("输入你的选择:"); scanf("%d",n); switch(n) { case 1:{jiami(name);printf("\t加密成功!!\n\n"); break;} case 2:{jiemi(name);printf("\t解密成功!!\n\n"); break;} default:{printf("输入操作不存在!");} } return 0;}。

5. 谁有PYTHON编写的凯撒密码的加密和解密代码

给你写了一个.

def convert(c, key, start = 'a', n = 26):

a = ord(start)

offset = ((ord(c) - a + key)%n)

return chr(a + offset)

def caesarEncode(s, key):

o = ""

for c in s:

if c.islower():

o+= convert(c, key, 'a')

elif c.isupper():

o+= convert(c, key, 'A')

else:

o+= c

return o

def caesarDecode(s, key):

return caesarEncode(s, -key)

if __name__ == '__main__':

key = 3

s = 'Hello world!'

e = caesarEncode(s, key)

d = caesarDecode(e, key)

print e

print d

运行结果:

Khoor zruog!

Hello world!

凯撒加密算法(最简单的对称加密)

凯撒密码是罗马扩张时期朱利斯• 凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令。它将字母表中的字母移动一定位置而实现加密。例如如果向右移动 2 位,则 字母 A 将变为 C,字母 B 将变为 D,…,字母 X 变成 Z,字母 Y 则变为 A,字母 Z 变为 B。

因此,假如有个明文字符串“Hello”用这种方法加密的话,将变为密文: “Jgnnq” 。而如果要解密,则只要将字母向相反方向移动同样位数即可。如密文“Jgnnq”每个字母左移两位 变为“Hello” 。这里,移动的位数“2”是加密和解密所用的密钥。

该程序既可用于加密又可用于解密。只要传入明文和偏移量即可加密,解密需要传入密文和负的偏移量就可以解密。

输出的结果:

凯撒密码由于加解密比较简单,密钥总共只有 26 个,攻击者得到密文后即使不知道密钥,也可一个一个地试过去,最多试 26 次就可以得到明文。

这里不光根据 offset 偏移进行加密,还加上了字符所在的下标进行混合加密。

输出的结果:

凯撒密码 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);

}