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 |