php實現凱撒加密演算法(php實現凱撒加密演算法是什麼)

本文目錄一覽:

凱撒加密法

 凱撒加密法的替換方法是通過排列明文和密文字母表,密文字母表示通過將明文字母表向左或向右移動一個固定數目的位置。例如,當偏移量是左移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-tw/n/127559.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
簡單一點的頭像簡單一點
上一篇 2024-10-03 23:16
下一篇 2024-10-03 23:16

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • PHP獲取301跳轉後的地址

    本文將為大家介紹如何使用PHP獲取301跳轉後的地址。301重定向是什麼呢?當我們訪問一個網頁A,但是它已經被遷移到了另一個地址B,此時若伺服器端做了301重定向,那麼你的瀏覽器在…

    編程 2025-04-27
  • PHP登錄頁面代碼實現

    本文將從多個方面詳細闡述如何使用PHP編寫一個簡單的登錄頁面。 1. PHP登錄頁面基本架構 在PHP登錄頁面中,需要包含HTML表單,用戶在表單中輸入賬號密碼等信息,提交表單後服…

    編程 2025-04-27
  • PHP與Python的比較

    本文將會對PHP與Python進行比較和對比分析,包括語法特性、優缺點等方面。幫助讀者更好地理解和使用這兩種語言。 一、語法特性 PHP語法特性: <?php // 簡單的P…

    編程 2025-04-27
  • PHP版本管理工具phpenv詳解

    在PHP項目開發過程中,我們可能需要用到不同版本的PHP環境來試驗不同的功能或避免不同版本的兼容性問題。或者我們需要在同一台伺服器上同時運行多個不同版本的PHP語言。但是每次手動安…

    編程 2025-04-24
  • PHP數組去重詳解

    一、array_unique函數 array_unique是php中常用的數組去重函數,它基於值來判斷元素是否重複,具體使用方法如下: $array = array(‘a’, ‘b…

    編程 2025-04-24
  • PHP導出Excel文件

    一、PHP導出Excel文件列寬調整 當我們使用PHP導出Excel文件時,有時需要調整單元格的列寬。可以使用PHPExcel類庫中的setWidth方法來設置單元格的列寬。下面是…

    編程 2025-04-24
  • php擴展庫初探

    一、什麼是php擴展庫? PHP擴展庫(PHP extension)是一些用C語言編寫的動態鏈接庫,用於擴展PHP的功能。PHP擴展庫使得PHP可以與各種資料庫系統相連、SMTP、…

    編程 2025-04-23

發表回復

登錄後才能評論