Encrypting

Ok so I know my assignment isn't very hard, but I'm not sure exactly what to do.

This is what the assignment says:
Assume the client encodes information in four-integer groups. You are to write a program to encrypt data as follows:

for a four digit input sequence, for each digit p, the encrypted digit e is generated as
1. e = (p+7) % 10
2. for every sequence e(1)e(2)e(3)e(4), rearrange the digits to e(3)e(4)e(1)e(2).

Call an encryption/decryption function from main(). It should be one function, taking a four digit sequence and a toggle to specify encrypt or decrypt.


This is the code I have so far, let me know what you think I should change or add? I don't know what to with the decryption option, I'm also not sure what the best way to rearrange the digits is.

Thanks in advance!

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
#include <iostream>
#include <cstdlib>

using namespace std;


void encriptFunc (int);

int main(int argc, char** argv) {
	
	int num;
	
	cout << "Enter a four digit integer: ";
	cin >> num;
	
	encriptFunc(num);
	
	return 0;
}

void encriptFunc(int p)
{

    int e = 0;
    int count = 0;
    while (p)
    {
        e = (p+7) %10;
    }
    
   

Last edited on
Call an encryption/decryption function from main(). It should be one function, taking a four digit sequence and a toggle to specify encrypt or decrypt.

so your encriptFunc should contain in the prototype a specifier
the client encodes information in four-integer groups

so int p should be int p[4]
and your encriptFunc have a void type so it does not return so it can't do a thing because after encryption you can't access the encripted code so it should have another parameter (could be -but not recommended here- the same input parameter (int p[4])) like int q[4]
First of all this is an endless loop:
1
2
3
4
    while (p)
    {
        e = (p+7) %10;
    }


Secondly it says P is an individual digit not a string of digits. To get the right most digit you can call p = num % 10; Then after you encrypt that number divide by 10 num /= 10; This will result in the order of 4 3 2 1 instead of 1 2 3 4 though. So remember this when we want to change to order of 3 4 1 2. Also you are probably going to want to store these in an array.


so something like this:

1
2
3
4
5
6
7
8
9
10
const int digits = 4;
int ReverseEncryptedDigits[digits]; //reverse order of digits 4 3 2 1 instead of 1 2 3 4
const int base = 10; //digits in base 10
int p = num % base; //set initial value of p
int offset = 0; //offset in the static array
while( num /= 10 ) //while num != 0
{
    ReverseEncryptedDigits[offset] = ( p + 7 ) % base;
    p = num % base;
}



Then the swapping from 4 3 2 1 to 3 4 1 2 shouldn't be too difficult. I would how ever probably put it in the reverse order of the required to make converting back to an integer easier ( 2 1 4 3 ).

to swap we can call:

1
2
3
temp = ReverseEncryptedDigits[source];
ReverseEncryptedDigits[source] = ReverseEncryptedDigits[destination];
ReverseEncryptedDigits[destination] = temp;


Then to change back to integer we can do something like:

1
2
3
4
5
6
7
8
int result = 0;
int multiplier = 1;

for( int digit = 0; digit < digits; ++digit )
{
    result += ReverseEncryptedDigits[digit] * multiplier;
    multiplier *= base;
}


If you have it in the correct order and not reverse order you will probably want to iterate backwards to make life easier.
Thanks!!
If I make the function an int function then what do I return?
I would say return the encrypted/decrypted value. It does say to pass the integer and a toggle variable to specify encrypt or decrypt.


so the prototype should look like:

int EncryptDecrypt( int number , bool encrypting = true )
or something along those lines.

then you should have the inside like this:

1
2
3
4
5
6
7
8
9
10
if( encrypting )
{
    //doing encrypting stuff
}
else
{
    //doing decrypting stuff
}

return result; //encrypted or decrypted result 
Thanks! I tried putting it all together and it isn't working.. Do you mind looking at my code again to see what I'm doing wrong? Thank you so much for all your help!!

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
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int encriptFunc (int, string);

int main(int argc, char** argv) {
	
	int num;
	string answer;
	
	cout << "Enter a four digit integer: ";
	cin >> num;
	cout << "encrypt or decrypt? ";
	cin >> answer;
	
	encriptFunc(num, answer);
	
	cout << num;
	
	return 0;
}

int encriptFunc(int p, string answer)
{

	if (answer == "encrypt")
	{
    const int digits = 4;
	int RevDigits[digits]; 
	const int base = 10;
	int num, temp, index, source;
	p = num % base; 
	int offset = 0; 
	
	while( num /= 10 ) 
	{
    	RevDigits[offset] = ( p + 7 ) % base;
    	p = num % base;
	}

	temp = RevDigits[source];
	RevDigits[source] = RevDigits[index];
	RevDigits[index] = temp;
        return RevDigits[4];
	}
	
	else
	{
	int RevDigits[4]; 
	int base = 10;
	int result = 0;
	int mult = 1;
	for( int digit = 0; digit < 4; ++digit )
	{
    	result += RevDigits[digit] * mult;
    	mult *= base;
	}
        return RevDigits[4];
	}
	
}
Last edited on
I see a bunch of problems.
1) P is the digit not the number so you have num and p switched.
2) You are trying to return an out of bounds position in the array.
3) You misunderstood when I was said "change" back into an integer I meant to change from array of single digits to an integer of 1 value ( eg 1 2 3 4 to 1234
4) You spell encrypt wrong.

