fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int crypt(int v, bool encrypt) {
  7. if (v<0 || v>9999) return -1;
  8.  
  9. int d = encrypt? 7 : 3;
  10.  
  11. char digits[4];
  12.  
  13. for(int i=3; i>=0; i--,v/=10) digits[i] = (v % 10 + d) % 10;
  14.  
  15. swap(digits[0],digits[2]); swap(digits[1],digits[3]);
  16.  
  17. int r = 0;
  18. for(int i=3, m=1; i>=0; i--,m*=10) r += digits[i] * m;
  19.  
  20. return r;
  21. }
  22.  
  23. int main() {
  24. int e = crypt(8765, true); //encrypt
  25. int d = crypt(e, false); //decrypt
  26.  
  27. cout <<
  28. "Encrypted:\t" << setw(4) << setfill('0') << e << endl <<
  29. "Decrypted:\t" << d << endl;
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 4664KB
stdin
Standard input is empty
stdout
Encrypted:	3254
Decrypted:	8765