新聞中心
如何用python編寫凱撒密碼 ?
凱撒密碼是對(duì)字母表整體進(jìn)行偏移的一種變換加密。因此,建立一個(gè)字母表,對(duì)明文中每個(gè)字母,在這個(gè)字母表中偏移固定的長(zhǎng)度即可得到對(duì)應(yīng)的密文字母。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了興山免費(fèi)建站歡迎大家使用!
最基本的實(shí)現(xiàn)如下:
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時(shí)不對(duì)c做upper處理.
同樣的,也可以在字母表中追加數(shù)字,各種符號(hào),空格等.
凱撒密碼實(shí)現(xiàn)英文短句的加解密
1. 將“We are students.”這個(gè)英文詞句用k=4的凱薩密碼翻譯成密碼
1. 愷撒密碼,
作為一種最為古老的對(duì)稱加密體制,他的基本思想是:
通過(guò)把字母移動(dòng)一定的位數(shù)來(lái)實(shí)現(xiàn)加密和解密。
例如,如果密匙是把明文字母的位數(shù)向后移動(dòng)三位,那么明文字母B就變成了密文的E,依次類推,X將變成A,Y變成B,Z變成C,由此可見,位數(shù)就是凱撒密碼加密和解密的密鑰。
如:ZHDUHVWXGHQWV(后移三位)
2. 凱撒密碼,
是計(jì)算機(jī)C語(yǔ)言編程實(shí)現(xiàn)加密和解密。挺復(fù)雜的。你可以研究一下哦。
2. 將凱撒密碼(K=7)的加密、解密過(guò)程用C語(yǔ)言編程實(shí)現(xiàn)
/*
聲明:MSVC++6.0環(huán)境測(cè)試通過(guò)
*/
#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的加密、解密過(guò)程用C語(yǔ)言編程實(shí)現(xiàn)
(2)kaiser加密算法 具體程序:#include #include char encrypt(char ch,int n)/*加密函數(shù),把字符向右循環(huán)移位n*/ { while(ch='A'ch='a'ch='z') { return ('a'+(ch-'a'+n)%26); } return ch; } void menu()/*菜單,1.加密,2.解密,3.暴力破解,密碼只能是數(shù)字*/ { 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);/*輸入解密密碼(可以為加密時(shí)候的密碼)*/ 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++)/*暴力破解過(guò)程,在察看信息正確后,可以按'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);/*顯示當(dāng)前破解所用密碼*/ printf("Press 'Q' to quit and other key to continue。
\n"); printf("==========================================================\n"); ch1=getch(); if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'時(shí)退出*/ { 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. 怎樣編寫程序:實(shí)現(xiàn)愷撒密碼加密單詞"julus"
用下面程序:新建個(gè)txt,放進(jìn)去任意單詞,設(shè)置#define N 5中的值,實(shí)現(xiàn)字母移位,達(dá)到加密目的。
本程序提供解密功能/************************************************************************//* 版權(quán)所有:信息工程學(xué)院 王明 使用時(shí)請(qǐng)注明出處??! *//* 算法:凱撒密碼體制 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請(qǐng)選擇需要進(jì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. 誰(shuí)有PYTHON編寫的凱撒密碼的加密和解密代碼
給你寫了一個(gè).
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
運(yùn)行結(jié)果:
Khoor zruog!
Hello world!
python凱撒密碼實(shí)現(xiàn)
#?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))??#?創(chuàng)建一個(gè)字典,?鍵為原字符串,?值為加密字符串
#?定義凱撒加密函數(shù),?輸入字符串,?輸出凱撒加密后字符串
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'))??#?結(jié)果為Gur?Mra?bs?Clguba
用Python語(yǔ)言從文件夾中提取文件進(jìn)行凱撒加密?
import string
def kaisa(s, k): #定義函數(shù) 接受一個(gè)字符串s 和 一個(gè)偏移量k
lower = string.ascii_lowercase #小寫字母
upper = string.ascii_uppercase #大寫字母
before = string.ascii_letters #無(wú)偏移的字母順序 小寫+大寫
after = lower[k:] + lower[:k] + upper[k:] + upper[:k] #偏移后的字母順序 還是小寫+大寫
#分別把小寫字母和大寫字母偏移后再加到一起
table = ''.maketrans(before, after) #創(chuàng)建映射表
return s.translate(table) #對(duì)s進(jìn)行偏移 即加密
s = input('請(qǐng)輸入一個(gè)字符串:')
k = int(input('請(qǐng)輸入一個(gè)整數(shù)密鑰:'))
print(kaisa(s, k))
調(diào)用此函數(shù)
當(dāng)前題目:python凱撒密碼函數(shù) Python中凱撒密碼
網(wǎng)頁(yè)URL:http://fisionsoft.com.cn/article/hpjeij.html