Here is an example ( probably could be done in a much better way with a dynamic container and the algorithm functions.

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
#include <iostream>

int EncryptDecrypt( int Number , bool Encrypting = true ); //you can use std::string also
void Swap( int Container[] , int ElementOneOffset , int ElementTwoOffset ); //swapping two elements

int main()
{
	int number;
	while( std::cout << "Please enter a 4 digit number to encrypt: " && std::cin >> number
	&& number < 1000 && number > 9999 )
	{
		std::cout << "Invalid number: " << number << " is not greater than 999 or less than 10000"
		<< std::endl;
	}
	
	int Encrypted = EncryptDecrypt( number );
	int Decrypted = EncryptDecrypt( Encrypted , false ); //we could simply assign to number
	//but we wish to test the decrypt function
	
	std::cout << "Starting: " << number << std::endl;
	std::cout << "Encrypted: " << Encrypted << std::endl;
	std::cout << "Decrypted: " << Decrypted << std::endl;
	
	return 0;
}

int EncryptDecrypt( int Number , bool Encrypting )
{
	const int Digits = 4; //4 digit long number
	const int Base = 10; //we are using decimal base
	int ReverseDigits[Digits]; //the digits stored in reverse order --will be the e
	int Digit = Number  % Base; //this will be p and set to right digit of number
	int CurrentDigit = 0; //current digit we on , offset of ReverseDigits array
	int Increment = 7; //default is encrypt
	int Result = 0; //result we return
	int Multiplier = 1; //multiplier to the current digit same as 10^digit-1
	
	if( !Encrypting ) //decrypting
		Increment = -Increment;
	
	if( Number < 1000 && Number ) //less than 1000 but not0
	{
		int Temp = Number;
		int TempOffset = Digits - 1;
		
		while( Temp < 1000 )
		{
			ReverseDigits[TempOffset--] = (Increment + Base) % Base; //Digit == 0
			Temp *= Base;
		}
	}
	else  //Number == 0
	{
		for( int Offset = 0; Offset < Digits; ++Offset )
		{
			ReverseDigits[Offset] = (Increment + Base) % Base; //Digit == 0
		}

	}
		
	while( Number ) //loop while there are digits left ( Number != 0 )
	{
		Digit = Number % Base; //Get the next digit
		Number /= Base; //Remove the last digit
		ReverseDigits[CurrentDigit++] = ( Digit + Increment + Base) % Base;
		//We must Add base incase of a negative number while decrypting.
		//increment current digit
	}
	
	//Current order is 4321 we wish to have order 2143 so we must swap a few
	
	Swap( ReverseDigits , 0 , 2 ); //Swap ReverseDigits[0] with ReverseDigits[2];
	Swap( ReverseDigits , 1 , 3 ); //Swap ReverseDigits[1] with ReverseDigits[3];
	
	
	for( int digit = 0; digit < Digits; ++digit )
	{
		Result += ReverseDigits[digit] * Multiplier;
		Multiplier *= Base;
	}
	
	return Result;
	
}

void Swap( int Container[] , int ElementOneOffset , int ElementTwoOffset )
{
	int temp = Container[ElementOneOffset];
	Container[ElementOneOffset] = Container[ElementTwoOffset];
	Container[ElementTwoOffset] = temp;
}
Please enter a 4 digit number to encrypt: 1234
Starting: 1234
Encrypted: 189
Decrypted: 1234

http://ideone.com/5aTkAU
I repeat what I said earlier
so your encriptFunc should contain in the prototype a specifier
the client encodes information in four-integer groups

it is not 4 digits integer, it is 4 integers
int p[4]
Topic archived. No new replies allowed.