导航:首页 > 文档加密 > 仿射加密

仿射加密

发布时间:2022-01-20 12:37:13

① 下面密文是用模26下的仿射密码加密的gzyyf,明文以he开头,试解密消息

hellk.仿射密码公式是:密文=明文剩于密钥1加密钥2然后和与26取余,即e(x)=(ax+b)mod26. 从提示he开头,代入即g=(ha+b)mon26,z=(ea+b)mod26,可求得a=11. b=7.,从而得出其它结果。注:公式中,a表示密钥1,且与26互质,即取值是1.3.5.7...23.25共13个,b是密钥2,取值范围是0-25.0-25对应英文字母a-z. 所以h=7,e=4,其它类推。还有,结果不应该是hello? 算出来是hellk.

② 仿射密码怎样实现选择明文攻击,唯密文攻击,已知明文攻击

密码学的知识,不太了解,帮你找了下
http://book.csdn.net/bookfiles/884/10088427852.shtml
往下一点就找到了,网页有点乱~~

6. 仿射密码
把加法密码和乘法密码联合起来,就得到了所谓的仿射密码(affine cipher)—— 两种密码与一对密钥的组合。乘法密码使用第一个密钥,加法密码使用第二个密钥。如图3-11所示,仿射密码其实就是被先后使用的两种密码。我们本来可以提出一种有关加密和解密的复杂运算,如C = (P ′ k1 + k2) mod 26和P = ((C - k2) ′ k1-1) mod 26。然而,我们用临时结果(T)表示两种单独的运算,以表明无论什么时候使用密码组合,均需确保在行的另一端要有一个逆,该逆在加密和解密过程中使用的顺序是相反的。如果加法是加密过程中的最终运算,那么减法就是解密过程中的初始运算。

在仿射密码中,明文P和密文C的关系是

图3-11 仿射密码

例3.9
仿射密码使用密钥对,在该密钥对中第一密钥来自集合Z26*,第二密钥来自集合Z26。密钥域的长度是26 ′ 12 = 312。

例3.10
运用仿射密码对带有密钥对(7, 2)的信息“hello”进行解密。

解答

对乘法密钥使用7,对加法密钥使用2,就会得到“ZEBBW”。

例3.11
在模26中,用仿射密码对具有密钥对(7, 2)的信息“ZEBBW”进行解密。

解答

把- 2 o 24 (mod 26)的加法逆加在接收的密文中。然后,再用结果乘以7–1 o 15 (mod 26)的乘法逆,就求出了明文字符。因为2在集合Z26中有一个加法逆,7在集合Z26*中有一个乘法逆,明文正好就是我们在例3.10中所用的。

③ 仿射加密法的举例

设仿射加密函数是 ,由上表知:11-1mod(26)=19,所以相应的仿射解密函数是 。
若加密明文是sorcery,首先把明文每个字母转换为数字18,14,17,2,4,17,24。然后对明文进行加密,得密文为welcylk。

④ 仿射加密c语言,以下程序能运行,但出不了结果,求更改。不改形式。!!

#include<iostream>//仿射加密,输入sorcery-->输出welcylk
usingnamespacestd;
#include<string.h>

voidFsJiami(chars[99])
{
inti;inta[99];char*p;
p=s;
while(*p!='')
{
for(i=0;i<7;i++)
a[i]=s[i]-'a';//字符转换为数字
for(i=0;i<7;i++)
{
a[i]=(11*a[i]+6)%26;//仿射加密函数
s[i]=a[i]+'a';//数字转换为字符
}
p++;//这里需要这个。。不过似乎你的原理错了。。
}
}

voidmain()
{
chars[99];inti;
gets(s);
FsJiami(s);
for(i=0;i<7;i++)
{
cout<<s[i];
if((i+1)%7==0)cout<<endl;
}

}

⑤ 密码学加密(仿射加密)

QVWB MECSRI E RWSQY!

移位密码、仿射密码、维吉尼亚密码以及置换密码的源代码:
VC6.0调试通过

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <cctype>
#include <malloc.h>
#include <iostream>
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 to return...");
getch();
return;
}
for(i=0; i<length; 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: %s\n", c);
for(i=0; i<length; 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%s\n", 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 to return...");
return;
}

for(i=0; i<length; 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: %s\n", c);
for(i=0; i<length; 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%s\n", 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<lenc; 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: %s\n", c);
for(i=0, j=0; i<lenc; 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%s\n", 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<len; i++)
{
if(c[i]<65||c[i]>90)
{
for(j=i; j<len-1; 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<m; i++)
{
scanf("%d", key+i);
key[i]--;
}
getchar();

for(i=0; i<len; i++)
{
j = (i/m)*m;
q[i] = c[*(key+i%m)+j];
}
q[i] = '\0';

printf("Result is: %s\n", q);

for(i=0, j=0; i<len; 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%s\n", 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;
}

⑥ 仿射加密法的注意事项

注1. 仿射加密函数要求gcd(a,26)=1,即要求a和26互素,否则 就不是一个单射函数。
注2. 从仿射加密函数的表达式易知,当a=1,b=3时,这种仿射密码就是着名的凯撒密码。
注3. 在求解仿射解密函数时,需要求a在Z26上的乘法逆元a-1∈Z26,这可由扩展欧几里得算法求解,下表列出了在Z26上所有与26互素元素的乘法逆元:
1-13-15-17-19-111-115-117-119-121-123-125-11921153197231151725

⑦ 仿射加密法的含义

仿射加密法与单码加密法没什么不同,因为明文的每个字母分别只映射到一个密文字母。仿射密码的加密算法就是一个线性变换,即对任意的明文字符x,对应的密文字符为 ,其中,a,b∈Z26,且要求gcd(a,26)=1,函数e(x)称为仿射加密函数。

阅读全文

与仿射加密相关的资料

热点内容
工作三年的大专程序员 浏览:728
java毕业设计文献 浏览:143
筹码集中度指标源码 浏览:482
listsortjava 浏览:186
plc闪光电路编程实例 浏览:299
socket编程试题 浏览:206
华为的服务器怎么设置从光驱启动 浏览:871
程序员真的累吗 浏览:328
学信网app为什么刷脸不了 浏览:874
天蝎vs程序员 浏览:996
单片机下载口叫什么 浏览:190
程序员的道 浏览:926
云服务器不实名违法吗 浏览:558
怎样查看文件夹图片是否重复 浏览:995
文件怎么导成pdf文件 浏览:808
打开sql表的命令 浏览:103
安卓手机如何面部支付 浏览:38
天元数学app为什么登录不上去 浏览:825
明日之后为什么有些服务器是四个字 浏览:104
安卓系统l1是什么意思 浏览:26