TL:DR
I need to work out how to reverse the shiftright function using the same input values, by somehow manipulating the values after input.
/TL:DR
I have to create an encryption and decryption program that utilizes multiple methods of encryption, due to time and skill constraints I have decided on the combination of a simple shift cipher and a block permutation, I now have 2 separate programs that achieve encryption separately, which is fine for now, but I am having trouble reversing or "decrypting" the block permutation, I have found that because of the simplicity of my shift cipher I am able to just negate the chosen shift value and that returns the original input, but it doesn't have the same effect with my block cipher code, can anyone suggest how I would achieve the correct return value?
the program will eventually utilize a key value to attain numeric values that will depict the encryption method e.g. Someone will enter a password "Chopsticks" which will then be passed through a method that returns a select number of digits "12-1-3-1-4" which will then be used as the variables in the encryption, 12 will be the shift, 1 the permutation value 3 the block size etc etc...
So when someone loads in an encrypted file the key will be found on the end, and the encryption can be reversed
But at the minute I'm at odd with how to reverse the block permutation as stated, any suggestions?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
using namespace std;
char myarray[1024];
char arraySection[1024];
char strarray[1024];
string FullText;
string sectionTemp;
string EncryptedText;
int sectionLength;
int chosenLength = 5;
int *sectionCharacter;
void makeSection();
char *arrayLetter;
int permValue;
int FullTextLength;
char *shiftright(char myarray[], char size);
int main(void)
{
ifstream inFile;
string FileText;
string UserChoice;
std::cout << "Please enter the file name you wish to open: ";
getline(cin, UserChoice);
inFile.open(UserChoice);
std::cout << "Please choose your permutation: ";
cin >> chosenLength;
if (!inFile) {
std::cout << "Unable to open file";
exit(1); // terminate with error
}
while (getline(inFile, FileText))
{
std::cout << FileText << " " << endl;
}
strcpy_s(strarray, FileText.c_str());
FullText = strarray;
strarray[0] = '\0';
FullTextLength = FullText.length();
std::cout << "this is text length: " << FullTextLength << endl;
makeSection();
char *permutedBlock = shiftright(arraySection, chosenLength);
EncryptedText = "";
EncryptedText += permutedBlock;
for (int i = chosenLength; i < FullTextLength; i+=chosenLength)
{
makeSection();
permutedBlock = shiftright(arraySection, chosenLength);
EncryptedText += permutedBlock;
for (int j = 0; j < FullTextLength - i; j++)
{
FullText += " ";
}
}
std::cout << "This is strarray: " << strarray << endl;
std::cout << "This is FullText: " << FullText << endl;
std::cout << "This is arraySection: " << arraySection << endl;
std::cout << "This is EncryptedText: " << EncryptedText << endl;
return(0);
}
void makeSection()
{
arraySection[0] = '\0';
sectionLength = chosenLength;
sectionCharacter = new int[FullText.length()];
for (int i = 0; i < sectionLength ; i++)
{
sectionCharacter[i] = (char)FullText[i];
arraySection[i] = (char)sectionCharacter[i];
}
for (int i = 0; i < sectionLength; i++)
{
FullText.erase(FullText.begin());
}
delete sectionCharacter;
}
char *shiftright(char myarray[], char size)
{
int count = 0;
while (count < 1)
{
char temp = myarray[0];
for (int i = 0; i < (size - 1); i++)
{
myarray[i] = myarray[i + 1];
}
myarray[size - 1] = temp;
count++;
}
return myarray;
}
|
Input from test.txt: "This is a test"
Permutation Value entered: 5
Output:"his Ts a iest t"