導航:首頁 > 文檔加密 > 仿射加密

仿射加密

發布時間: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)稱為仿射加密函數。

閱讀全文

與仿射加密相關的資料

熱點內容
工作三年的大專程序員 瀏覽:726
java畢業設計文獻 瀏覽:140
籌碼集中度指標源碼 瀏覽:479
listsortjava 瀏覽:183
plc閃光電路編程實例 瀏覽:299
socket編程試題 瀏覽:204
華為的伺服器怎麼設置從光碟機啟動 瀏覽:869
程序員真的累嗎 瀏覽:326
學信網app為什麼刷臉不了 瀏覽:873
天蠍vs程序員 瀏覽:994
單片機下載口叫什麼 瀏覽:188
程序員的道 瀏覽:926
雲伺服器不實名違法嗎 瀏覽:558
怎樣查看文件夾圖片是否重復 瀏覽:995
文件怎麼導成pdf文件 瀏覽:808
打開sql表的命令 瀏覽:103
安卓手機如何面部支付 瀏覽:38
天元數學app為什麼登錄不上去 瀏覽:825
明日之後為什麼有些伺服器是四個字 瀏覽:104
安卓系統l1是什麼意思 瀏覽:26