當前位置:首頁 » 編程軟體 » 凱撒密碼編譯

凱撒密碼編譯

發布時間: 2022-04-18 13:39:04

㈠ 如何用python編寫凱撒密碼

凱撒密碼是對字母表整體進行偏移的一種變換加密。因此,建立一個字母表,對明文中每個字母,在這個字母表中偏移固定的長度即可得到對應的密文字母。

最基本的實現如下:

defcaesarcipher(s:str,rot:int=3)->str:
_='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
encode=''
i=0
forcins:
try:
encode+=_[(_.index(c.upper())+rot)%len(_)]
except(Exception,)ase:
encode+=c
returnencode


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

如果要求解密後保持大小寫,那麼,字母表_還需要包含所有小寫字母並且index時不對c做upper處理.

同樣的,也可以在字母表中追加數字,各種符號,空格等.

㈡ 將凱撒密碼X的加密、解密過程用C語言編程實現

1、在密碼學中,愷撒密碼(或稱愷撒加密、愷撒變換、變換加密)是一種最簡單且最廣為人知的加密技術。它是一種替換加密的技術,明文中的所有字母都在字母表上向後(或向前)按照一個固定數目進行偏移後被替換成密文。例如,當偏移量是3的時候,所有的字母A將被替換成D,B變成E,以此類推。這個加密方法是以愷撒的名字命名的,當年愷撒曾用此方法與其將軍們進行聯系。愷撒密碼通常被作為其他更復雜的加密方法中的一個步驟,例如維吉尼爾密碼。愷撒密碼還在現代的ROT13系統中被應用。但是和所有的利用字母表進行替換的加密技術一樣,愷撒密碼非常容易被破解,而且在實際應用中也無法保證通信安全。例子愷撒密碼的替換方法是通過排列明文和密文字母表,密文字母表示通過將明文字母表向左或向右移動一個固定數目的位置。

2、kaiser加密演算法具體程序:

#include<stdio.h>
#include<conio.h>
charencrypt(charch,intn)/*加密函數,把字元向右循環移位n*/
{
while(ch>='A'&&ch<='Z')
{
return('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
return('a'+(ch-'a'+n)%26);
}
returnch;
}
voidmenu()/*菜單,1.加密,2.解密,3.暴力破解,密碼只能是數字*/
{
clrscr();
printf(" =========================================================");
printf(" 1.Encryptthefile");
printf(" 2.Decryptthefile");
printf(" 3.Forcedecryptfile");
printf(" 4.Quit ");
printf("========================================================= ");
printf("Pleaseselectaitem:");
return;
}
main()
{
inti,n;
charch0,ch1;
FILE*in,*out;
charinfile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*輸入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*輸入加密密碼*/
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*輸入加密後文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Encryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*輸入解密密碼(可以為加密時候的密碼)*/
n=26-n;
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*輸入解密後文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Decryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*輸入解密後文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
for(i=1;i<=25;i++)/*暴力破解過程,在察看信息正確後,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("========================================================== ");
printf("Theoutfileis: ");
printf("========================================================== ");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf(" ======================================================== ");
printf("Thecurrentkeyis:%d ",i);/*顯示當前破解所用密碼*/
printf("Press'Q'toquitandotherkeytocontinue...... ");
printf("========================================================== ");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'時退出*/
{
clrscr();
printf(" GoodBye! ");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf(" Forcedecryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf(" GoodBye! ");
sleep(3);
}

㈢ 凱撒密碼怎麼用C語言編,急救!!!附加解釋,謝謝

凱撒加密是最簡單的加密,就是 把字元移動n位, 例如 :移動1位時,a就用b表示,f用e表示。

#include<stdio.h>
int main(void)
{
char buf[] = "hello";
int i = 0;

printf("before: %s\n", buf);
while (buf[i])
buf[i++] += 1; // 移1位,a 變b
printf("after: %s\n", buf);
return 0;
}

㈣ 怎麼用Python編輯出此凱撒密碼的解密密碼

凱撒密碼的加密密鑰與解密密鑰是相反數,因此,k給相反數即可:
kaisa(kaisa(s, 3), -3)

㈤ C語言編程問題,凱撒密碼

#include<stdio.h>
chars[]="(BTBU)isakeystate-,Sciences,Engineering,Law,Economics,History,PhilosophyandManagement.";
voidfun(char*s,intn)
{
inti=0;
while(s[i]!=0)
{
if(s[i]<='z'&&s[i]>='a')
{
s[i]+=n;
if(s[i]>'z')s[i]-=24;
}
elseif(s[i]<='Z'&&s[i]>='A')
{
s[i]+=n;
if(s[i]>'Z')s[i]-=24;
}
i++;
}
}
voidmain()
{
fun(s,3);
puts(s);
}

㈥ 凱撒密碼用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語言編寫,求救!

寫的一般般,希望對LZ有所幫助
#include <stdio.h>
#include <string.h>
int main()
{
char str[201];//存放字元
char tmp[11];//臨時變數
int i;//循環變數
int len;//存放消息長度
scanf("%s",tmp);//這里輸入START,開始
getchar();//接收回車
while(strcmp(tmp,"ENDOFINPUT"))
{
gets(str);//由於輸入中有空格,所以用gets輸入
getchar();//接收回車
len = strlen(str);
for(i=0;i<len;i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
str[i] -= 5 ;
if(str[i] < 65)
{
str[i] +=26;
}
}
}
scanf("%s",tmp);//這里輸入END,結束
printf("%s\n",str);//處理完就直接輸出結果
scanf("%s",tmp);//輸入START表示繼續,輸入ENDOFINPUT則表示最後一個數據集
getchar();//接收回車
}
return 0;

}

熱點內容
sql網校 發布:2025-03-20 06:16:42 瀏覽:279
安卓手機圖標排列為什麼會混亂 發布:2025-03-20 06:16:05 瀏覽:760
手機pin初始密碼是多少 發布:2025-03-20 06:15:59 瀏覽:899
javaif常量變數 發布:2025-03-20 06:15:57 瀏覽:343
iis安裝sql 發布:2025-03-20 06:05:31 瀏覽:148
製作自解壓安裝 發布:2025-03-20 05:41:49 瀏覽:304
華為連接電視密碼是多少 發布:2025-03-20 05:31:11 瀏覽:493
演算法第五版 發布:2025-03-20 05:17:57 瀏覽:730
湖南台訪問 發布:2025-03-20 05:10:32 瀏覽:38
腳本和秒搶 發布:2025-03-20 05:06:29 瀏覽:592