openGPMP
Open Source Mathematics Package
Public Member Functions | List of all members
gpmp::Cipher Class Reference

#include <cipher.hpp>

Public Member Functions

std::string caesar (std::string plaintext, int64_t key)
 
std::string keyword_encode (std::string key)
 
std::string keyword (std::string plaintext, std::string encoded_text)
 

Detailed Description

Examples
cipher.cpp.

Definition at line 49 of file cipher.hpp.

Member Function Documentation

◆ caesar()

std::string gpmp::Cipher::caesar ( std::string  plaintext,
int64_t  key 
)
Examples
cipher.cpp.

Definition at line 46 of file cipher.cpp.

46  {
47  std::string hashtext = "";
48 
49  for (int64_t i = 0; uint64_t(i) < plaintext.length(); i++) {
50  // inject transformation
51  if (isupper(plaintext[i])) {
52  // upper case
53  hashtext += char(int64_t(plaintext[i] + key - 65) % 26 + 65);
54  } else {
55  // lower case
56  hashtext += char(int64_t(plaintext[i] + key - 97) % 26 + 97);
57  }
58  }
59 
60  return hashtext;
61 }

Referenced by main().

◆ keyword()

std::string gpmp::Cipher::keyword ( std::string  plaintext,
std::string  encoded_text 
)
Examples
cipher.cpp.

Definition at line 99 of file cipher.cpp.

100  {
101  std::string cipher = "";
102 
103  /*
104  * This loop ciphered the message. Spaces, special characters
105  * and numbers remain same.
106  */
107  for (int64_t i = 0; uint64_t(i) < plaintext.size(); i++) {
108  if (plaintext[i] >= 'a' && plaintext[i] <= 'z') {
109  int64_t pos = plaintext[i] - 97;
110  cipher += encoded_text[pos];
111  }
112 
113  else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') {
114  int64_t pos = plaintext[i] - 65;
115  cipher += encoded_text[pos];
116  }
117 
118  else {
119  cipher += plaintext[i];
120  }
121  }
122  return cipher;
123 }

Referenced by main().

◆ keyword_encode()

std::string gpmp::Cipher::keyword_encode ( std::string  key)
Examples
cipher.cpp.

Definition at line 63 of file cipher.cpp.

63  {
64  std::string encoded = "";
65  // This array represents the 26 letters of alphabets
66  bool arr[26] = {0};
67 
68  // This loop inserts the keyword at the start of the encoded
69  // string
70  for (int64_t i = 0; uint64_t(i) < key.size(); i++) {
71  if (key[i] >= 'A' && key[i] <= 'Z') {
72  /*
73  * To check whether the character is inserted earlier
74  * in the encoded string or not
75  */
76  if (arr[key[i] - 65] == 0) {
77  encoded += key[i];
78  arr[key[i] - 65] = 1;
79  }
80  } else if (key[i] >= 'a' && key[i] <= 'z') {
81  if (arr[key[i] - 97] == 0) {
82  encoded += key[i] - 32;
83  arr[key[i] - 97] = 1;
84  }
85  }
86  }
87 
88  // This loop inserts the remaining characters in the encoded
89  // string.
90  for (int64_t i = 0; i < 26; i++) {
91  if (arr[i] == 0) {
92  arr[i] = 1;
93  encoded += char(i + 65);
94  }
95  }
96  return encoded;
97 }

Referenced by main().


The documentation for this class was generated from the following files: