Answer to Question #181576 in Python for Usama Ali

Question #181576

A company that wants to send data over the Internet has asked you to write a program in Python 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:

a)     Replace each digit with the result of adding 6 to the digit and getting the remainder after dividing the new value by 10.

b)    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 (in Python) that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number.


1
Expert's answer
2021-04-14T19:07:17-0400
def swap_digits(digit1, digit2):
    return digit2, digit1


def encrypt_digit(digit):
    return (digit+6)%10


def decrypt_digit(digit):
    return (digit-6)%10


def get_digits(number):
    first_digit = int(str(number)[0])
    second_digit = int(str(number)[1])
    third_digit = int(str(number)[2])
    fourth_digit = int(str(number)[3])


    return first_digit, second_digit, third_digit, fourth_digit
    


# encryption
#---------------------------------------------------------------------------------------------------
number = int(input("Enter four-digit number: "))


first_digit, second_digit, third_digit, fourth_digit = get_digits(number)


first_digit = encrypt_digit(first_digit)
second_digit = encrypt_digit(second_digit)
third_digit = encrypt_digit(third_digit)
fourth_digit = encrypt_digit(fourth_digit)


first_digit, third_digit = swap_digits(first_digit, third_digit)
second_digit, fourth_digit = swap_digits(second_digit, fourth_digit)


encrypted_number = int(str(first_digit)+str(second_digit)+str(third_digit)+str(fourth_digit))


print("Encrypted number: ", encrypted_number)
print()
#---------------------------------------------------------------------------------------------------


# decryption
#---------------------------------------------------------------------------------------------------
encrypted_number = int(input("Enter encrypted four-digit number: "))


first_digit, second_digit, third_digit, fourth_digit = get_digits(encrypted_number)


first_digit, third_digit = swap_digits(first_digit, third_digit)
second_digit, fourth_digit = swap_digits(second_digit, fourth_digit)


first_digit = decrypt_digit(first_digit)
second_digit = decrypt_digit(second_digit)
third_digit = decrypt_digit(third_digit)
fourth_digit = decrypt_digit(fourth_digit)


decrypted_number = int(str(first_digit)+str(second_digit)+str(third_digit)+str(fourth_digit))


print("Decrypted number: ", decrypted_number)
#--------------------------------------------------------------------------------------------------

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