當前位置:首頁 » 密碼管理 » hill加密解密

hill加密解密

發布時間: 2024-05-30 11:26:15

⑴ c璇璦鎬庝箞緙栧啓hill瀵嗙爜錛

// 甯屽皵綆楁硶鐨勫姞瀵嗕笌瑙e瘑x0dx0a#include x0dx0a#include x0dx0a#include x0dx0a#include x0dx0a#include // nDime涓哄叏閮ㄥ彉閲忥紝鍙閫嗙煩闃電殑緇存暟x0dx0aint nDime;x0dx0aint index = 0;// MAXN涓烘槑鏂囩殑鏈澶ч暱搴x0dx0aconst int MAXN = 256;// 鐭╅樀鐩鎬箻錛宎鏄涓涓鍒椾負1鐨勭煩闃礬x0dx0avoid MultiplyMatrix(int a[], int b[][10], int *text)x0dx0a{x0dx0a int i, j, t; for (i = 0; i < nDime; i++)x0dx0a {x0dx0a t = 0;x0dx0a for (j = 0;j < nDime; j++)x0dx0a t += b[i][j] * a[j];x0dx0a text[index++] = t;x0dx0a }x0dx0a}// 奼傝屽垪寮忕殑鍊糪x0dx0aint determinant(int m[][10], int size)x0dx0a{x0dx0a int row, column;x0dx0a int temp1[10], temp2[10], t; for (column = 0; column < size; column++)x0dx0a {x0dx0a temp1[column] = m[0][column];x0dx0a temp2[column] = m[0][column];x0dx0a }x0dx0a for (row = 1; row < size; row++)x0dx0a {x0dx0a for (column = 0; column < size; column++)x0dx0a {x0dx0a int diff = column - row;x0dx0a int sum = column + row;x0dx0a if (diff < 0)x0dx0a diff += size;x0dx0a if (sum >= size)x0dx0a sum %= size;x0dx0a temp1[diff] *= m[row][column];x0dx0a temp2[sum] *= m[row][column];x0dx0a }x0dx0a }x0dx0a t = 0;x0dx0a for (row = 0; row < size; row++)x0dx0a t += temp1[row] - temp2[row]; return t;x0dx0a}// 奼傜煩闃典腑鏌愪竴鍏冪礌鐨勪唬鏁頒綑瀛愬紡x0dx0aint func(int matrix[][10], const int i, const int j)x0dx0a{x0dx0a int row, column, m, n;x0dx0a int NewMatrix[10][10]; m = n = 0;x0dx0a for (row = 0; row < nDime; row++)x0dx0a {x0dx0a if (i == row)x0dx0a continue;x0dx0a for (column = 0; column < nDime; column++)x0dx0a {x0dx0a if (j == column)x0dx0a continue;x0dx0a NewMatrix[m++][n++] = matrix[row][column];x0dx0a }x0dx0a }x0dx0a x0dx0a printf ("New Array:\n");x0dx0a for (row = 0; row < nDime - 1; row++)x0dx0a {x0dx0a for (column = 0; column < nDime - 1; column++)x0dx0a printf("%d ", NewMatrix[row][column]);x0dx0a printf("\n");x0dx0a } int sign = (!((i + j) % 2)) ? 1 : -1;x0dx0a return sign * determinant(NewMatrix, nDime - 1);x0dx0a}// 瀵圭煩闃墊眰閫嗭紝cm鐭╅樀涓巑鐭╅樀浜掗哱x0dx0avoid ConverseMatrix(int m[][10], int cm[][10])x0dx0a{x0dx0a // 鐭╅樀奼傞嗭紝鍒╃敤鏁板﹀叕寮廇錛堥嗭級= (1 / |A|)涔樹互A*x0dx0a // 鍏朵腑錛寍A|琛ㄧず琛屽垪寮廇鐨勫礆紝鑰孉*琛ㄧず鐭╅樀A鐨勪即闅忕煩闃礬x0dx0a int row, column;x0dx0a int StarMat[10][10]; // StarMat琛ㄧずm鐨勪即闅忕煩闃礬x0dx0a int t; // 鍒濆嬪寲浼撮殢鐭╅樀x0dx0a for (row = 0; row < 10; row++)x0dx0a for (column = 0; column < 10; column++)x0dx0a StarMat[row][column] = 0; // 奼備即闅忕煩闃礬x0dx0a for (row = 0; row < nDime; row++)x0dx0a for (column = 0; column < nDime; column++)x0dx0a {x0dx0a StarMat[row][column] = func(m, row, column);x0dx0a printf("浼撮殢鐭╅樀錛%d", StarMat[row][column]);x0dx0a } // 奼傝屽垪寮忕殑鍊糪x0dx0a t = determinant(m, nDime); // 奼傚嚭閫嗗悜鐭╅樀x0dx0a for (row = 0; row < nDime; row++)x0dx0a for (column = 0; column < nDime; column++)x0dx0a cm[row][column] = StarMat[row][column] / t;x0dx0a // 杈撳嚭閫嗗悜鐭╅樀x0dx0a for (row = 0; row < nDime; row++)x0dx0a for (column = 0; column < nDime; column++)x0dx0a printf("%d ", cm[row][column]);x0dx0a printf("\n");x0dx0a}// 甯屽皵鍔犲瘑鍙婅В瀵嗙畻娉昞x0dx0avoid ShellPassword(int *OText, int TextLen, int matrix[][10], int *text)x0dx0a{x0dx0a int i, j, n, a[10];x0dx0a x0dx0a // 鍒ゆ柇瑕佸皢OText鍒嗘垚鍑犻儴鍒哱x0dx0a n = TextLen / nDime;x0dx0a if (TextLen % nDime)x0dx0a n++; // 鐭╅樀鐩鎬箻x0dx0a // 灝哋Text鍒嗘垚鐨勫嚑閮ㄥ垎鍒嗗埆涓巑atrix鐭╅樀鐩鎬箻x0dx0a for (i = 0; i < n; i++)x0dx0a {x0dx0a for (j = 0; j < 10; j++)x0dx0a a[j] = 0;x0dx0a for (j = 0; j < nDime; j++)x0dx0a a[j] = OText[i * nDime + j];x0dx0a MultiplyMatrix(a, matrix, text);x0dx0a }x0dx0a}x0dx0aint main(void)x0dx0a{x0dx0a int i, temp, row, column;x0dx0a // matrix瀛樻斁鍔犲瘑鎴栬В瀵嗙煩闃碉紝Password涓哄姞瀵嗗悗鐨勭粨鏋淺x0dx0a // OText瀛樻斁鍘熸枃杞鎹涓烘櫘閫氭暟瀛楋紝濡侫~1錛孼~26x0dx0a int matrix[10][10], ConMatrix[10][10], OText[MAXN], Password[MAXN], OriText[MAXN];x0dx0a char text[MAXN];x0dx0a char sel; printf("=================================================\n");x0dx0a putchar('\n');x0dx0a printf(" SHELL鍔犲瘑瑙e瘑鍣\n");x0dx0a putchar('\n');x0dx0a printf("=================================================\n"); while (1)x0dx0a {x0dx0a // 鍒濆嬪寲鐭╅樀x0dx0a for (row = 0; row < 10; row++)x0dx0a for (column = 0; column < 10; column++)x0dx0a matrix[row][column] = 0; putchar('\n');x0dx0a printf("1.鍔犲瘑\n");x0dx0a printf("2.瑙e瘑\n");x0dx0a printf("0.閫鍑\n");x0dx0a printf("璇瘋緭鍏ヤ綘鐨勯夋嫨:\n");x0dx0a sel = getche(); switch (sel)x0dx0a {x0dx0a case Ƈ':x0dx0a printf("\n璇瘋緭鍏ュ師鏂:\n");x0dx0a memset(text, '\0', sizeof(text) / sizeof(char));x0dx0a memset(Password, 0, sizeof(Password) / sizeof(int));x0dx0a gets(text); printf("杈撳叆鍔犲瘑鐭╅樀鐨勭淮鏁幫紝緇存暟涓嶈兘瓚呰繃10緇:\n");x0dx0a scanf("%d", &nDime);x0dx0a printf("杈撳叆鐭╅樀錛岃ョ煩闃靛繀欏諱負鍙閫嗙煩闃碉紝鍚﹀垯灝嗕笉鑳借繘琛岃В瀵:\n");x0dx0a // 鍙閫嗙煩闃靛嵆錛岃続涓簄闃剁煩闃碉紝濡傛灉瀛榥鍦ㄩ樁鐭╅樀B浣垮緱AB=BA=1x0dx0a // 鍒欑煩闃礎鏄鍙閫嗙殑錛岀ОB鏄疉鐨勯嗙煩闃礬x0dx0a for (row = 0; row < nDime; row++)x0dx0a for (column = 0; column < nDime; column++)x0dx0a scanf("%d", &matrix[row][column]);x0dx0a // 灝嗗皬鍐欏瓧姣嶈漿鎹涓哄ぇ鍐欏瓧姣峔x0dx0a for (i = 0; text[i] != '\0' i++)x0dx0a if (islower(text[i]))x0dx0a text[i] |= 0x20;x0dx0a // OText瀛樻斁灝嗗瓧姣嶈漿鎹涓虹浉搴旀暟錛屽侫~1錛孼~26x0dx0a for (i = 0; i < MAXN; i++)x0dx0a OText[i] = 0;x0dx0a for (i = 0; text[i] != '\0' i++)x0dx0a OText[i] = text[i] - 'A' + 1;x0dx0a // 鍔犲瘑x0dx0a ShellPassword(OText, strlen(text), matrix, Password);x0dx0a // 灝嗗姞瀵嗗悗鐨勫唴瀹規墦鍗板嚭鏉x0dx0a printf("鍔犲瘑鍚庣殑鍐呭逛負:\n");x0dx0a for (i = 0; i < strlen(text); i++)x0dx0a printf("%d ", Password[i]);x0dx0a putchar('\n');x0dx0a break;x0dx0a case ƈ':x0dx0a break;x0dx0a case Ɔ':x0dx0a return 0;x0dx0a default:x0dx0a break;x0dx0a }x0dx0a getchar();x0dx0a } return 0;x0dx0a} 璇戠爜綆楁硶鎴戜細鍦ㄦ槑澶╀笂浼犱笂鏉ワ紝浣犵殑鍔犲瘑瀵嗛掗鏄涓涓涓夐樁鐨勬暟緇勶紝瀵嗘枃C鏄錛1729 2514 811 1659 2472 858 1739 2514 849 1902 2736 905 1659 2472 858

