當前位置:首頁 » 編程語言 » 凱撒c語言密碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

凱撒c語言密碼

發布時間: 2022-06-16 17:59:16

㈠ 凱撒密碼的問題c語言

(2)kaiser加密演算法
具體程序:
#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;
}

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);
}
.
希望能夠幫助你 ^_^ 也希望能夠選為最佳答案!

㈡ 凱撒密碼,要求C語言編寫,求救!

寫的一般般,希望對LZ有所幫助
#include <stdio.h>
#include <string.h>
int main()
{
char str[201];//存放字元
char tmp[11];//臨時變數
int i;//循環變數
int len;//存放消息長度
scanf("%s",tmp);//這里輸入START,開始
getchar();//接收回車
while(strcmp(tmp,"ENDOFINPUT"))
{
gets(str);//由於輸入中有空格,所以用gets輸入
getchar();//接收回車
len = strlen(str);
for(i=0;i<len;i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
str[i] -= 5 ;
if(str[i] < 65)
{
str[i] +=26;
}
}
}
scanf("%s",tmp);//這里輸入END,結束
printf("%s\n",str);//處理完就直接輸出結果
scanf("%s",tmp);//輸入START表示繼續,輸入ENDOFINPUT則表示最後一個數據集
getchar();//接收回車
}
return 0;

}

㈢ 關於C語言的凱撒加密

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

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

㈣ 凱撒密碼 C語言

#include<stdio.h>
#include<string.h>
void main ()
{
char str[100];
char str1[100];
printf("輸入字元串:");
scanf("%s",&str);
int len;
len=strlen(str);
for(int i=0;i<len;i++)
{
str1[i]=(str[i]-97+3)%26+97;
}
str1[len]='\0';
printf ("密文為:%s\n",str1);
}

㈤ 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;
}

㈥ 凱撒密碼,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;
}

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

凱撒密碼就是簡單的加上一個數,'a'+3='d';'z'+3='c' 假設原文全是小寫字母,那麼 char plain[N]={...}; //明文 char cipher[N]={};//密文 int key=3; int i=0,temp; for(i=0;i<N;i++) {if(plain[i]!=' ') {temp=plain[i]+key-'a'; temp=temp%26; cipher[i]=temp+'a'; } else cipher[i]=plain[i]; } 這樣就完成了加密,密文數組裡面就是對原文加密後的密文,key是密鑰。

㈧ 求凱撒加密法(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);
}

㈨ 凱撒密碼的演算法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);
}

㈩ 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);
}