Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java Program 2 Affine Cipher An affine cipher is an old encryption scheme that encrypts messages using the following function: ek(x) = (ax +

In Java Program 2 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 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. For example, an input file may contain the following: df13051a3d36cd28ea4fbfe9cd0cbfcdeae92fea 0c13ea051328cdeab1b8133d36eabf13051a3d36 cd282fea36e2b10ceab12f3628130c130559eae9 2feab1b8133d36ea36cdfecd2fbf131acd2f4c which, if successfully brute-forced should produce output that looks something like the following. Message: Computer Science is no more about computers than astronomy is about telescopes. Affine Cipher Keys -=-=-=-=-=-=-=-=-=- a = 7 a^-1 = 183 b = 10 If using Java, your program should be runnable from the command line as: java Affine encrypted.txt and output the results to the standard output. If using PHP it should be invokable from the command line as: php affine.php encrypted.txt and output the results to the standard output

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

116 $ 678.19 $ 106767.24 117 $ 685.76 $ 107953.00 118 $ 693.37 $ 109146.37 119 $ 701.04 $ 110347.41 120 $ 708.75 $ 111556.16 Total Interest Earned: $ 41556.16 Total Nest Egg: $ 111556.16 2. Program 2 - Afline Cipher An affine cipher is an old encryption scheme that encrypts messages using the following function: ex (2) = (ar + b) mod n Where n is an integer representing the size of our character set with 0 Sa,b, x 5n-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 ex(x) = (7.+10) mod 256 To encrypt "Hello" we would use the values 72, 101, 108, 108, and 111 and encrypt each one as: ex(72) = (7.72 +10) mod 256 = 2 ex (101) = (7.101 + 10) mod 256 = 205 ex (108) = (7.108 + 10) mod 256 = 254 ex(108) = (7.108 + 10) mod 256 = 254 ex (111) = (7.111 + 10) mod 256 = 19 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, we'll 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, dz(y) = (a-.(y-b)) mod n where a is the inverse of a modulo n. The inverse of an integer a is the unique value a- such that (a.a-) mod n = 1 In our example, with a = 7 and n = 256, the inverse, 7-1 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. So to decrypt the first letter above, we would compute? d. (2) = (183. (2 - 10)) mod 256 = 72 Given a and n, how can we find an inverse, a-'? 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,a-'), but we will provide a way to programmatically check that you've 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, ab. For example, an input file may Unt (1832 TUJ mod 25672 Given a and n, how can we find an inverse, a-l? 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,a-'), but we will provide a way to programmatically check that you've 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,a-! For example, an input file may contain the following: df13051a3d36cd28ea4fbfe9cd0cbfcdeae92fea Oc13ea051328cdeab1b8133d36eabf13051a3d36 cd282fea36e2b10ceab12f3628130c130559eae9 2feab1b8133d36ea36cdfecd2fbf131acd2f4c To have an inverse, a must be relatively prime to n; i.e. geda,n) = 1 2 Take care, as negative values should "wrap around" back to positive ones, -8 mod 256 = 248 for example. However, some programming languages' modulo operator may result in negative values and you may need to correct for this. Page 4 es which, if successfully brute-forced should produce output that looks something like the following: Message: Computer Science is no more about computers than astronomy is about telescopes. Affine Cipher Keys -= -= - = -= - = - = - = - = - =- a = 7 a -1 = 183 b = 10 If using Java, your program should be runnable from the command line as: java Affine encrypted.txt and output the results to the standard output. If using PHP it should be invokable from the command line as: php affine.php encrypted.txt and output the results to the standard output

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions