Answer to Question #254603 in C for Jack

Question #254603
A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 5 to the digit and getting the remainder after dividing the new value by 8. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write a separate application that inputs an encrypted
1
Expert's answer
2021-10-21T16:06:22-0400

Source code

#include <stdio.h>


int encrypt( int num){
	int n1 = num / 1000;
	int n2 = (num % 1000) / 100;
	int n3 = (num % 100) / 10;
	int n4 = num % 10;
	n1 = (n1 + 5) % 8;
	n2 = (n2 + 5) % 8;
	n3 = (n3 + 5) % 8;
	n4 = (n4 + 5) % 8;
	return (n3 * 1000 + n4 * 100 + n1 * 10 + n2);    
}


int decrypt( int num)
{
	int n1 = num / 1000;
	int n2 = (num % 1000) / 100;
	int n3 = (num % 100) / 10;
	int n4 = num % 10;
	n1 = (n1 + 3) % 8;
	n2 = (n2 + 3) % 8;
	n3 = (n3 + 3) % 8;
	n4 = (n4 + 3) % 8;
	return (n3 * 1000 + n4 * 100 + n1 * 10 + n2);
}


int main()
{
	int num;
	printf("Enter a four digit number: ");
	scanf("%d",&num);
	printf("The number after encryption is: %d\n",encrypt(num));
	printf("The encrypted number after decryption is: %d",decrypt(encrypt(num)));
}


Output


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
APPROVED BY CLIENTS