A. 凱撒加密法,求代碼
不好意思! 這個 真的 不擅長! 幫不了您!
B. C語言編寫凱撒加密(簡單版的),網上搜到的那個大哥200多行代碼用不著……
#include<ctype.h>
int jiami(char str[],int len)
{
for(int i=0;i<len;i++)
{
if(!isalpha(str[i]))// 判斷是否為字元
{
str[i]=str[i]+3;
if(str[i]>'Z'&&str[i]<'a')str[i]=str[i]-'Z'+'A'-1;//控制X,Y,Z回到A,B,C
if(str[i]>'z')str[i]=str[i]-'z'+'a'-1;//控制x,y,z回到a,b,c
}
}
return 1;
}
這是我寫的,已經可以用了,你試試
C. 用C語言實現任意字元串的加密,其中,字母用凱撒加密方法加密,非字母不變
我盡量用注釋闡述了思路,希望可以幫到你!!
#include<stdio.h>
#include<string.h>
#define N 80 //可加密字元串最大長度
char plaintext[N]={0}; //明文,輸入時輸入字元,參與運算時強制轉換成整數
int ciphertext[N]={0}; //密文,保存成整數,輸出時強制轉換成字元
int k; //後(右)移位數,相當於密鑰
void getPlainText() //獲得明文字元串
{
printf("請輸入明文:");
scanf("%s",plaintext);
printf("\n");
}
void getLength() //獲取後(右)移位數(密鑰)
{
printf("請輸入後移的位數:");
scanf("%d",&k);
k%=26; //因為字母只有26個,所以超過26相當於重復
}
void Caesar_cipher() //凱撒加密,本程序採用的是字母循環後(右)移
{
unsigned int i;
for(i=0;i<strlen(plaintext);i++)
{
//兩個bool類型的變數是為了判斷字元是否是字母(包括大寫和小寫)
bool flag1=plaintext[i]>='a'&&plaintext[i]<='z';
bool flag2=plaintext[i]>='A'&&plaintext[i]<='Z';
if(flag1||flag2){ //如果是字母,加密
ciphertext[i]=(int)plaintext[i]+k; //字母在字母表中後(右)移K位
if(ciphertext[i]>(int)'z'){ //保證是循環後(右)移
ciphertext[i]-=26;
}
}
else //非字母字元,不做處理,原樣保存
ciphertext[i]=(int)plaintext[i];
}
}
void printCipherText() //輸出加密後的密文
{
unsigned int i;
printf("\n加密後的密文是:");
for(i=0;i<strlen(plaintext);i++) //把參與計算後是整數強制轉換成對應的字元
printf("%c",(char)ciphertext[i]);
printf("\n");
}
void main()
{
getPlainText(); //明文
getLength(); //後(右)移位數
Caesar_cipher(); //凱撒加密
printCipherText(); //密文
}
D. 什麼是凱撒加密法
簡單的說,就是位移加密。
比如你的密碼是ABCDE
然後設置凱撒密碼的偏移量為3的話
那加密之後的密碼就是DEFGH
E. 求凱撒加密法(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);
}
F. 凱撒加密法
凱撒加密法的替換方法是通過排列明文和密文字母表,密文字母表示通過將明文字母表向左或向右移動一個固定數目的位置。例如,當偏移量是左移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}
G. 3、 凱撒加密演算法加解密程序:
明文以實現加密,輸出密文。
H. C++編程 凱撒加密
#include<iostream>
#include<string>
usingnamespacestd;
voidencrypt(string&s)//加密
{
inti=s.length();
for(intj=0;j<i;++j)
{
if('A'<=s[j]&&s[j]<='Z')
{
if('A'<=s[j]&&s[j]<='W')
s[j]+=3;
else
s[j]-=23;
//cout<<s[j];
s[j]+=32;
//cout<<s[j]<<endl;
}
elseif('a'<=s[j]&&s[j]<='z')
{
if('a'<=s[j]&&s[j]<='w')
s[j]+=3;
else
s[j]-=23;
//cout<<s[j];
s[j]-=32;
//cout<<s[j]<<endl;
}
}
}
intmain()//測試
{
stringxy;
cout<<"輸入字元串"<<endl;
cin>>xy;
encrypt(xy);
cout<<xy<<endl;
return0;
}
聽說回答的夠長才能夠自動採納
I. 用python2.7.10編寫凱撒密碼加密和解密程序
s=raw_input('[開始加密]pleaseinputyourstr:')
s=list(s)
n=0
forswins:
s[n]=chr(ord(sw)+3)
n=n+1
sout=''
forsw2ins:
sout=sout+sw2
print'[加密結果]:',sout
解密的類似,主要用到ord、chr函數。
J. 將凱撒密碼X的加密、解密過程用C語言編程實現
1、在密碼學中,愷撒密碼(或稱愷撒加密、愷撒變換、變換加密)是一種最簡單且最廣為人知的加密技術。它是一種替換加密的技術,明文中的所有字母都在字母表上向後(或向前)按照一個固定數目進行偏移後被替換成密文。例如,當偏移量是3的時候,所有的字母A將被替換成D,B變成E,以此類推。這個加密方法是以愷撒的名字命名的,當年愷撒曾用此方法與其將軍們進行聯系。愷撒密碼通常被作為其他更復雜的加密方法中的一個步驟,例如維吉尼爾密碼。愷撒密碼還在現代的ROT13系統中被應用。但是和所有的利用字母表進行替換的加密技術一樣,愷撒密碼非常容易被破解,而且在實際應用中也無法保證通信安全。例子愷撒密碼的替換方法是通過排列明文和密文字母表,密文字母表示通過將明文字母表向左或向右移動一個固定數目的位置。
2、kaiser加密演算法具體程序:
#include<stdio.h>
#include<conio.h>
charencrypt(charch,intn)/*加密函數,把字元向右循環移位n*/
{
while(ch>='A'&&ch<='Z')
{
return('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
return('a'+(ch-'a'+n)%26);
}
returnch;
}
voidmenu()/*菜單,1.加密,2.解密,3.暴力破解,密碼只能是數字*/
{
clrscr();
printf(" =========================================================");
printf(" 1.Encryptthefile");
printf(" 2.Decryptthefile");
printf(" 3.Forcedecryptfile");
printf(" 4.Quit ");
printf("========================================================= ");
printf("Pleaseselectaitem:");
return;
}
main()
{
inti,n;
charch0,ch1;
FILE*in,*out;
charinfile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*輸入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*輸入加密密碼*/
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*輸入加密後文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Encryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*輸入解密密碼(可以為加密時候的密碼)*/
n=26-n;
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*輸入解密後文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Decryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*輸入解密後文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
for(i=1;i<=25;i++)/*暴力破解過程,在察看信息正確後,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("========================================================== ");
printf("Theoutfileis: ");
printf("========================================================== ");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf(" ======================================================== ");
printf("Thecurrentkeyis:%d ",i);/*顯示當前破解所用密碼*/
printf("Press'Q'toquitandotherkeytocontinue...... ");
printf("========================================================== ");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'時退出*/
{
clrscr();
printf(" GoodBye! ");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf(" Forcedecryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf(" GoodBye! ");
sleep(3);
}