Created
May 21, 2020 17:30
-
-
Save cypher-nullbyte/63c4b4a9908d2a28bd6af3be36a76bd8 to your computer and use it in GitHub Desktop.
Vpropel VIT | POD | 21/05/2020 | Cipher text generation | 25
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include<string> | |
#include<ctype.h> | |
using namespace std; | |
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++; | |
} | |
} | |
} | |
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; | |
} | |
} | |
} | |
void cypher(string message, string key) | |
{ | |
int keyMatrix[3][3]; | |
getKeyMatrix(key, keyMatrix); | |
int messageVector[3][1]; | |
for (int i = 0; i < 3; i++) | |
messageVector[i][0] = (message[i]) % 65; | |
int cipherMatrix[3][1]; | |
encrypt(cipherMatrix, keyMatrix, messageVector); | |
string CipherText; | |
for (int i = 0; i < 3; i++) | |
CipherText += cipherMatrix[i][0] + 65; | |
cout <<CipherText; | |
} | |
int main() | |
{ | |
string msg; | |
cin>>msg; | |
string key; | |
cin>>key; | |
if(key.length()!=9) | |
{cout<<"enter 9 letter key"; exit(1);} | |
for(int i=0;i<msg.length();i++) | |
if(islower(msg[i])) | |
msg[i]-=32; | |
for(int i=0;i<key.length();i++) | |
if(islower(key[i])) | |
key[i]-=32; | |
cypher(msg, key); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def encrypt(msg_matrix,key_matrix): | |
emsg=[] | |
for i in range(3): | |
temp=0 | |
for j in range(3): | |
temp+=msg_matrix[j]*key_matrix[i][j] | |
emsg.append(chr(temp%26+65)) | |
print(''.join(emsg)) | |
def cypher(msg,key): | |
msg_matrix=[ord(msg[i])%65 for i in range(3)] | |
temp=0; | |
key_matrix=[] | |
k=0 | |
for i in range(3): | |
temp=[] | |
for j in range(3): | |
temp.append(ord(key[k])%65) | |
k+=1 | |
key_matrix.append(temp) | |
encrypt(msg_matrix,key_matrix) | |
message=input().upper() | |
key=input().upper() | |
if len(key)!=9: | |
print("enter 9 letter key") | |
exit(None) | |
cypher(message,key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cipher text generation | |
Encryption is a security technique where the text message is converted into unreadable form called as Cipher text. | |
Generate a cipher text using polygraphic substitution method based on linear algebra. Each letter is represented by a number modulo 26. | |
To encrypt a message, each block of n letters (considered as an n-component vector) is multiplied by an invertible n × n matrix, against modulus 26. | |
The matrix used for encryption is the cipher key, and it should be chosen randomly from the set of invertible n × n matrices (modulo 26). | |
Generate a cipher text for a text message of 3 letter alphabet. Hence the key should be 3*3 matrix. | |
Check whether given key satisfies 9 characters in it. If not, popup the message for entering 9 letter key. | |
Example: | |
Encryption: | |
We have to encrypt the message ‘ACT’.The key is ‘GYBNQKURP’ which 9 in characters hence can be written as the 3x3 matrix: | |
The message ‘ACT’ is written as vector: | |
Multiplied by key matrix, encrypted output is | |
https://www.geeksforgeeks.org/hill-cipher/ | |
(for images n question) | |
Corresponding output is “POH” | |
Input Format | |
Text message: ACT | |
Key: GYBNQKURP | |
Output Format | |
Cipher text: POH |
Awesome. Thanks a lot Dude :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.geeksforgeeks.org/hill-cipher/