c語言編寫hill密碼

// 希爾演算法加密與解密
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <memory.h>// nDime為全部變數,可逆矩陣的維數
int nDime;
int index = 0;// MAXN為明文的最大長度
const int MAXN = 256;// 矩陣相乘,a是一個列為1的矩陣
void MultiplyMatrix(int a[], int b[][10], int *text)
{
int i, j, t; for (i = 0; i < nDime; i++)
{
t = 0;
for (j = 0;j < nDime; j++)
t += b[i][j] * a[j];
text[index++] = t;
}
}// 求行列式的值
int determinant(int m[][10], int size)
{
int row, column;
int temp1[10], temp2[10], t; for (column = 0; column < size; column++)
{
temp1[column] = m[0][column];
temp2[column] = m[0][column];
}
for (row = 1; row < size; row++)
{
for (column = 0; column < size; column++)
{
int diff = column - row;
int sum = column + row;
if (diff < 0)
diff += size;
if (sum >= size)
sum %= size;
temp1[diff] *= m[row][column];
temp2[sum] *= m[row][column];
}
}
t = 0;
for (row = 0; row < size; row++)
t += temp1[row] - temp2[row]; return t;
}// 求矩陣中某一元素的代數餘子式
int func(int matrix[][10], const int i, const int j)
{
int row, column, m, n;
int NewMatrix[10][10]; m = n = 0;
for (row = 0; row < nDime; row++)
{
if (i == row)
continue;
for (column = 0; column < nDime; column++)
{
if (j == column)
continue;
NewMatrix[m++][n++] = matrix[row][column];
}
}

printf ("New Array:\n");
for (row = 0; row < nDime - 1; row++)
{
for (column = 0; column < nDime - 1; column++)
printf("%d ", NewMatrix[row][column]);
printf("\n");
} int sign = (!((i + j) % 2)) ? 1 : -1;
return sign * determinant(NewMatrix, nDime - 1);
}// 對矩陣求逆,cm矩陣與m矩陣互逆
void ConverseMatrix(int m[][10], int cm[][10])
{
// 矩陣求逆,利用數學公式A(逆)= (1 / |A|)乘以A*
// 其中,|A|表示行列式A的值,而A*表示矩陣A的伴隨矩陣
int row, column;
int StarMat[10][10]; // StarMat表示m的伴隨矩陣
int t; // 初始化伴隨矩陣
for (row = 0; row < 10; row++)
for (column = 0; column < 10; column++)
StarMat[row][column] = 0; // 求伴隨矩陣
for (row = 0; row < nDime; row++)
for (column = 0; column < nDime; column++)
{
StarMat[row][column] = func(m, row, column);
printf("伴隨矩陣:%d", StarMat[row][column]);
} // 求行列式的值
t = determinant(m, nDime); // 求出逆向矩陣
for (row = 0; row < nDime; row++)
for (column = 0; column < nDime; column++)
cm[row][column] = StarMat[row][column] / t;
// 輸出逆向矩陣
for (row = 0; row < nDime; row++)
for (column = 0; column < nDime; column++)
printf("%d ", cm[row][column]);
printf("\n");
}// 希爾加密及解密演算法
void ShellPassword(int *OText, int TextLen, int matrix[][10], int *text)
{
int i, j, n, a[10];

// 判斷要將OText分成幾部分
n = TextLen / nDime;
if (TextLen % nDime)
n++; // 矩陣相乘
// 將OText分成的幾部分分別與matrix矩陣相乘
for (i = 0; i < n; i++)
{
for (j = 0; j < 10; j++)
a[j] = 0;
for (j = 0; j < nDime; j++)
a[j] = OText[i * nDime + j];
MultiplyMatrix(a, matrix, text);
}
}
int main(void)
{
int i, temp, row, column;
// matrix存放加密或解密矩陣,Password為加密後的結果
// OText存放原文轉換為普通數字,如A~1,Z~26
int matrix[10][10], ConMatrix[10][10], OText[MAXN], Password[MAXN], OriText[MAXN];
char text[MAXN];
char sel; printf("=================================================\n");
putchar('\n');
printf(" SHELL加密解密器\n");
putchar('\n');
printf("=================================================\n"); while (1)
{
// 初始化矩陣
for (row = 0; row < 10; row++)
for (column = 0; column < 10; column++)
matrix[row][column] = 0; putchar('\n');
printf("1.加密\n");
printf("2.解密\n");
printf("0.退出\n");
printf("請輸入你的選擇:\n");
sel = getche(); switch (sel)
{
case '1':
printf("\n請輸入原文:\n");
memset(text, '\0', sizeof(text) / sizeof(char));
memset(Password, 0, sizeof(Password) / sizeof(int));
gets(text); printf("輸入加密矩陣的維數,維數不能超過10維:\n");
scanf("%d", &nDime);
printf("輸入矩陣,該矩陣必須為可逆矩陣,否則將不能進行解密:\n");
// 可逆矩陣即,設A為n階矩陣,如果存n在階矩陣B使得AB=BA=1
// 則矩陣A是可逆的,稱B是A的逆矩陣
for (row = 0; row < nDime; row++)
for (column = 0; column < nDime; column++)
scanf("%d", &matrix[row][column]);
// 將小寫字母轉換為大寫字母
for (i = 0; text[i] != '\0'; i++)
if (islower(text[i]))
text[i] |= 0x20;
// OText存放將字母轉換為相應數,如A~1,Z~26
for (i = 0; i < MAXN; i++)
OText[i] = 0;
for (i = 0; text[i] != '\0'; i++)
OText[i] = text[i] - 'A' + 1;
// 加密
ShellPassword(OText, strlen(text), matrix, Password);
// 將加密後的內容列印出來
printf("加密後的內容為:\n");
for (i = 0; i < strlen(text); i++)
printf("%d ", Password[i]);
putchar('\n');
break;
case '2':
break;
case '0':
return 0;
default:
break;
}
getchar();
} return 0;
} 解碼演算法我會在明天上傳上來,你的加密密鑰是一個三階的數組,密文C是:1729 2514 811 1659 2472 858 1739 2514 849 1902 2736 905 1659 2472 858

