Line data Source code
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 3 : std::string gpmp::Cipher::caesar(std::string plaintext, int64_t key) { 47 3 : std::string hashtext = ""; 48 : 49 36 : for (int64_t i = 0; uint64_t(i) < plaintext.length(); i++) { 50 : // inject transformation 51 33 : if (isupper(plaintext[i])) { 52 : // upper case 53 20 : hashtext += char(int64_t(plaintext[i] + key - 65) % 26 + 65); 54 : } else { 55 : // lower case 56 13 : hashtext += char(int64_t(plaintext[i] + key - 97) % 26 + 97); 57 : } 58 : } 59 : 60 3 : return hashtext; 61 0 : } 62 : 63 3 : std::string gpmp::Cipher::keyword_encode(std::string key) { 64 3 : std::string encoded = ""; 65 : // This array represents the 26 letters of alphabets 66 3 : bool arr[26] = {0}; 67 : 68 : // This loop inserts the keyword at the start of the encoded 69 : // string 70 25 : for (int64_t i = 0; uint64_t(i) < key.size(); i++) { 71 22 : 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 5 : if (arr[key[i] - 65] == 0) { 77 5 : encoded += key[i]; 78 5 : arr[key[i] - 65] = 1; 79 : } 80 17 : } else if (key[i] >= 'a' && key[i] <= 'z') { 81 15 : if (arr[key[i] - 97] == 0) { 82 14 : encoded += key[i] - 32; 83 14 : arr[key[i] - 97] = 1; 84 : } 85 : } 86 : } 87 : 88 : // This loop inserts the remaining characters in the encoded 89 : // string. 90 81 : for (int64_t i = 0; i < 26; i++) { 91 78 : if (arr[i] == 0) { 92 59 : arr[i] = 1; 93 59 : encoded += char(i + 65); 94 : } 95 : } 96 6 : return encoded; 97 0 : } 98 : 99 3 : std::string gpmp::Cipher::keyword(std::string plaintext, 100 : std::string encoded_text) { 101 3 : std::string cipher = ""; 102 : 103 : /* 104 : * This loop ciphered the message. Spaces, special characters 105 : * and numbers remain same. 106 : */ 107 27 : for (int64_t i = 0; uint64_t(i) < plaintext.size(); i++) { 108 24 : if (plaintext[i] >= 'a' && plaintext[i] <= 'z') { 109 14 : int64_t pos = plaintext[i] - 97; 110 14 : cipher += encoded_text[pos]; 111 : } 112 : 113 10 : else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') { 114 6 : int64_t pos = plaintext[i] - 65; 115 6 : cipher += encoded_text[pos]; 116 : } 117 : 118 : else { 119 4 : cipher += plaintext[i]; 120 : } 121 : } 122 3 : return cipher; 123 0 : }