#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
struct hill
{
int i,j
int s[10][10]
}a[3],b
struct hill mul(struct hill,struct hill)
int det(struct hill,int,int)
struct hill inv(struct hill)
void code()
void uncode()
FILE *word,*num
void main()
{
int i=0,j=0,k=0
word=fopen("word.txt","r")
num=fopen("num.txt","r")
if(word==NULL||num==NULL)
{
printf("Error!\n")
exit(0)
}
fclose(word)
fclose(num)
while(!i)
{
fflush(stdin)
printf("输入密码\n")
for(i=0i<10i++)
{
for(j=0j<10j++)
{
a[k].s[i][j]=getchar()
if(a[k].s[i][j]=='\n') break
}
if(a[k].s[i][0]=='\n') break
}
for(a[k].j=0a[k].s[0][a[k].j]!='\n'a[k].j++)
for(a[k].i=0a[k].s[a[k].i][0]!='\n'a[k].i++) a[k].s[a[k].i][a[k].j]=0
for(i=0i<a[k].ii++) for(j=0j<a[k].jj++) a[k].s[i][j]=a[k].s[i][j]-'a'
i=0
if(a[k].i==a[k].j)
{
if(det(a[k],a[k].i,a[k].j)%2!=0&&det(a[k],a[k].i,a[k].j)%13!=0)
{
printf("是(0)否(1)继续输入?:")
scanf("%d",&i)
k++
}
else printf("密码不合格!\n")
}
else printf("密码不合格!\n")
}
while(i)
{
printf("1:加密 2:解密\n")
scanf("%d",&i)
switch(i)
{
case 1: code()break
case 2: uncode()break
default: i=0
}
}
}
struct hill mul(struct hill a,struct hill b)
{
int i=0,j=0,k=0
struct hill ans
ans.i=a.i
ans.j=b.j
for(i=0i<ans.ii++)
{
for(j=0j<ans.jj++)
{
ans.s[i][j]=0
for(k=0k<a.jk++)
ans.s[i][j]=ans.s[i][j]+a.s[i][k]*b.s[k][j]
}
}
for(i=0i<ans.ii++)
{
for(j=0j<ans.jj++) ans.s[i][j]=(ans.s[i][j]%26+26)%26+'a'
ans.s[i][j]='\0'
}
return(ans)
}
struct hill inv(struct hill a)
{
char c
int i=0,j=0,k=0
struct hill ans
ans.i=a.i
ans.j=a.j
switch(det(a,a.i,a.j)%26)
{
case 1: k=1break
case 3: k=9break
case 5: k=21break
case 7: k=15break
case 9: k=3break
case 11: k=19break
case 15: k=7break
case 17: k=23break
case 19: k=11break
case 21: k=5break
case 23: k=17break
case 25: k=25break
}
for(i=0i<ans.ii++)
{
for(j=0j<ans.jj++)
ans.s[i][j]=(((int)pow(-1,i+j)*det(a,i,j)*k)%26+26)%26
}
for(i=0i<ans.ii++) ans.s[i][ans.j]='\0'
for(i=0i<ans.ii++)
{
for(j=0j<ij++)
{
c=ans.s[i][j]
ans.s[i][j]=ans.s[j][i]
ans.s[j][i]=c
}
}
return(ans)
}
int det(struct hill a,int m,int n)
{
int i=0,j=0,x=0
if(m!=a.i||n!=a.j)
{
for(i=0i<a.ii++) for(j=nj<a.jj++) a.s[i][j]=a.s[i][j+1]
for(i=mi<a.ii++) for(j=0j<a.jj++) a.s[i][j]=a.s[i+1][j]
a.i--
a.j--
}
if(a.i==2&&a.j==2) return(a.s[0][0]*a.s[1][1]-a.s[0][1]*a.s[1][0])
for(i=0i<a.ii++) x+=a.s[0][i]*det(a,0,i)*(int)pow(-1,i)
return(x)
}
void code()
{
int i=0,j=0,space[10],k=0,n=0
struct hill ans
char c=0
word=fopen("word.txt","r")
c=fgetc(word)
while(c!=EOF)
{
if(c>='a'&&c<='z') n++
c=fgetc(word)
}
fclose(word)
word=fopen("word.txt","r")
num=fopen("num.txt","w")
while(n)
{
printf("剩余%d: ",n)
scanf("%d",&j)
n=n-j
for(k=0a[k].i!=jj++)
b.i=a[k].i
b.j=1
for(j=0j<10j++) space[j]=-1
j=0
for(i=0i<b.ii++)
{
b.s[i][0]=fgetc(word)
if(b.s[i][0]<'a'||b.s[i][0]>'z')
{
space[j]=i
j++
b.s[i][0]=fgetc(word)
}
}
for(i=0i<b.ii++)
{
b.s[i][0]-='a'
b.s[i][1]='\0'
}
ans=mul(a[k],b)
for(i=0i<ans.ii++)
{
for(j=0j<10j++) if(i==space[j]) break
if(j!=10) fprintf(num," %c",ans.s[i][0])
else fprintf(num,"%c",ans.s[i][0])
}
}
fclose(word)
fclose(num)
}
void uncode()
{
int k=0,n=0
int i=0,j=0,space[10]
struct hill ans
char c=0
word=fopen("num.txt","r")
c=fgetc(word)
while(c!=EOF)
{
if(c>='a'&&c<='z') n++
c=fgetc(word)
}
fclose(word)
word=fopen("word.txt","w")
num=fopen("num.txt","r")
b.i=a[k].i
b.j=1
while(n)
{
printf("剩余%d: ",n)
scanf("%d",&j)
n=n-j
for(k=0a[k].i!=jj++)
b.i=a[k].i
b.j=1
for(j=0j<10j++) space[j]=-1
j=0
for(i=0i<b.ii++)
{
b.s[i][0]=fgetc(num)
if(b.s[i][0]<'a'||b.s[i][0]>'z')
{
space[j]=i
j++
b.s[i][0]=fgetc(num)
}
}
for(i=0i<b.ii++)
{
b.s[i][0]-='a'
b.s[i][1]='\0'
}
ans=mul(inv(a[k]),b)
for(i=0i<ans.ii++)
{
for(j=0j<10j++) if(i==space[j]) break
if(j!=10) fprintf(word," %c",ans.s[i][0])
else fprintf(word,"%c",ans.s[i][0])
}
}
fclose(word)
fclose(num)
}
给:维吉尼亚密码的C语言源代码
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
void crypt(char m[],char k[],char r[])
{
int i,j,s=0
j=strlen(k)
for(i=0m[i]i++)
m[i]=tolower(m[i])
for(i=0m[i]i++)
if(isalpha(m[i]))
{
r[i]=(m[i]-'a'+k[s%j]-'a')%26+'a'
s++/* s用来跳过明文中的空格字符 */
}
else
r[i]=m[i]
r[i]=0/* 密文字符串结束符 */
for(i=0r[i]i++)
r[i]=toupper(r[i])
}
void decrypt(char c[],char k[],char m[])
{
int i,j,s=0
j=strlen(k)
for(i=0c[i]i++)
c[i]=tolower(c[i])
for(i=0c[i]i++)
if(isalpha(c[i]))
{
m[i]=(c[i]-k[s%j]+26)%26+'a'
s++
}
else
m[i]=c[i]
m[i]=0
}
void main()
{
char m[]="welcome to my blog.i am bugeyes."//我这里是赋值了一个固定的字符串为明文序列,你也可以做成用户输入的
char k[]="bugeyeswuyan"//我这里是赋值了一个固定的字符串为密钥序列,你也可以做成用户输入的
char c[80]
char d[80]
system("cls")
crypt(m,k,c)
decrypt(c,k,d)
puts(m)
puts(k)
puts(c)
puts(d)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)