仿射密码

仿射密码


2024年3月13日发(作者:)

仿射密码

o 加法密码和乘法密码结合就构成仿射密码,仿射密码的加密和解密算法是:

C

=

Ek

(

m

)=(

k

1

m

+

k

2) mod

n

M

=

Dk

(c)=

k

1(

c

-

k

2) mod

n

o 仿射密码具有可逆性

的条件是gcd(

k

,

n

)=1。当

k

1=0时,仿射密码变为加法密码,当

k

2=0时,仿射密码变为

乘法密码。 o 仿射密码中的密钥空间的大小为

nφ(n)

,当

n

为26字母,

φ(n)

=12,

因此仿射密码的密钥空间为12×26 = 312。 仿射密码举例 o 设密钥K= (7, 3),

用仿射密码加密明文hot。 o 三个字母对应的数值是7、14和19。分别加密如下:

(7×7 + 3) mod 26 = 52 mod 26 =0 (7×14 + 3) mod 26 = 101 mod 26 =23

(7×19 + 3) mod 26 =136 mod 26 =6 o 三个密文数值为0、23和6,对应的密文是

AXG。

移位密码、仿射密码、维吉尼亚密码以及置换密码的源代码:

VC6.0调试通过

#include

#include

#include

#include

#include

#include

using namespace std;

void Shift() /*移位密码*/

{

char c[100];

int length, i=0, key=0;

system("cls");

printf("********Shift Cipher(移位密码)********nPlease input primal sentence(请

输入最初的明文--一组英文字母): ");

gets(c);

length=strlen(c);

printf("Input the key(0~26): ");

scanf("%d", &key);

getchar();

if(key<0)

{

printf("The value of key is error!nPress any key ");

getch();

return;

}

for(i=0; i

{

if(c[i]>96&&c[i]<123)

c[i] = (c[i]+key-97)%26+65;

else if(c[i]>64&&c[i]<91)

c[i] = (c[i]+key-65)%26+65;

}

printf("Result is: %sn", c);

for(i=0; i

{

if(c[i]>64&&c[i]<91)

c[i] = (c[i]-key-65+26)%26+97;

}

printf("nAfter translated the sentence,we can see the primal sentence as

follow:n%sn", c);

printf("Press any key to return(按任何键返回)...");

getch();

}

int gcd(int a, int b) /*辗转相除法求a,b的最大公因数*/

{

int k = 0;

do

{

k = a%b;

a = b;

b = k;

}while(k!=0);

return a;

}

int Ni(int a, int b) /*求a相对于b的逆*/

{

int i = 0;

while(a*(++i)%b!=1);

return i;

}

void Affine() /*仿射密码*/

{

char c[100];

int length, i=0, ka=0, kb=0, tmp;

system("cls");

printf("********Affine Cipher(仿射密码)********nPlease input primal

sentence(请输入最初的明文): ");

gets(c);

length = strlen(c);

printf("Input the key(2 numbers): ");

scanf("%d%d", &ka, &kb);

getchar();

if(gcd(ka,26)!=1)

{

printf("The value of the key is error!nPress any key ");

return;

}

for(i=0; i

{

if(c[i]>96&&c[i]<123)

c[i] = (ka*(c[i]-97)+kb)%26+65;

else if(c[i]>64&&c[i]<91)

c[i] = (ka*(c[i]-65)+kb)%26+65;

}

printf("Result is: %sn", c);

for(i=0; i

{

if(c[i]>64&&c[i]<91)

{

tmp = Ni(ka,26)*((c[i]-65)-kb);

if(tmp<0)

c[i] = tmp%26+26+97;

else

c[i] = tmp%26+97;

}

}

printf("nAfter translated the sentence,we can see the primal sentence as

follow:n%sn", c);

printf("Press any key to return(按任何键返回)...");

getch();

}

void Vigenere() /*维吉利亚密码*/

{

char c[100], key[100];

int lenc, lenk, i=0, j=0, tmp;

system("cls");

printf("********Vigenere Cipher(维吉利亚密码)********nPlease input primal

sentence(请输入最初的明文): ");

gets(c);

lenc = strlen(c);

strcpy(c, strupr(c));

printf("Input the key: ");

gets(key);

lenk = strlen(key);

strcpy(key, strupr(key));

for(; i

{

j = j%lenk;

if(c[i]>64&&c[i]<91)

{

c[i] = (c[i]-65+key[j]-65)%26+65;

j++;

}

}

printf("Result is: %sn", c);

for(i=0, j=0; i

{

j = j%lenk;

if(c[i]>64&&c[i]<91)

{

tmp = c[i]-65-(key[j]-65);

if(tmp>=0)

c[i] = tmp%26+97;

else

c[i] = (tmp+26)%26+97;

j++;

}

}

printf("nAfter translated the sentence,we can see the primal sentence as

follow:n%sn", c);

printf("Press any key to return(按任何键返回)...");

getch();

}

void Permutation() /*置换密码*/

{

char c[100], *q;

int *key, len, m, i, j=0;

system("cls");

printf("********Permutation Cipher(置换密码)********nPlease input primal

sentence(请输入最初的明文): ");

gets(c);

strcpy(c, strupr(c));

len = strlen(c);

for(i=0; i

{

if(c[i]<65||c[i]>90)

{

for(j=i; j

c[j] = c[j+1];

len--;

}

}

c[len] = '0';

printf("Input the length of the key: ");

scanf("%d", &m);

key = (int*)malloc(m*sizeof(int));

q = (char *)malloc(len*sizeof(int));

printf("Input the key: ");

for(i=0; i

{

scanf("%d", key+i);

key[i]--;

}

getchar();

for(i=0; i

{

j = (i/m)*m;

q[i] = c[*(key+i%m)+j];

}

q[i] = '0';

printf("Result is: %sn", q);

for(i=0, j=0; i

{

j = (i/m)*m;

c[*(key+i%m)+j] = q[i]+32;

}

c[len] = '0';

printf("After translated the sentence,we can see the primal sentence as

follow:n%sn", c);

printf("Press any key to return(按任何键返回)...");

free(key);

free(q);

getch();

}

int main()

{

char i = '0';

system("cls");

while(i!='5')

{

system("cls");

printf("********Press 1~5 to choose(请按1~5选择):********n");

printf("1. Shift Cipher(移位密码)n2. Affine Cipher(仿射密码)n3. Vigenere

Cipher(维吉利亚密码)n4. Permutation Cipher(置换密码)n5. Exit(退出)n");

i = getch();

if(i=='1')

Shift();

else if(i=='2')

Affine();

else if(i=='3')

Vigenere();

else if(i=='4')

Permutation();

else if(i=='5')

break;

}

system("pause");

return 0;

}


发布者:admin,转转请注明出处:http://www.yc00.com/news/1710293783a1732281.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信