Question: Please write in Java not C Affine Cipher An affine cipher is an old encryption scheme that encrypts messages using the following function: ek(x) =

Please write in Java not C

Affine Cipher An affine cipher is an old encryption scheme that encrypts messages using the following function: ek(x) = (ax + b) mod n Where n is an integer representing the size of our character set with 0 a, b, x n 1. For this exercise we will use the extended ASCII character set so n = 256. To encrypt a message, we choose two integers a, b to define the encryption function. For example, if a = 7 and b = 10 then ek(x) = (7x + 10) mod 256 To encrypt Hello we would use the values 72, 101, 108, 108, and 111 and encrypt each one as: ek(72) = (7 72 + 10) mod 256 = 2 ek(101) = (7 101 + 10) mod 256 = 205 ek(108) = (7 108 + 10) mod 256 = 254 ek(108) = (7 108 + 10) mod 256 = 254 ek(111) = (7 111 + 10) mod 256 = 19 Page 3 However, when mapped back to the extended ASCII character set, this results in some non-printable characters. So, to ensure that it is still a plaintext file, well use hex values of the ASCII text values instead. The encrypted value would thus be: 02 cd fe fe 13 (spaces added for readability and should be ignored in general) To decrypt a message we need to invert the decryption function, dk(y) = a 1 (y b) mod n where a 1 is the inverse of a modulo n. The inverse of an integer a is the unique value a 1 such that (a a 1 ) mod n = 1 In our example, with a = 7 and n = 256, the inverse, 71 mod 256 is 183 since 7 183 mod 256 = 1. Note, however, that an inverse may not exist. In this particular case, no even number a will have an inverse.1 So to decrypt the first letter above, we would compute2 dk(2) = (183 (2 10)) mod 256 = 72 Given a and n, how can we find an inverse, a 1 ? Obviously it cannot be zero, nor can it be 1 (1 is its own inverse). There is a simple algorithm (the Extended Euclidean Algorithm) that can solve this problem, but n = 256 is small enough that a brute-force strategy of testing all possibilities will suffice (or you can precompute and store all inverse pairs if you wish). Write a program that takes a file name as a command line argument which contains a message encrypted with an affine cipher in hexadecimal format (ignore all whitespace). We will not provide the encryption or decryption keys (a, b, a1 ), but we will provide a way to programmatically check that youve decrypted the message: the decrypted message will always contain the string Computer Science somewhere in the file. Your task will be to brute-force decrypt the message and print it to the standard output along with the discovered encryption keys, a, b, a1 .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!