openGPMP
Open Source Mathematics Package
cipher.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 
34 /*
35  * This file shows the implementation of some basic stream cipher
36  * algorithms
37  */
38 #include <cmath>
39 #include <cstdlib>
40 #include <iostream>
41 #include <openGPMP/nt/cipher.hpp>
42 #include <sstream>
43 #include <stdio.h>
44 #include <string>
45 
46 std::string gpmp::Cipher::caesar(std::string plaintext, int64_t key) {
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 }
62 
63 std::string gpmp::Cipher::keyword_encode(std::string key) {
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 }
98 
99 std::string gpmp::Cipher::keyword(std::string plaintext,
100  std::string encoded_text) {
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 }
std::string keyword_encode(std::string key)
Definition: cipher.cpp:63
std::string keyword(std::string plaintext, std::string encoded_text)
Definition: cipher.cpp:99
std::string caesar(std::string plaintext, int64_t key)
Definition: cipher.cpp:46