Hill shifrlash chiziqli algebraga asoslangan poligrafik almashtirish shifridir

Hill shifrlash
Hill shifrlash chiziqli algebraga asoslangan poligrafik almashtirish shifridir.
Har bir harf 26 moduli bilan ifodalanadi. Ko'pincha A = 0, B = 1, …, Z = 25 oddiy
sxemasidan foydalaniladi, ammo bu muhim xususiyat emas. shifr. Xabarni shifrlash
uchun har bir n ta harf bloki (n-komponentli vektor sifatida ko'rib chiqiladi) modul
26 ga qarshi teskari n × n matritsaga ko'paytiriladi. Xabarning shifrini ochish uchun
har bir blok ishlatiladigan matritsaning teskari qismiga ko'paytiriladi. shifrlash.
Shifrlash uchun ishlatiladigan matritsa shifrlash kalitidir va u teskari n × n
matritsalar to'plamidan tasodifiy tanlanishi kerak (modul 26).
Misollar:
Kirish : ochiq matn: ACT
kaliti: GYBNQKURP
Chiqish: shifrlangan matn: POH
Kirish: ochiq matn: GFG
kaliti: HILLMAGIC
Chiqish: shifrlangan matn: SWK
Shifrlash
Biz “ACT” (n=3) xabarini shifrlashimiz kerak. Kalit “GYBNQKURP” bo‘lib,
uni nxn matritsasi sifatida yozish mumkin:
"ACT" xabari vektor sifatida yoziladi:
Shifrlangan vektor quyidagicha berilgan:
"POH" shifrlangan matnga mos keladi
Shifrni ochish
Xabarning shifrini ochish uchun biz shifrlangan matnni yana vektorga
aylantiramiz, so'ngra kalit matritsaning teskari matritsasiga (IFKVIVVMI harflar
bilan) ko'paytiramiz.Avvalgi misolda ishlatilgan matritsaning teskarisi:
Oldingi "POH" shifrlangan matn uchun:
bu bizga "ACT" ni qaytaradi.
Faraz qilaylik, barcha alifbolar katta harf bilan yozilgan.
Quyida n=3 uchun yuqoridagi fikrning amalga oshirilishi keltirilgan.
// C++ code to implement Hill Cipher
#include
using namespace std;
// Following function generates the
// key matrix for the key string
void getKeyMatrix(string key, int keyMatrix[][3])
{
int k = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
keyMatrix[i][j] = (key[k]) % 65;
k++;
}
}
}
// Following function encrypts the message
void encrypt(int cipherMatrix[][1],
int keyMatrix[][3],
int messageVector[][1])
{
int x, i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 1; j++)
{
cipherMatrix[i][j] = 0;
for (x = 0; x < 3; x++)
{
cipherMatrix[i][j] +=
keyMatrix[i][x] * messageVector[x][j];
}
cipherMatrix[i][j] = cipherMatrix[i][j] % 26;
}
}
}
// Function to implement Hill Cipher
void HillCipher(string message, string key)
{
// Get key matrix from the key string
int keyMatrix[3][3];
getKeyMatrix(key, keyMatrix);
int messageVector[3][1];
// Generate vector for the message
for (int i = 0; i < 3; i++)
messageVector[i][0] = (message[i]) % 65;
int cipherMatrix[3][1];
// Following function generates
// the encrypted vector
encrypt(cipherMatrix, keyMatrix, messageVector);
string CipherText;
// Generate the encrypted text from
// the encrypted vector
for (int i = 0; i < 3; i++)
CipherText += cipherMatrix[i][0] + 65;
// Finally print the ciphertext
cout << " Ciphertext:" << CipherText;
}
// Driver function for above code
int main()
{
// Get the message to be encrypted
string message = "ACT";
// Get the key
string key = "GYBNQKURP";
HillCipher(message, key);
return 0;
}
Chiqish:
Shifrlangan matn: POH
Xuddi shunday tarzda siz shifrlangan xabarning shifrini ochish uchun kodni
yuqorida bayon qilingan amallarni bajarish orqali yozishingiz mumkin.
|