TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
hill_cipher.cpp File Reference

Implementation of Hill cipher algorithm. More...

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include "../numerical_methods/lu_decomposition.h"
Include dependency graph for hill_cipher.cpp:

Go to the source code of this file.

Classes

class  ciphers::HillCipher
 Implementation of Hill Cipher algorithm. More...
 

Namespaces

namespace  ciphers
 Algorithms for encryption and decryption.
 

Functions

template<typename T >
static std::ostream & operator<< (std::ostream &out, matrix< T > const &v)
 
void test1 (const std::string &text)
 Self test 1 - using 3x3 randomly generated key.
 
void test2 (const std::string &text)
 Self test 2 - using 8x8 randomly generated key.
 
int main ()
 

Variables

static const char * ciphers::STRKEY
 

Detailed Description

Implementation of Hill cipher algorithm.

Program to generate the encryption-decryption key and perform encryption and decryption of ASCII text using the famous block cipher algorithm. This is a powerful encryption algorithm that is relatively easy to implement with a given key. The strength of the algorithm depends on the size of the block encryption matrix key; the bigger the matrix, the stronger the encryption and more difficult to break it. However, the important requirement for the matrix is that:

  1. matrix should be invertible - all inversion conditions should be satisfied and
  2. its determinant must not have any common factors with the length of character set Due to this restriction, most implementations only implement with small 3x3 encryption keys and a small subset of ASCII alphabets.

In the current implementation, I present to you an implementation for generating larger encryption keys (I have attempted upto 10x10) and an ASCII character set of 97 printable characters. Hence, a typical ASCII text file could be easily encrypted with the module. The larger character set increases the modulo of cipher and hence the matrix determinants can get very large very quickly rendering them ill-defined.

Note
This program uses determinant computation using LU decomposition from the file lu_decomposition.h
The matrix generation algorithm is very rudimentary and does not guarantee an invertible modulus matrix.
Todo
Better matrix generation algorithm.
Author
Krishna Vedala

Definition in file hill_cipher.cpp.

Function Documentation

◆ main()

int main ( void )

Main function

Definition at line 533 of file hill_cipher.cpp.

533 {
534 std::srand(std::time(nullptr));
535 std::cout << "Key dictionary: (" << std::strlen(ciphers::STRKEY) << ")\n\t"
536 << ciphers::STRKEY << "\n";
537
538 std::string text = "This is a simple text with numb3r5 and exclamat!0n.";
539
540 test1(text);
541 test2(text);
542
543 return 0;
544}
static void test2()
Self-implementations, 2nd test.
static void test1()
Self-test implementations, 1st test.
static const char * STRKEY

◆ operator<<()

template<typename T >
static std::ostream & operator<< ( std::ostream & out,
matrix< T > const & v )
static

operator to print a matrix

Definition at line 55 of file hill_cipher.cpp.

55 {
56 const int width = 15;
57 const char separator = ' ';
58
59 for (size_t row = 0; row < v.size(); row++) {
60 for (size_t col = 0; col < v[row].size(); col++)
61 out << std::left << std::setw(width) << std::setfill(separator)
62 << v[row][col];
63 out << std::endl;
64 }
65
66 return out;
67}

◆ test1()

void test1 ( const std::string & text)

Self test 1 - using 3x3 randomly generated key.

Parameters
textstring to encrypt and decrypt

Definition at line 471 of file hill_cipher.cpp.

471 {
472 // std::string text = "Hello world!";
473 std::cout << "======Test 1 (3x3 key) ======\nOriginal text:\n\t" << text
474 << std::endl;
475
476 std::pair<matrix<int>, matrix<int>> p =
478 matrix<int> ekey = p.first;
479 matrix<int> dkey = p.second;
480
481 // matrix<int> ekey = {{22, 28, 25}, {5, 26, 15}, {14, 18, 9}};
482 // std::cout << "Encryption key: \n" << ekey;
483 std::string gibberish = ciphers::HillCipher::encrypt_text(text, ekey);
484 std::cout << "Encrypted text:\n\t" << gibberish << std::endl;
485
486 // matrix<int> dkey = ciphers::HillCipher::generate_decryption_key(ekey);
487 // std::cout << "Decryption key: \n" << dkey;
488 std::string txt_back = ciphers::HillCipher::decrypt_text(gibberish, dkey);
489 std::cout << "Reconstruct text:\n\t" << txt_back << std::endl;
490
491 std::ofstream out_file("hill_cipher_test1.txt");
492 out_file << "Block size: " << ekey.size() << "\n";
493 out_file << "Encryption Key:\n" << ekey;
494 out_file << "\nDecryption Key:\n" << dkey;
495 out_file.close();
496
497 assert(txt_back == text);
498 std::cout << "Passed :)\n";
499}
static std::pair< matrix< int >, matrix< int > > generate_keys(size_t size, int limit1=0, int limit2=10)
Generate encryption and decryption key pair.
static const std::string decrypt_text(const std::string &text, const matrix< int > &decrypt_key)
Decrypt a given text using a given key.
static const std::string encrypt_text(const std::string &text, const matrix< int > &encrypt_key)
Encrypt a given text using a given key.
std::vector< std::valarray< T > > matrix

◆ test2()

void test2 ( const std::string & text)

Self test 2 - using 8x8 randomly generated key.

Parameters
textstring to encrypt and decrypt

Definition at line 506 of file hill_cipher.cpp.

506 {
507 // std::string text = "Hello world!";
508 std::cout << "======Test 2 (8x8 key) ======\nOriginal text:\n\t" << text
509 << std::endl;
510
511 std::pair<matrix<int>, matrix<int>> p =
513 matrix<int> ekey = p.first;
514 matrix<int> dkey = p.second;
515
516 std::string gibberish = ciphers::HillCipher::encrypt_text(text, ekey);
517 std::cout << "Encrypted text:\n\t" << gibberish << std::endl;
518
519 std::string txt_back = ciphers::HillCipher::decrypt_text(gibberish, dkey);
520 std::cout << "Reconstruct text:\n\t" << txt_back << std::endl;
521
522 std::ofstream out_file("hill_cipher_test2.txt");
523 out_file << "Block size: " << ekey.size() << "\n";
524 out_file << "Encryption Key:\n" << ekey;
525 out_file << "\nDecryption Key:\n" << dkey;
526 out_file.close();
527
528 assert(txt_back.compare(0, text.size(), text) == 0);
529 std::cout << "Passed :)\n";
530}