fork download
  1. #include <iostream>
  2.  
  3. int EncryptDecrypt( int Number , bool Encrypting = true ); //you can use std::string also
  4. void Swap( int Container[] , int ElementOneOffset , int ElementTwoOffset ); //swapping two elements
  5.  
  6. int main()
  7. {
  8. int number;
  9. while( std::cout << "Please enter a 4 digit number to encrypt: " && std::cin >> number
  10. && number < 1000 && number > 9999 )
  11. {
  12. std::cout << "Invalid number: " << number << " is not greater than 999 or less than 10000"
  13. << std::endl;
  14. }
  15.  
  16. int Encrypted = EncryptDecrypt( number );
  17. int Decrypted = EncryptDecrypt( Encrypted , false ); //we could simply assign to number
  18. //but we wish to test the decrypt function
  19.  
  20. std::cout << "Starting: " << number << std::endl;
  21. std::cout << "Encrypted: " << Encrypted << std::endl;
  22. std::cout << "Decrypted: " << Decrypted << std::endl;
  23.  
  24. return 0;
  25. }
  26.  
  27. int EncryptDecrypt( int Number , bool Encrypting )
  28. {
  29. const int Digits = 4; //4 digit long number
  30. const int Base = 10; //we are using decimal base
  31. int ReverseDigits[Digits]; //the digits stored in reverse order --will be the e
  32. int Digit = Number % Base; //this will be p and set to right digit of number
  33. int CurrentDigit = 0; //current digit we on , offset of ReverseDigits array
  34. int Increment = 7; //default is encrypt
  35. int Result = 0; //result we return
  36. int Multiplier = 1; //multiplier to the current digit same as 10^digit-1
  37.  
  38. if( !Encrypting ) //decrypting
  39. Increment = -Increment;
  40.  
  41. if( Number < 1000 && Number ) //less than 1000 but not0
  42. {
  43. int Temp = Number;
  44. int TempOffset = Digits - 1;
  45.  
  46. while( Temp < 1000 )
  47. {
  48. ReverseDigits[TempOffset--] = (Increment + Base) % Base; //Digit == 0
  49. Temp *= Base;
  50. }
  51. }
  52. else //Number == 0
  53. {
  54. for( int Offset = 0; Offset < Digits; ++Offset )
  55. {
  56. ReverseDigits[Offset] = (Increment + Base) % Base; //Digit == 0
  57. }
  58.  
  59. }
  60.  
  61. while( Number ) //loop while there are digits left ( Number != 0 )
  62. {
  63. Digit = Number % Base; //Get the next digit
  64. Number /= Base; //Remove the last digit
  65. ReverseDigits[CurrentDigit++] = ( Digit + Increment + Base) % Base;
  66. //We must Add base incase of a negative number while decrypting.
  67. //increment current digit
  68. }
  69.  
  70. //Current order is 4321 we wish to have order 2143 so we must swap a few
  71.  
  72. Swap( ReverseDigits , 0 , 2 ); //Swap ReverseDigits[0] with ReverseDigits[2];
  73. Swap( ReverseDigits , 1 , 3 ); //Swap ReverseDigits[1] with ReverseDigits[3];
  74.  
  75.  
  76. for( int digit = 0; digit < Digits; ++digit )
  77. {
  78. Result += ReverseDigits[digit] * Multiplier;
  79. Multiplier *= Base;
  80. }
  81.  
  82. return Result;
  83.  
  84. }
  85.  
  86. void Swap( int Container[] , int ElementOneOffset , int ElementTwoOffset )
  87. {
  88. int temp = Container[ElementOneOffset];
  89. Container[ElementOneOffset] = Container[ElementTwoOffset];
  90. Container[ElementTwoOffset] = temp;
  91. }
Success #stdin #stdout 0s 3300KB
stdin
1234
stdout
Please enter a 4 digit number to encrypt: Starting: 1234
Encrypted: 189
Decrypted: 1234