-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.35-dec.cpp
More file actions
43 lines (33 loc) · 1.03 KB
/
4.35-dec.cpp
File metadata and controls
43 lines (33 loc) · 1.03 KB
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
// This implements the decryption scheme from exercise 4.35
#include <iostream>
using namespace std;
int main(){
int inputNumber;
cout << "Enter 4-digit number to decrypt: " << endl;
cin >> inputNumber;
// the 4 digits of the number separated
int digit1, digit2, digit3, digit4;
// 1 2 3 4
// digit1 digit2 digit3 digit4
digit4 = (inputNumber / 1)% 10;
digit3 = (inputNumber / 10) % 10;
digit2 = (inputNumber / 100) % 10;
digit1 = (inputNumber / 1000) % 10;
// encryption algorithm
digit1 = (digit1 + 10 - 7) % 10;
digit2 = (digit2 + 10 - 7) % 10;
digit3 = (digit3 + 10 - 7) % 10;
digit4 = (digit4 + 10 - 7) % 10;
int temp;
// swap digit1 and digit3
temp = digit1;
digit1 = digit3;
digit3 = temp;
// swap digit2 and digit4
temp = digit2;
digit2 = digit4;
digit4 = temp;
int encryptedNumber = digit1*1000 + digit2*100 + digit3*10 + digit4;
cout << "The decrypted number is: " << encryptedNumber << endl;
return 0;
}