
1. 恺撒密码,
作为一种最为古老的对称加密体制,他的基本思想是:
通过把字母移动一定的位数来实现加密和解密。
例如,如果密匙是把明文字母的位数向后移动三位,那么明文字母B就变成了密文的E,依次类推,X将变成A,Y变成B,Z变成C,由此可见,位数就是凯撒密码加密和解密的密钥。
如:ZHDUHVWXGHQWV(后移三位)
2. 凯撒密码,
是计算机C语言编程实现加密和解密。挺复杂的。你可以研究一下哦。
2.将凯撒密码(K=7)的加密、解密过程用C语言编程实现/*
声明:MSVC++6.0环境测试通过
*/
#include<stdio.h>
#include<ctype.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,nchar ch0,ch1FILE *in,*outchar 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-nprintf("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=1i<=25i++)/*暴力破解过程,在察看信息正确后,可以按'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_file2char cfp_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 nprintf("输入你要 *** 作的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!
程序:
wlp = 0.35*pi
whp = 0.65*pi
wc = [wlp/pi,whp/pi]
N = ceil(8/0.15)
n=0:N-1
window= hanning(N)
[h1,w]=freqz(window,1)
subplot(411)
stem(window)%画窗函数
axis([0 60 0 1.2])
grid
xlabel('n')
title('Hanning窗函数')
subplot(412)
plot(w/pi,20*log(abs(h1)/abs(h1(1)))) %画Hanning窗频谱图
axis([0 1 -350 0])
grid
xlabel('w/pi')
ylabel('幅度(dB)')
title('Hanning窗函数的频谱')
hn = fir1(N-1,wc, hanning (N))%基于窗函数的 FIR 滤波器
[h2,w]=freqz(hn,1,512) %freqz是用离散傅里叶变换的标准公示计算的 fft使用快速傅里
subplot(413)
stem(n,hn)
axis([0 60 -0.25 0.25])
grid
xlabel('n')
ylabel('h(n)')
title('Hanning窗函数的单位脉冲响应')
subplot(414)
plot(w/pi,20*log(abs(h2)/abs(h2(1))))
grid
xlabel('w/pi')
ylabel('幅度(dB)')
title('滤波器相对幅度响应')
结果:
Wp=2.5Rp=0.25dBWs=3As=50dB
下面是程序,把t取1:1:499,
wp=2.5ws=3As=50
tr_width=ws-wp
M=ceil((As-7.95)/(14.36*tr_width/(2*pi))+1)+1
n=[0:1:M-1]
beta=0.1102*(As-8.7)
wc=(ws+wp)/2
hd=ideal_lp(wc,M)
w_kai=(kaiser(M,beta))'
h=hd.*w_kai
t=0:1:499
x=sin(2*t)+sin(5*t)+sin(20*t)
subplot(2,1,1)plot(t,x)
title('x=sin(2*n)+sin(5*n)+sin(20*n)')
xlabel('时间/s')ylabel('幅度')
y=filter(h,[1],x)
subplot(2,1,2)plot(t,y)
title('y=filter(h,[1],x)')
需要说明的是,你这个题给出的信号和所取的点数不太匹配,我做了频谱分析了,性能不算好,分析原因是因为正弦信号在抽取时,一定要做整数倍周期的抽取,你抽取了500点,而500点不是上述三个正弦信号的任意一个周期的整数倍,这样会产生频率泄露的问题。
为了给你一个直观的看法,我重新设计了你这个题目,你可以看一下效果。我选择了一个信号:
x=sin(0.1*pi*n)+sin(0.3*pi*n)
做了整数周期的抽取。
wp=0.2*piws=0.3*piAs=50
tr_width=ws-wp
M=ceil((As-7.95)/(14.36*tr_width/(2*pi))+1)+1
n=[0:1:M-1]
beta=0.1102*(As-8.7)
wc=(ws+wp)/2
hd=ideal_lp(wc,M)
w_kai=(kaiser(M,beta))'
h=hd.*w_kai
n=0:199
x=sin(0.1*pi*n)+sin(0.3*pi*n)
y=filter(h,[1],x)
subplot(2,2,1)stem(n,x)
title('x=sin(0.1*pi*n)+sin(0.3*pi*n)')
subplot(2,2,2)plot(n,y)
>>
title('y=filter(h,[1],x)')
x1=fft(x,200)
x11=abs(x1)
title('y=filter(h,[1],x)')
subplot(2,2,3)stem(n,x11)
title('the
frequency
of
x')
y1=fft(y,200)
y11=abs(y1)
subplot(2,2,4)stem(n,y11)
title('the
frequency
of
y')
你在MATLAB中运行可以清晰的看到0.3pi的信号被滤掉。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)