import java.util.Scanner;
public class Caeser {
private String table; // 定义密钥字母表
private int key; // 定义密钥key
public Caeser(String table, int key) {
// 根据不同的字母表和不同的密钥生成一个新的凯撒算法,达到通用的目的
super();
this.table = table;
this.key = key;
}
public String encrypt(String from) {
//凯撒加密算法,传入明文字符串,返回一个密文字符串
String to = "";
for (int i = 0; i from.length(); i++) {
to += table.charAt((table.indexOf(from.charAt(i))+key)%table.length());
}
return to;
}
public static void main(String[] args) {
Caeser caeser = new Caeser("abcdefghijklmnopqrstuvwxyz", 3);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要加密的字符串");
String str =scanner.nextLine(); //输入字符串 security
String result = caeser.encrypt(str); //调用加密方法进行加密
System.out.print(result); // 可得结果 vhfxulwb
}
}
不用类也是可以做的,不过看起来有些哆嗦,随便看一下吧,自己再改简单一点
using namespace std;
const int N=20;
void main()
{ int k;
void encipher(char c[],int b);
void decipher(char c[],int b);
char plaintext[N];
cout"Please enter the keywards k(0k26):"endl;
cink;
cout"Please enter the plaintext:"endl;
cinplaintext;
cout"the ciphertext is:";
encipher(plaintext,k);
cout"the plaintext is:";
decipher(plaintext,k);
}
void encipher(char c[],int b)
{
int i;
int a[N];
for(i=0;iN;i++)
a[i]=c[i]; //注:128~255是IBM-PC上专用的,ASCII代码中000-127是标准的,如果是z加20的就会超出128,故先赋给整型,然后再转换过来,
for(i=0;a[i]!='\0'iN;i++)
{
if((a[i]='A'a[i]='Z')||(a[i]='a'a[i]='z'))
{
a[i]=a[i]+b;
if((a[i]'Z'a[i]='Z'+b)||(a[i]'z'))
a[i]=a[i]-26;}
}
for(i=0;iN;i++)
c[i]=a[i];
for(i=0;iN;i++)
coutc[i];
coutendl;
}
void decipher(char c[],int b)
{
int i;
for(i=0;c[i]!='\0'iN;i++)
{
if((c[i]='A'c[i]='Z')||(c[i]='a'c[i]='z'))
{
c[i]=c[i]-b;
if((c[i]='a'-bc[i]'a')||c[i]'A')
c[i]=c[i]+26;}
}
for(i=0;iN;i++)
coutc[i];
}
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!
import java.*;
public class Practise {
public static void main(String[] args) {
String P = new String();// 明文
String K = new String();// 密钥
String C = new String();// 密文
short LR=-1;//间隔的方向,向左为-1,向右为1
P = "benrencainiaoyizhi";
K = "P";
C = "QTCGTCRPXCXPDNXOWX";
System.out.println("明文:"+P);
System.out.println("密钥:"+K);
System.out.println("密文:"+C+"\n");
CaesarCode caesar=new CaesarCode();
LR=1;
System.out.println("加密:"+caesar.encrypt(P, K, LR));
LR=-1;
System.out.println("解密:"+caesar.decrypt(K, C, LR).toLowerCase());
}
}
class CaesarCode {
private char alphabet[] = new char[26];//存储字母表
//加密
protected String encrypt(String P,String K,short LR)
{
int i=0,j=0,n=0;//n是间隔
String C=new String();//密文
P=P.toUpperCase();
P=getNewP(P);
K=K.toUpperCase();
n=getN(K);
//将明文转换成密文
for(i=0;iP.length();i++)
{
j=String.valueOf(alphabet).indexOf(P.charAt(i));//获取密文字母在字母表所在的下标
j=(j+n*LR+26)%26;//向左或向右移动n位
C+=(char)(j+65);
}
return C;
}
//解密
protected String decrypt(String K,String C,short LR)
{
int i=0,j=0,n=0;//n是间隔
String P=new String();//明文
K=K.toUpperCase();
C=C.replaceAll(" +"," ");
C=C.toUpperCase();
n=getN(K);
//将密文转换成明文
for(i=0;iC.length();i++)
{
j=String.valueOf(alphabet).indexOf(C.charAt(i));//获取密文字母在字母表所在的下标
j=(j+n*LR+26)%26;//向左或向右移动n位
P+=(char)(j+65);
}
return P;
}
//获取经过处理的明文
private String getNewP(String P)
{
int i=0;
char p[] = P.toCharArray();
for (i = 0; i P.length(); i++) {
if (p[i] 'A' || p[i] 'Z')// 将非字母换成空格
{
p[i] = ' ';
}
}
P = String.valueOf(p);
P = P.replaceAll(" +", "");// 将明文的所有空格去掉
return P;
}
//获取间隔
private int getN(String K)
{
int i=0,n=0;
//生成字母表
for(i=0;i26;i++)
{
alphabet[i]=(char)(i+65);//字母A在ASCII表中的值是065
}
if(isNum(K))
{
n=Integer.parseInt(K);
}
else
{
n=String.valueOf(alphabet).indexOf(K);//当K不是数字时适用
}
return n;
}
//判断密钥是否为数字
private boolean isNum(String K)
{
return K.matches("[0-9]+");//+表示1个或多个(如"3"或"225")
}
}