Block Cipher Reversal

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"
Last edited on
Observations:

Only the last line of a file will be processed.

Using new[] and then using delete instead of delete[] results in undefined behavior.

The use c-style strings and C++ strings is inconsistent.

Should your output be longer than your input as it is?

If you can clearly describe what you're doing, you can probably describe how to undo it.
The task as hand is not to focused on our coding etiquette, more that we have the understanding of the ciphers, so its ability to work with multiple lines and masses of text do not worry me too much, as I have limited time to undo what I already


Provided that the output is back in it original order the additional whitespace doesn't worry me either, I'm assuming you're talking about the code that adds white space if the there are still character in the FullText variable?

well I've discovered with a bit of playing within the program that, if I allow the user to define the value of "count" with another input, and "size" via another input, that the value of count must always be lower than the value of "size" (logically) and that if I encrypt something for example using "count =3" and "size = 7" the value needed to return the string back to its original form is size-count, so "count = 4" when trying to "decrypt"

But if I try to double up on the encryption with different value, it all screws up and I think this is because of the white space, but only seems to happen when the initial input values are lower than the second input eg,
1st run
string: "This is a test"
input: "C = 1" "S=4"
output "hisT s ai estt "

2nd run
string: "hisT s ai estt "
input: "C=3" "S=5"
output "T hisi s at est"

Now to "decrypt"

1st run: "T hisi s at est"
input: "C=2" "S=5"
output "hisT s ai estt "

2nd run: "hisT s ai estt "
input: "C=4" "S=5"
output: "This is a test "

I think I've just solved my issues writing this out, and realised there are certain numerical rules I'm going to have to put in place, I've edited my code so that the Block created in "makeSection" is a separate input to count and size, and provided that size is always lower than the block input, it works no matter how many times,

But I've discovered also that with the test sentence "This is a test" I can't go above "7"for the block size

Also what would be the best way to remove the white space once decrypted? The white space could actually be useful to me while the file is encrypted
Topic archived. No new replies allowed.