⑶ 希爾密碼原理

希爾密碼(Hill Cipher)是運用基本矩陣論原理的替換密碼,由Lester S. Hill在1929年發明。每個字母當作26進制數字:A=0, B=1, C=2... 一串字母當成n維向量,跟一個n×n的矩陣相乘,再將得出的結果MOD26。

中文名
希爾密碼
外文名
Hill Cipher
原理
基本矩陣論
類別
替換密碼
提出者
Lester S. Hill
快速
導航
產生原因

原理

安全性分析

例子
簡介
希爾密碼是運用基本矩陣論原理的替換密碼,由Lester S. Hill在1929年發明。
每個字母當作26進制數字:A=0, B=1, C=2... 一串字母當成n維向量,跟一個n×n的矩陣相乘,再將得出的結果模26。
注意用作加密的矩陣(即密匙)在必須是可逆的,否則就不可能解碼。只有矩陣的行列式和26互質,才是可逆的。
產生原因
隨著科技的日新月異和人們對信用卡、計算機的依賴性的加強,密碼學顯得愈來愈重要。密碼學是一門關於加密和解密、密文和明文的學科。若將原本的符號代換成另一種符號,即可稱之為廣義的密碼。狹義的密碼主要是為了保密,是一種防止竊文者得知內容而設的另一種符號文字,也是一般人所熟知的密碼。
使用信用卡、網路賬號及密碼、電子信箱、電子簽名等都需要密碼。為了方便記憶,許多人用生日、電話號碼、門牌號碼記做密碼,但是這樣安全性較差。
為了使密碼更加復雜,更難解密,產生了許多不同形式的密碼。密碼的函數特性是明文對密碼為一對一或一對多的關系,即明文是密碼的函數。傳統密碼中有一種叫移位法,移位法基本型態是加法加密系統C=P+s(mod m)。一般來說,我們以1表示A,2表示B,……,25表示Y,26表示Z,以此類推。由於s=0時相當於未加密,而0≤s≤m-1(s≥m都可用0≤s≤m-1取代),因此,整個系統只有m-1種變化。換言之,只要試過m-1次,機密的信息就會泄漏出去。
由此看來,日常生活中的密碼和傳統的密碼的可靠性較差,我們有必要尋求一種容易將字母的自然頻度隱蔽或均勻化,從而有利於統計分析的安全可靠的加密方法。希爾密碼能基本滿足這一要求。
原理
希爾加密演算法的基本思想是,將d個明文字母通過線性變換將它們轉換為d個密文字母。解密只要作一次逆變換就可以了,密鑰就是變換矩陣本身。[1]
希爾密碼是多字母代換密碼的一種。多字母代換密碼可以利用矩陣變換方便地描述,有時又稱為矩陣變換密碼。令明文字母表為Z,若採用L個字母為單位進行代換,則多碼代換是映射f:Z→Z。若映射是線性的,則f是線性變換,可以用Z上的L×L矩陣K表示。若是滿秩的,則變換為一一映射,且存在有逆變換K。將L個字母的數字表示為Z上的L維矢量m,相應的密文矢量c,且mK=c,以K作為解密矩陣,可由c恢復出相應的明文c·K=m。
在軍事通訊中,常將字元(信息)與數字對應(為方便起見,我們將字元和數字按原有的順序對應,事實上這種對應規則是極易被破解的):
abcde…x y z
12345…242526
如信息「NOSLEEPPING」對應著一組編碼14,15,19,12,5,5,16,16,9,14,7。但如果按這種方式直接傳輸出去,則很容易被敵方破譯。於是必須採取加密措施,即用一個約定的加密矩陣K乘以原信號B,傳輸信號為C=KB(加密),收到信號的一方再將信號還原(破譯)為B=KC。

