本文目錄一覽:
凱撒加密法
凱撒加密法的替換方法是通過排列明文和密文字母表,密文字母表示通過將明文字母表向左或向右移動一個固定數目的位置。例如,當偏移量是左移3的時候(解密時的密鑰就是3):
明文字母表:ABCDEFGHIJKLMNOPQRSTUVWXYZ
密文字母表:DEFGHIJKLMNOPQRSTUVWXYZABC
使用時,加密者查找明文字母表中需要加密的消息中的每一個字母所在位置,並且寫下密文字母表中對應的字母。需要解密的人則根據事先已知的密鑰反過來操作,得到原來的明文。例如:
明文:THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
密文:WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ
凱撒加密法的加密、解密方法還能夠通過同餘的數學方法進行計算。首先將字母用數字代替,A=0,B=1,…,Z=25。此時偏移量為n的加密方法即為:
En(x)=(x+n)mod26{\displaystyle E_{n}(x)=(x+n)\mod 26}
解密就是:
Dn(x)=(x−n)mod26{\displaystyle D_{n}(x)=(x-n)\mod 26}
凱撒密碼的算法用PHP語言的怎麼實現啊?
?php
echo “請輸入明文M(注意不要輸入空白串)\n”;
$m=trim(fgets(STDIN));
$c=”;
for($i=0;$istrlen($m);$i++)
$c.=chr((ord(substr($m,$i,1))-ord(‘a’)+3)%26+ord(‘a’));
echo “結果是:\n$c\n”;
?
運行示例如下:
E:\ygbphp a.php
請輸入明文M(注意不要輸入空白串)
123
結果是:
NOP
特別主意:
以上程序在命令提示符下運行,如果要在網頁上運行,那麼$m=trim(fgets(STDIN));應該修改為$m=$_GET[‘m’];相關原理不在本帖討論,假設你明白。
凱撒密碼實現英文短句的加解密
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!
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/127559.html