一种playfair密码变种加密方法如下:首先选择一个密钥单词(称为pair)(字母不重复,且都为小写字母)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一种playfair密码变种加密方法如下:首先选择一个密钥单词(称为pair)(字母不重复,且都为小写字母)相关的知识,希望对你有一定的参考价值。
后与字母表中其他字母一起填入至一个5*5的方阵中,填入方法如下:
1.首先按行填入密钥串。
2.紧接其后,按字母序按行填入不在密钥串中的字母。
3.由于方阵中只有25个位置,最后剩下的那个字母则不需要变换。
如果密钥为youandme,则该方阵如下:
y o u a n
d m e b c
f g h i j
k l p q r
s t v w x
请问这个怎么做??????????????
#include<string.h>
int find(char *key,int n,char c);
char alph[26]='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';
void find_fang(char fang[5][5],char c1,int *i_1,int *j_1);
void main()
char key[25],yuanwen[50],miwen[50];
printf("\n请输入密钥(1~25):");
gets(key);
printf("\n请输入原文(1~50):");
gets(yuanwen);
char fang[5][5];
int i,j,k=0,len,t=0;
len=strlen(key);
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(key[k]!='\0')
fang[i][j]=key[k];
k++;
else
while(find(key,len,alph[t]))
t++;
fang[i][j]=alph[t];
t++;
char t1,t2;
int i_1=-1,j_1=-1,i_2=-1,j_2=-1;
for(i=0;i<strlen(yuanwen);i+=2)
t1=yuanwen[i];
t2=yuanwen[i+1];
if(t1==t2||t2=='\0')
miwen[i]=t1;
miwen[i+1]=t2;
else
find_fang(fang,t1,&i_1,&j_1);
find_fang(fang,t2,&i_2,&j_2);
if(i_1==-1||j_1==-1||i_2==-1||j_2==-1)
miwen[i]=t1;
miwen[i+1]=t2;
else if(i_1==i_2||j_1==j_2)
miwen[i]=t2;
miwen[i+1]=t1;
else
miwen[i]=fang[i_1][j_2];
miwen[i+1]=fang[i_2][j_1];
printf("加密后的密文为:");
puts(miwen);
int find(char *key,int n,char c)
int i,flag=0;
for(i=0;i<n;i++)
if(key[i]==c)
flag=1;
break;
return flag;
void find_fang(char fang[5][5],char c1,int *i_1,int *j_1)
int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(fang[i][j]==c1)
*i_1=i;
*j_1=j;
break;
本回答被提问者采纳
playfair密码解密加密
#include
#include
#include
#include
int A[1000][1000];//转化矩阵
int a[1000][1000];//单位矩阵[A E]
int B[1000][1000];//矩阵的逆矩阵A^(-1)
int ming[1000][1000];//明文矩阵
int mi[1000][1000];//密文矩阵
int n;//矩阵的阶数
void input()//输入数据
{
int i, j;
for( i = 1; i <= n; i++ )
for( j = 1; j <= n; j++ )
A[i][j] = rand() % 26;
memcpy( a, A, sizeof( A ) );//将矩阵A复制给a
for( i = 1; i <= n; i++ )//将矩阵变成[a E]的形式,E为单位矩阵
{
for( j = n + 1; j <= 2*n; j++ )
{
if( i + n == j )
a[i][j] = 1;
else
a[i][j] = 0;
}
}
}
void output() //输出函数
{
int i,j;
printf("矩阵A的元素\n");
for( i = 1; i <= n; i++ )
{
for( j = 1; j <= n; j++ ){
printf("%d ",A[i][j]);
}
printf("\n");
}
printf("A矩阵的逆矩阵B为\n");
for( i = 1; i <= n; i++ )//输出A矩阵的逆矩阵B
{
for( j = 1; j <= n; j++ )
{
B[i][j] = a[i][j+n];
printf("%d ",B[i][j]);
}
printf("\n");
}
}
int Extend_Gcd( int a, int b, int &x, int &y )//扩展欧几里得算法
{
if( b == 0 )
{
x = 1;
y = 0;
return a;
}
int r = Extend_Gcd( b, a % b, x, y );
int t = x;
x = y;
y = t - a / b * y;
return r;
}
int ni( int a)//求逆a*x=1(mod n)
{
int x, y;
int d = Extend_Gcd( a, 26, x, y );
if( d == 1 )
return ( x % 26 + 26 ) % 26;
else
return -1;
}
int gaosi()//高斯-约当消元求A矩阵的逆矩阵B
{
int i, j, k;
for( k = 1; k <= n; k++ )//高斯-约当消元
{
int Ni = ni( a[k][k] );
if( Ni == -1 ) return 0;
for( i = k + 1; i <= 2 * n; i++ )
a[k][i] = ( a[k][i] * Ni % 26 + 26 ) % 26;
for( i = 1; i <= n; i++ )
{
if( i == k ) continue;
for( j = k + 1; j <= 2 * n; j++ )
a[i][j] = ( ( a[i][j] - a[i][k] * a[k][j] % 26 ) % 26 + 26 ) % 26;
}
}
return 1;
}
void jiami() //加密过程
{
int i, j, k;
char mingstr[100];
char mingc;
printf("请输入明文");
scanf("%s",&mingstr);
int len = strlen( mingstr );
if( len % n )
{
for( i = len; i < len/n*n+n; i++)
mingstr[i] = ‘a‘;
mingstr[i] = ‘\0‘;
}
puts( mingstr );
int Len = strlen( mingstr );
for( i = 1; i <= Len/n; i++ )//将明文分成len/n段
{
for( j = 1; j <= n; j++ )//求每一段的明文转换为矩阵
{
if( mingstr[(i-1)*n+j-1] >= ‘a‘ && mingstr[(i-1)*n+j-1] <= ‘z‘ )
ming[i][j] = mingstr[(i-1)*n+j-1] - ‘a‘;
else
ming[i][j] = mingstr[(i-1)*n+j-1] - ‘A‘;
}
}
for( k = 1; k <= Len/n; k++ )//求len/n段的密文矩阵
{
for( i = 1; i <= n; i++ )//利用矩阵的乘法
{
mi[k][i] = 0;
for( j = 1; j <= n; j++ )
mi[k][i] = ( mi[k][i] + ming[k][j] * A[j][i] % 26 + 26 ) % 26;
}
}
printf("密文为");
for( i = 1; i <= Len/n; i++ )//输出密文
{
for( j = 1; j <= n; j++ )
{
mingc = mi[i][j] + ‘A‘;
printf("%c",mingc);
}
}
printf("\n");
}
void jiemi() //解密过程
{
int i, j, k;
char mistr[100];
char mingc;
printf("请输入密文");
scanf("%s",&mistr);
int len = strlen( mistr );
for( i = 1; i <= len/n; i++ )//将密文分成len/n段
{
for( j = 1; j <= n; j++ )//求每一段的密文转换为矩阵
{
if( mistr[(i-1)*n+j-1] >= ‘a‘ && mistr[(i-1)*n+j-1] <= ‘z‘ )
mi[i][j] = mistr[(i-1)*n+j-1] - ‘a‘;
else
mi[i][j] = mistr[(i-1)*n+j-1] - ‘A‘;
}
}
for( k = 1; k <= len/n; k++ )//求len/n段的明文矩阵
{
for( i = 1; i <= n; i++ )//利用矩阵的乘法
{
ming[k][i] = 0;
for( j = 1; j <= n; j++ )
ming[k][i] = ( ming[k][i] + mi[k][j] * B[j][i] % 26 + 26 ) % 26;
}
}
printf("明文为");
for( i = 1; i <= len/n; i++ )//输出明文
{
for( j = 1; j <= n; j++ )
{
mingc = ming[i][j] + ‘A‘;
printf("%c",mingc);
}
}
printf("\n");
}
int main()
{
int flag;
do{
printf( "1.加密2.解密3.退出\n");
scanf("%d",&flag);
if(flag==1)
{printf("请输入加密矩阵的阶数n:");
scanf("%d",&n);
do{
input();//数据输入
}while( !gaosi() );
output();
jiami();}//加密过程
else if(flag==2)
{jiemi();//解密过程
}
else if(flag!=1&&flag!=2&&flag!=3) printf("输入错误,请重新输入!\n");
}while(flag!=3);
return 0;
}
*******************************************************************************************
#ifndef _PLAY_FAIR_H_
#define _PLAY_FAIR_H_
void cst_playfair_table (char *in_key);
void prnt_playfair_table ();
int playfair_encrypt (char *plain_txt, char *cipher_txt);
int playfair_decrypt (char *cipher_txt, char *plain_txt);
#endif
***********************************************************
#include
#include
#include
#include "playfair.h"
#include "playfair.c"
int main ()
{
char plain_txt[1000], cipher_txt[1000];
char key[26] = {0};
printf ("输入加密密钥: ");
scanf ("%s", key);
cst_playfair_table (key);
printf ("密钥矩阵为:\n");
prnt_playfair_table ();
printf ("输入明文: ");
scanf ("%s", plain_txt);
playfair_encrypt (plain_txt, cipher_txt);
printf ("密文为: %s\n", cipher_txt);
playfair_decrypt (cipher_txt, plain_txt);
printf ("把密文解密 :%s\n", plain_txt);
return 0;
}
*****************************************************
以上是关于一种playfair密码变种加密方法如下:首先选择一个密钥单词(称为pair)(字母不重复,且都为小写字母)的主要内容,如果未能解决你的问题,请参考以下文章