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/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

发表回复

登录后才能评论