#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; } *****************************************************
时间: 2024-10-18 18:08:18