1. 將凱撒密碼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);
}
2. 請簡述窗函數法設計FIR數字濾波器的方法與步驟。
濾波器的理想頻率響應函數為Hd(ejω),則其對應的單位脈沖響應為hd(n)=窗函數設計法的基本原理是用有限長單位脈沖響應序列h(n)逼hd(n)。由於hd(n)往往是無限長序列,且是非因果的,所以用窗函數。w(n)將hd(n)截斷,並進行加權處理:
h(n)=hd(n)w(n)h(n)就作為實際設計的FIR數字濾波器的單位脈沖響應序列,其頻率響應函數H(ejω)為H(ejω)=用窗函數法設計的濾波器性能取決於窗函數w(n)的類型及窗口長度N的取值。設計過程中,要根據對阻帶最小衰減和過渡帶寬度的要求選擇合適的窗函數類型和窗口長度N。
一般都選用Ⅰ型線性相位濾波器即濾波器階數M為偶數,程序如下:
wp=;ws=;Ap=1;As=100;
dev=[Rp Rs];
[M,wc,beta,ftype]=kaiserord(f,a,dev);
M=mod(M,2)+M;
plot(omega/pi,20*log10(abs(mag)));
運行程序可以得到濾波器的通阻帶衰減,畫出頻率響應,若同阻帶衰減不滿足要求還可以使用濾波器的優化,一般使用的等波紋FIR進行優化。
(2)kaiser窗函數c語言擴展閱讀:
濾波器與機箱之間的一段連線會產生兩種不良作用: 一個是機箱內部空間的電磁干擾會直接感應到這段線上,沿著電纜傳出機箱,藉助電纜輻射,使濾波器失效;另一個是外界干擾在被板上濾波器濾波之前,藉助這段線產生輻射,或直接與線路板上的電路發生耦合,造成敏感度問題;
濾波陣列板、濾波連接器等面板濾波器一般都直接安裝在屏蔽機箱的金屬面板上。由於直接安裝在金屬面板上,濾波器的輸入與輸出之間完全隔離,接地良好,電纜上的干擾在機箱埠上被濾除,因此濾波效果相當理想。
3. 用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(); //密文
}
4. 用窗函數設計低通濾波器時,出現Cannot find an exact (case-sensitive) match for 'Kaiser'.求解
b = fir1(34,0.48,'high', kaiser(35,.5));
值得注意的是,fir1第一個參數為長度,這里為34,所以kaiser用35
5. 簡述採用窗函數法設計FIR數字濾波器的設計步驟及主要公式。
將模擬頻率轉化為數字頻率,設取樣時間為T(要滿足抽樣定理)
Ωp=2π*fp*T Ωs=2π*fs*T
過渡帶寬度△Ω=Ωp-Ωs
阻帶衰減已經超過74db,要選用Kaiser窗了,Kaiser的參數可變,要根據公式確定濾波器的參數
一般都選用Ⅰ型線性相位濾波器即濾波器階數M為偶數,程序如下:
wp=;ws=;Ap=1;As=100;
Rp=1-10.^(-0.05*Ap);Rs=10.^(-0.05*As);
f=[fp fs];
a=[0 1];
dev=[Rp Rs];
[M,wc,beta,ftype]=kaiserord(f,a,dev);
M=mod(M,2)+M;
h=fir1(M,wc,ftype,kaiser(M+1,beta));
omega=linspace(0,pi,512);
mag=freqz(h,[1],omega);
plot(omega/pi,20*log10(abs(mag)));
grid;
omega1=linspace(0,wp,512);
h1=freqz(h,[1],omega1);
omega2=linspace(ws,pi,512);
h2=freqz(h,[1],omega2);
fprintf('Ap=%.4f\n',-20*log10(min(abs(h1))));
fprintf('As=%.4f\n',-20*log10(max(abs(h2))));
運行程序可以得到濾波器的通阻帶衰減,畫出頻率響應,若同阻帶衰減不滿足要求還可以使用濾波器的優化,一般使用的等波紋FIR進行優化
6. 凱撒密碼的問題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);
}
.
希望能夠幫助你 ^_^ 也希望能夠選為最佳答案!
7. 幫幫忙,能不能給我 基於C語言的FIR濾波器設計的程序代碼(包括CMD,C,ASM),謝謝了 真的很急!!!
#include"math.h"
void firwin(n,band,fln,fhn,wn,h)
int n,band,wn;
double fln,fhn,h[];
{int i,n2,mid;
double s,pi,wc1,wc2,beta,delay;
double window();
beta=0.0;
if(wn==7)
{printf("input beta parameter of Kaiser window(2<beta<10)\n");
scanf("%1f",&beta);
}
pi=4.0*atan(1.0);
if((n%2)==0)/*如果n是偶數*/
{n2=n/2+1;/*這行什麼意思*/
mid=1;
}
else
{n2=n/2;
mid=0;
}
delay=n/2.0;
wc1=2.0*pi*fln;
if(band>=3) wc2=2.0*pi*fhn;/*先判斷用戶輸入的數據,如果band參數大於3*/
switch(band)
{case 1:
{for(i=0;i<=n2;i++)
{s=i-delay;
h[i]=(sin(wc1*s)/(pi*s))*window(wn,n+1,i,beta);
h[n-i]=h[i];
}
if(mid==1) h[n/2]=wc1/pi;
break;
}
case 2:
{for(i=0;i<=n2;i++)
{s=i-delay;
h[i]=(sin(pi*s)-sin(wc1*s))/(pi*s);
h[i]=h[i]*window(wn,n+1,i,beta);
h[n-i]=h[i];
}
if(mid==1) h[n/2]=1.0-wc1/pi;
break;
}
case 3:
{for(i=0;i<n2;i++)
{s=i-delay;
h[i]=(sin(wc2*s)-sin(wc1*s))/(pi*s);
h[i]=h[i]*window(wn,n+1,i,beta);
h[n-i]=h[i];
}
if(mid==1)h[n/2]=(wc2-wc1)/pi;
break;
}
case 4:
{for(i=0;i<=n2;i++)
{s=i-delay;
h[i]=(sin(wc1*s)+sin(pi*s)-sin(wc2*s))/(pi*s);
h[i]=h[i]*window(wn,n+1,i,beta);
h[n-i]=h[i];
}
if(mid==1)h[n/2]=(wc1+pi-wc2)/pi;
break;
}
}
}
static double window(type,n,i,beta)
int i,n,type;
double beta;
{int k;
double pi,w;
double kaiser();
pi=4.0*atan(1.0);
w=1.0;
switch(type)
{case 1:
{w=1.0;
break;
}
case 2:
{k=(n-2)/10;
if(i<=k)
w=0.5*(1.0-cos(i*pi/(k+1)));
break;
}
case 3:
{w=1.0-fabs(1.0-2*i/(n-1.0));
break;
}
case 4:
{w=0.5*(1.0-cos(2*i*pi/(n-1)));
break;
}
case 5:
{w=0.54-0.46*cos(2*i*pi/(n-1));
break;
}
case 6:
{w=0.42-0.5*cos(2*i*pi/(n-1))+0.08*cos(4*i*pi/(n-1));
break;
}
case 7:
{w=kaiser(i,n,beta);
break;
}
}
return(w);
}
static double kaiser(i,n,beta)
int i,n;
double beta;
{
double a,w,a2,b1,b2,beta1;
double bessel0();
b1=bessel0(beta);
a=2.0*i/(double)(n-1)-1.0;
a2=a*a;
beta1=beta*sqrt(1.0-a2);
b2=bessel0(beta1);
w=b2/b1;
return(w);
}
static double bessel0(x)
double x;
{int i;
double d,y,d2,sum;
y=x/2.0;
d=1.0;
sum=1.0;
for(i=1;i<=25;i++)
{d=d*y/i;
d2=d*d;
sum=sum+d2;
if(d2<sum*(1.0e-8)) break;
}
return(sum);
}
這是窗函數法的,當然還有其他的比如切比雪夫,零相位濾波什麼的,我也在研究,不是很懂哈
8. 採用窗函數法(Kaiser窗)設計一個FIR數字低通濾波器,說我14行的 'ideal'沒有定義。
function hd = ideal_lp(wc,M)
% Ideal LowPass filter computation
% --------------------------------
% [hd] = ideal_lp(wc,M)
%hd= ideal impulse response between 0 to M-1
%wc= cutoff frequency in radians
% M= length of the ideal filter
%
alpha = (M-1)/2; n = 0:1:(M-1);
m=n- alpha; fc = wc/pi; hd = fc*sinc(fc*m);
end
註:此處的wc=(Wst-Wp)/2
9. 用matlab處理語音信號,用到kaiser窗函數 kaiser(N,BTA),其中的N和BTA是什麼意思
把datareport 的datasource設置成和datagrid一樣的adodc就行 按鈕命令 set datareport1.datasource=adodc1
是在工程下面,和部件一樣是工程的子命令。\r\n而且datareport不一定非要data environment的可以脫離數據環境單獨使用的\r\n具體方法是把一個recordset對象作為數據源 \r\n2新建工程\r\n\r\n選數據工程
1、Data Report使用資料庫中的記錄生成報表。要使用它:
2、配置一個數據源,例如Microsoft數據環境,以訪問資料庫。
3、設定DataReport對象的DataSource屬性為數據源。
4、設定DataReport對象的DataMember屬性為數據成員。
5、右鍵單擊設計器,並單擊「檢索結構」。
6、向相應的節添加相應的控制項。
7、為每一個控制項設定DataMember和DataField屬性。
10. 利用Kaiser窗函數設計的FIR低通濾波器進行數字濾波
Wp=2.5;Rp=0.25dB
Ws=3;As=50dB;
下面是程序,把t取1:1:499,
wp=2.5;ws=3;As=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*pi;ws=0.3*pi;As=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的信號被濾掉。