當前位置:首頁 » 編程語言 » c語言凱撒加密

c語言凱撒加密

發布時間: 2022-06-13 07:56:25

A. 凱撒密碼,c語言,求救!

#include <stdio.h>
#include <string.h>

int main()
{
int i = 0;
int len = 0;
char ch;
char buf[256] = {0};
char nor[26] = {'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'};
char enc[26] = {'s','u','w','y','a','c','e','g','i','k','m','o','q','r','t','v','x','z','b','d','f','h','j','l','n','p'};

printf("Encode or Decode: ");
scanf("%c",&ch);
printf("please input your string: ");
fflush(stdin);
gets(buf);
len = strlen(buf);

switch (ch)
{
case 'e':
case 'E':
for (i=0;i<len;i++)
{
buf[i] = enc[buf[i] - 'a'];
}
break;
case 'd':
case 'D':
for (i=0;i<len;i++)
{
buf[i] = nor[i];
}
break;
default:
printf("wrong input!\n");
}

printf("<%s>\n",buf);

return 0;
}

B. 用C語言實現凱撒密碼加密解密,急!

忘了說解密了,解密過程完全可以使用上面的代碼,只要把key的值改成26-key就行了。比如,加密的密鑰是3,那麼解密密鑰就是23,這樣就可以使用同一段代碼。至於為什麼+23和-3的效果一樣,還請翻閱數論有關剩餘類環的知識。

C. C語言的凱撒加密

/*
和樓上的相比,或許 看上去很煩
ch[i] +=5;
if (ch[i] > 'Z')
{
ch[i] -= 26;
}

可以改成和 樓上的 方法
等價於 ch[i] = 'A' + (ch[i] - 'A' + 5) % 26;

*/

# include <stdio.h>
# include <stdlib.h> //用到了system(); 不寫 ,可以用 getchar();

#define strwidth 117 //定義長度

int main(void)
{

char ch[strwidth];
int i ;

printf("請輸入密碼:");
gets(ch); //輸入數據,用gets(); 保留了空格

for (i = 0; i < strwidth ;i++ )
{
if (ch[i] >= 'a' && ch[i] <= 'z' ) //判斷是否小寫字母
{
ch[i] +=5;
if (ch[i] > 'z') //不解釋,我想這樣,理解可能會方便點吧
{
ch[i] -= 26;
}
}
else if ( ch[i] >= 'A' && ch[i] <= 'Z') //判斷是否大寫字母
{
ch[i] +=5;
if (ch[i] > 'Z')
{
ch[i] -= 26;
}
}
}

printf("加密後為:%s\n" , ch); //輸出數據

system("pause");
return 0;
}

/*

或者 這樣

*/

# include <stdio.h>
# include <stdlib.h> //用到了system(); 不寫 ,可以用 getchar();

#define strwidth 117 //定義長度

int main(void)
{

char ch[strwidth];
int i ;

printf("請輸入密碼:");
gets(ch); //輸入數據,用gets(); 保留了空格

for (i = 0; i < strwidth ;i++ )
{
if (ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z' ) //判斷是否是字母
{
ch[i] +=5;

if ( ch[i]>'Z' && ch[i] <= 'Z' + 5 || ch[i] > 'z' )
{
ch[i] -= 26;
}
}
}

printf("加密後為:%s\n" , ch); //輸出數據

system("pause");
return 0;
}

D. 請問一下這道C語言編程題怎麼做

按照題目要求編寫的用凱撒密碼加密的C語言程序如下

#include<stdio.h>

int main(){

char s[80];

int offset,i;

fgets(s,80,stdin);

scanf("%d",&offset);

for(i=0;s[i]!='';i++){

if('A'<=s[i] && s[i]<='Z')

if(offset>=0)

s[i]='A'+(s[i]-'A'+offset)%26;

else

s[i]='A'+(s[i]-'A'+26+offset%26)%26;

else if('a'<=s[i] && s[i]<='z')

if(offset>=0)

s[i]='a'+(s[i]-'a'+offset)%26;

else

s[i]='a'+(s[i]-'a'+26+offset%26)%26;

}

printf("%s ", s);

return 0;

}

E. c語言里的凱撒密碼

#include<stdio.h>
#include<string.h>
intmain()
{
inti;intnumber;
chara[100];
scanf("%s",a);
number=strlen(a);
for(i=0;i<number;i++){
a[i]=a[i]+4;
}
for(i=0;i<number;i++){
printf("%c",a[i]);
}

return0;
}

F. 求凱撒加密法(C語言)

#include<stdio.h>
#include<conio.h> char encrypt(char ch,int n)/*加密函數,把字元向右循環移位n*/
{
while(ch>=A&&ch<=Z)
{
return (A+(ch-A+n)%26);
}
while(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;
}void logo()/*顯示版權信息*/
{
printf("\nZhensoft Encryption [Version:1.0.0]");
printf("\nCopyright (C) 2004 Zhensoft Corp.\n");
printf("\n http://www.zhensoft.com\n");
return;
}
main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();logo();
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();
logo();
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();
logo();
printf("\nGood Bye!\n");
sleep(3);
}

G. 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);
}

