代碼如下:
publicclassApp{
publicstaticvoidmain(String[]args){
Strings="HelloWorld!";
Stringresult="";
for(inti=0;i<s.length();i++){
result+=(char)(s.charAt(i)+12);
}
System.out.println("加密結果:"+result);
}
}
Ⅱ 用matlab實現凱撒密碼,仿射密碼,維吉尼亞密碼,素數判定和大數分解
functionY=caesarCode(plaintext,shift)
chars=['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'];
L=length(plaintext);
fori=1:L
forj=1:26
ifplaintext(i)==chars(j)
k=mod(j+shift,26);
ifk~=0
Y(i)=chars(k);
else
Y(i)=chars(26);
end
end
end
end
凱撒密碼,來自Canhui WANG
Ⅲ 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;
}
聽說回答的夠長才能夠自動採納
Ⅳ 將凱撒密碼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);
}
Ⅳ 凱撒密碼java編程實現圖形界面化代碼
class Caesar: def __init__(self): a = list(' ,.-!\'"') b = a[3:] + a[:3] self.emap = dict(zip(a,b)) self.dmap = dict(zip(b,a)) def encode(self, text): tmp = [ (x in self.emap and self.emap[x] or x) for x in text ] return ''.join(tmp) def decode(self, text): tmp = [ (x in self.dmap and self.dmap[x] or x) for x in text ] return ''.join(tmp)
Ⅵ Python編程-翻譯密碼
區分大小寫的凱撒密碼。
在凱撒密碼的基礎上針對大寫與小字字元區分處理即可:
解密只需要將7換成19(因為26-7=19),或者使用-7也可以:
print(caesarcipher(caesarcipher('Student!', 7),19))