Answer to Question #149159 in HTML/JavaScript Web Application for mohammed

Question #149159
A company wants to transmit data over the telephone, but it is concerned that its phones may be tapped. All of its data is transmitted as four-digit integers. It has asked you to write a program that will encrypt its data so that the data may be transmitted more securely. Your program should read a four-digit integer entered by the user in a prompt dialog and encrypt it as follow: Replace each digit by (the sum of that digit plus 7) modulus 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then display the encrypted integer. Write a program to decrypt the encrypted data.
1
Expert's answer
2020-12-06T10:23:51-0500

encoder:

var int = window.prompt("Enter an four-digit integer: ");


var digits = int.toString(10).split('').map(Number);


for (var i = 0; i < digits.length; i++) {
	digits[i] = (digits[i]+7) % 10;
}


var temp = digits[0];
digits[0] = digits[2];
digits[2] = temp;
temp = digits[1];
digits[1] = digits[3];
digits[3] = temp;


window.alert("Encrypted integer: " + digits.join(''));

decoder:

var enc = window.prompt("Enter encrypted integer: ");


var digits = enc.toString(10).split('').map(Number);


var temp = digits[0];
digits[0] = digits[2];
digits[2] = temp;
temp = digits[1];
digits[1] = digits[3];
digits[3] = temp;


for (var i = 0; i < digits.length; i++) {
	if (digits[i] >= 7) {
		digits[i] = digits[i] - 7;
	} else {
		digits[i] = digits[i] + 3;
	}
}


window.alert("Decrypted integer: " + digits.join(''));

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