⑷ 為什麼說加法密碼、乘法密碼、仿射密碼、置換密碼、Hill密碼以及Vigenere密碼

加法密碼就是真典密碼學中的愷撒密碼格式是:密文=(明文+密鑰)mod26,剩法密碼是愷撒密碼發展出來,格式是:密文=明文x實鑰mon26;置換密碼就是在簡單的縱行換位密碼中,明文以固定的寬度水平的寫在一張圖表紙上,密文按垂直方向讀出,解密就是密文按相同的寬度垂直的寫在圖表紙上,然後水平的讀出明文。希爾密碼(Hill Cipher)是運用基本矩陣論原理的替換密碼,由Lester S. Hill在1929年發明。每個字母當作26進制數字:A=0, B=1, C=2... 一串字母當成n維向量,跟一個n×n的矩陣相乘,再將得出的結果MOD26;Vigenere是愷撒密碼演變而來。使用一系列凱撒密碼組成密碼字母表的加密演算法,屬於多表密碼的一種簡單形式。
有興趣可以了解一下古典密碼學,這裡面都有。

熱點內容
安卓平板有什麼可以畫對稱的 發布:2024-07-27 09:36:03 瀏覽:132
羊創意腳本 發布:2024-07-27 09:29:30 瀏覽:894
榮耀v20升級存儲 發布:2024-07-27 09:20:19 瀏覽:485
安卓用什麼和電腦傳圖片 發布:2024-07-27 09:02:07 瀏覽:288
存儲過程就是 發布:2024-07-27 08:56:51 瀏覽:131
c語言高級試題 發布:2024-07-27 08:48:30 瀏覽:282
ip伺服器世界上有幾台 發布:2024-07-27 08:46:18 瀏覽:394
金立手機怎麼清理緩存 發布:2024-07-27 08:38:50 瀏覽:311
iphone文件夾不顯示 發布:2024-07-27 08:18:05 瀏覽:774
y510p固態硬碟做緩存 發布:2024-07-27 07:59:34 瀏覽:128