Answer to Question #296951 in C++ for Aiza ghafoor

Question #296951

Assignment 2



A company that wants to send data over the Internet has asked you to write a program that will encrypt the data so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a five-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 6 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the fourth, and swap the second digit with the fifth. Then print the encrypted integer.




Write a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number.

1
Expert's answer
2022-02-12T08:22:35-0500

Encrypted program:

#include <iostream>
#include <iomanip>
using namespace std;

const int N=5;

void int2digits(int x, int digits[N]) {
    for (int i=0; i<N; i++) {
        digits[N-1-i] = x%10;
        x /= 10;
    }
}

int digits2int(int digits[N]) {
    int res = 0;
    for (int i=0; i<N; i++) {
        res *= 10;
        res += digits[i];
    }
    return res;
}

void swap(int& a, int& b) {
    int tmp = a;
    a = b;
    b = tmp;
}

int encrypt(int x, int add) {
    int digits[N];

    int2digits(x, digits);
    for (int i=0; i<N; i++) {
        digits[i] += add;
        if (digits[i] >= 10) {
            digits[i] -= 10;
        }
    }
    swap(digits[0], digits[3]);
    swap(digits[1], digits[4]);

    int res = digits2int(digits);

    return res;
}

int main() {
    int x;

    cout << "Enter a five-digits integer ";
    cin >> x;
    int en = encrypt(x, 6);
    cout << "The encrypted integer is " << setfill('0') 
         << setw(5) << en << endl;


    return 0;
}


Decrypted program:

#include <iostream>
#include <iomanip>
using namespace std;

const int N=5;

void int2digits(int x, int digits[N]) {
    for (int i=0; i<N; i++) {
        digits[N-1-i] = x%10;
        x /= 10;
    }
}


int digits2int(int digits[N]) {
    int res = 0;
    for (int i=0; i<N; i++) {
        res *= 10;
        res += digits[i];
    }
    return res;
}

void swap(int& a, int& b) {
    int tmp = a;
    a = b;
    b = tmp;
}

int encrypt(int x, int add) {
    int digits[N];

    int2digits(x, digits);
    for (int i=0; i<N; i++) {
        digits[i] += add;
        if (digits[i] >= 10) {
            digits[i] -= 10;
        }
    }
    swap(digits[0], digits[3]);
    swap(digits[1], digits[4]);

    int res = digits2int(digits);

    return res;
}

int main() {
    int x;

    cout << "Enter a five-digits integer ";
    cin >> x;
    int en = encrypt(x, 4);
    cout << "The dencripted integer is " << setfill('0') 
         << setw(5) << en << endl;

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog