Answer the question
In order to leave comments, you need to log in
How to write a Vernam decryption function?
The task is to write a program that encrypts and decrypts the input text using the Vernam method.
I wrote encryption, but there is a problem with decryption.
Help with the decryption function who knows.
#include <string>
#include <iostream>
#include <tchar.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string B, C, D = "";
string A = "abcdefghijklmnopqrstuvwxyz"; //наш алфавит
cout << "Enter text to encrypt : " <<endl;
cin >> B;
cout << "Enter key for encryption : "<<endl;
cin >> C;
int* F = new int[B.size()];
int* G = new int[B.size()];
int c = C.size(); //делаем замену переменных для удобства
int b = B.size();
//Первое условие. Если длина вводимого слова больше, либо равна длине ключа
if (b >= c)
{
for (int i = 0; i < (b / c); i++)
{
D = D + C; //Записываем целое количество ключа. Растягиваем ключ по длине слова.
}
for (int j = 0; j < (b % c); j++)
{
D = D + C[j];
}
}
else
{
for (int s = 0; s < b; s++)
{
D = D + B[s];
} //Иначе если ключ длинее слова, ускорачиваем ключ до длины слова.
}
cout << D << endl;
for (int k = 0; k < b; k++)
{
for (int n = 0; n < 26; n++)
{
if (B[k] == A[n])
{
F[k] = n;
}
if (D[k] == A[n])
{
G[k] = n;
} //Здесь мы уже начинаем щифровать. Смысл заключается а том, что мы ишем номер буквы во
//вводимом ключе и номере, а после чего записываем
//их в массив
}
}
int e = 0;
for (int u = 0; u < b; u++)
{
e = ((F[u] + G[u]) % 26);
B[u] = A[e];
}
cout << " Encrypted text: " << B << endl;
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
Working encryption and decryption using the Vernam method
#include<iostream>
#include <conio.h>
using namespace std;
char array[26][26];
char text[22],key[22],encrypted_Text[22],decrypted_Text[22];
int findRow(char);
int findColumn(char);
int findDecRow(char,int);
int main()
{
int i=0,j,k,r,c;
k=96;
for (i=0;i<26;i++)
{
k++;
for (j=0;j<26;j++)
{
array[i][j]=k++;
if(k==123)
k=97;
}
}
cout << "Enter text to encrypt it :"<<endl;
cin >> text;
cout << "Enter key for encryption:"<<endl;
cin >> key;
// Encryption
for (i=0;key[i]!=NULL;i++)
{
c=findRow(key[i]);
r=findColumn(text[i]);
encrypted_Text[i]=array[r][c];
}
encrypted_Text[i]='\0';
cout << "Encrypted text is :"<<endl;
cout <<encrypted_Text<<endl;
//decryption
for (i=0;key[i]!=NULL;i++)
{
c=findColumn(key[i]);
r=findDecRow(encrypted_Text[i],c);
decrypted_Text[i]=array[r][0];
}
decrypted_Text[i]='\0';
cout << "Decrypted text is:"<<endl;
cout << decrypted_Text<<endl;
getch();
return(0);
}
int findRow(char c)
{
int i;
for (i=0;i<26;i++)
{
if(array[0][i]==c)
return(i);
}
return 0;
}
int findColumn(char c)
{
int i;
for (i=0;i<26;i++)
{
if(array[i][0]==c)
return(i);
}
return 0;
}
int findDecRow(char c,int j)
{
int i;
for (i=0;i<26;i++)
{
if(array[i][j]==c)
return(i);
}
return 0;
}
Since the cipher Comrade. Vernam - symmetrical, you have already written the decryption.
Let me explain - in a symmetric cryptosystem, for encryption, the gamma is added (in the case of W. Vernam, the key from the notebook) modulo equal to the power of the alphabet, with the encrypted text.
To decrypt, you need to add the same gamma again in the same modulus with the encrypted message - and voila, you have the decrypted text at the output.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question