宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

弗吉尼亚密码:

加密:

voidenVirginiachar*plaintext,int* k ) int length=strlen plaintext ); int m,n; forintI=0; i length; I ) {m=plaintext[i] – ‘a ‘; n=m k[i]; n 26? n=n : n=n % 26; printf’%c ‘,’ a’ n ); }printfn ); }解密:

voiddeVirginiachar*ciphertext,int* k ) intlength=strlen ) ciphertext ); int m,n; forintI=0; i length; I ) {m=ciphertext[i] – ‘a ‘; n=m – k[i]; n=0? n=n : n=n 26; printf’%c ‘,’ a’ n ); }printfn ); 希尔密码:

加密:

voidenhillcipher char * plaintext,int k[][10] ) intlength=strlen ) plaintext; int x[100]; int y[4][4]; forintI=0; i=length; I ) {x[i 1]=plaintext[i] – ‘a ‘; }forintI=1; i=3; I ) forintj=1; j=3; j ) {y[I][j]=x[I-1 ) * 3 j]; }int matrix[100]={ 0 }; int count=1; int flag=1; whilecount=length ) for ) intI=0; i 3; I ) forintj=0; j3; j { matrix [ count ]=k [ I ] [ j ] * y [ flag ] [ J1 ] matrix [ count ]; }matrix[count]=matrix[count]&; 出局; }标志; }forintj=1; j length; j ) printf’%c ‘,’ a’ matrix[j] ); }printfn ); }解密:

voiddehillcipher char * ciphertext,int k[][10] ) intlength=strlen ) cipher text ); int x[100]; int y[4][4]; forintI=0; i=length; I ) xI1 )=ciphertext )-‘a ); }forintI=1; i=3; I ) forintj=1; j=3; j ) {y[I][j]=x[I-1 ) * 3 j]; }int matrix[100]={ 0 }; int count=1; int flag=1; whilecount=length ) for ) intI=0; i 3; I ) forintj=0; j 3; j { matrix [ count ]=k [ I ] [ j ] * y [ flag ] [ J1 ] matrix [ count ]; }matrix[count]=matrix[count]&; 出局; }标志; }forintj=1; j length; j ) printf’%c ‘,’ a’ matrix[j] ); }printfn ); )希尔密码的加密算法其实是一样的,但是密钥不同,解密的密钥是加密密钥的乘法逆元。 另外,钥匙的长度也可以自己调整。 密钥选择的矩阵的行数和列数最好等于明文的长度。 上面的算法使用了3*3矩阵。 我用来加密8长的明文。 在明文后添加任意字符,最后输出8个即可。

由于时间有限,以后生成8*8、或者更高维的n次方阵的算法,以后会慢慢学习并上传。