H. 凱撒密碼的演算法c語言的怎麼實現啊

凱撒密碼是一種非常古老的加密方法,相傳當年凱撒大地行軍打仗時為了保證自己的命令不被敵軍知道,就使用這種特殊的方法進行通信,以確保信息傳遞的安全。他的原理很簡單,說到底就是字母於字母之間的替換。下面讓我們看一個簡單的例子:「」用凱撒密碼法加密後字元串變為「edlgx」,它的原理是什麼呢?把「」中的每一個字母按字母表順序向後移3位,所得的結果就是剛才我們所看到的密文。

#include <stdio.h>
main()
{
char M[100];
char C[100];
int K=3,i;
printf("請輸入明文M(注意不要輸入空白串)\n");
gets(M);

for(i=0;M[i]!='\0';i++)
C[i]=(M[i]-'a'+K)%26+'a';
C[i]='\0';

printf("結果是:\n%s\n",C);
}

I. 用C語言編程愷撒密碼加密解密程序

#include<stdio.h>

#defineisletter(c)(((c)>='a'&&(c)<='z')||((c)>='A'&&(c)<='Z'))

voidEnc(constchar*str,char*out,intkey)
{
inti=0;
while(str[i])
{
if(isletter(str[i]))
{
out[i]=str[i]+key;
if(!isletter(out[i]))
out[i]-=26;
}
else
out[i]=str[i];
i++;
}
out[i]=0;
}
voidDenc(constchar*str,char*out,intkey)
{
inti=0;
while(str[i])
{
if(isletter(str[i]))
{
out[i]=str[i]-key;
if(!isletter(out[i]))
out[i]+=26;
}
else
out[i]=str[i];
i++;
}
out[i]=0;
}

intmain()
{
charout[100],out2[100];
Enc("",out,3);
printf("%s ",out);
Denc(out,out2,3);
printf("%s ",out2);
}

J. 關於C語言的凱撒加密

你需要添加路徑: ../file.txt;
因為你的exe文件在debug文件夾下,在該文件夾下沒有你輸入的哪個文件,程序中是只讀的形式打開文件的。所以,會提示存出錯。

兩種解決辦法:
第一個:將exe拷貝到加密文件的路徑下
第二個:將文件烤到exe文件路徑下。
根本的解決辦法就是: 給定文件的相對路徑或者絕對路徑,絕對不會出現問題的。

熱點內容
綦江dns伺服器地址 發布:2024-05-05 15:04:11 瀏覽:555
山東省日照市監控伺服器地址 發布:2024-05-05 15:03:59 瀏覽:341
java提升教程 發布:2024-05-05 15:00:51 瀏覽:144
驅動編譯龍芯 發布:2024-05-05 14:41:31 瀏覽:957
起什麼密碼 發布:2024-05-05 14:29:48 瀏覽:562
安卓怎麼設置鎖屏時不顯示微信通話 發布:2024-05-05 14:21:59 瀏覽:223
qq怎麼訪問照片流 發布:2024-05-05 14:20:38 瀏覽:17
java實現的加密演算法 發布:2024-05-05 14:20:33 瀏覽:183
基礎it編程的書籍 發布:2024-05-05 14:19:47 瀏覽:442
網易夢之國伺服器ip 發布:2024-05-05 14:06:11 瀏覽:34