Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(JAVA) OPTION A: InternetUser, Communicator and Two Helper Classes Understand the Classes and Problem Every internet user -- perhaps better thought of as an Internet

(JAVA)

OPTION A: InternetUser, Communicator and Two Helper Classes

Understand the Classes and Problem

Every internet user -- perhaps better thought of as an Internet connection -- has certain data associated with him/her/it. We will oversimplify this by symbolizing such data with only two fields: a name ("Aristotle") and a globally accessible IP address ("139.12.85.191"). We could enhance these by adding other -- sometimes optional, sometimes needed -- data (such as a MAC address, port, local IP address, gateway, etc), but we keep things simple and use only these two for the assignment -- any more would not satisfy spec. Also, name is not technically a true datum for this emulation, but we use it to signify the the English description of an individual or process that is interacting with the Internet.

Most internet users, however, need more -- they want to communicate securely with other internet users. For example, Bob (bob, "139.12.85.191") might want to order something from Amazon (amazon, "54.239.26.128"), a transaction that requires Bob to transmit a credit card number to Amazon over the Internet. Today, this is done using a two-key (public key + private key) system called an RSA cryptosystem. You don't see it, but it's always there. Such a special internet user could be called a communicator. To use RSA encryption, every internet userwould need a pair of keys, the public-private key pair, above and beyond their basic name/ip address data. Everyone gets to see everyone else's public key, but the private key is only known to the one user: Bob's connection knows his private key, and Amazon's connection knows its private key. But Bob will know Amazon's public key and Amazon will know Bob'spublic key.

We will not be encrypting actual messages this week, but perhaps we will do so in a later assignment. For those of you interested in a more complete explanation of RSA encyption, you can start here:

Wikipedia page for RSA (cryptosystem) (Links to an external site.)Links to an external site.

Using OOP vocabulary and tools, a communicator would then consist of a normal internet user plus at least one other item: a key pair, that contains two keys: the public key and the private key.

Enter inheritance.

Two Helper Classes: IntPair and EncryptionSupport

I will supply you with two support classes, as follows.

Class IntPair

This class consists of two integers bundled together for easy handling. Examples of IntPair objects are (32, 195), (-983, 0) and (1000, 212078). As you can see, there is a first integer and a second integer, which we'll call firstInt and secondInt. Everything is public and no filtering is involved, so that our classes can get to the data easily and quickly. Details will be found in the text file to be downloaded, below.

Class EncryptionSupport

This class has no member data, only four static methods that will be used by our Communication class methods. Since they are general enough, we encapsulate them in a separate class. For example one of its methods, isPrime( long x), determines whether or not the passed parameter, x, is a prime number, a useful method for many other classes. The methods of the class can be examined in the source, but they need not be fully absorbed or analyzed for the purpose of this assignment. You can simply call them and expect that they do the right thing relative to the methods that you will write.

Both classes and the imports for the assignment can be downloaded here:

/*------------------------------------------------------------------------ Two classes used by assignment #4. Do not modify these defintions.

Place the definitions into your final submission, with each element appearing above the main Foothill class, and your own classes below Foothill.

The imports are needed by my solution and will probably be needed by yours, so you should place them in your project at the top of the file(s). ----------------------------------------------------------------------- */ import java.lang.*; import java.util.*;

// IntPair allows public, no filtering; classes that use it will protect it class IntPair { public long firstInt; public long secondInt;

// constructors IntPair() { firstInt = secondInt = 0; } IntPair(long frst, long scnd) { firstInt = frst; secondInt = scnd; } public String toString() { return "(" + firstInt + ", " + secondInt + ")"; } };

//EncryptionSupport contains only static methods that clients can use wherever //all method names should be fairly descriptive other than inverseMonN(), which //you can take as a black-box (see description of assignment) class EncryptionSupport { static private Random randObject = new Random(System.currentTimeMillis()); public static boolean isPrime(long x) { long k, loopLim;

if (x < 2) return false; if (x < 4) return true; if (x % 2 == 0 || x % 3 == 0) return false;

// now use the fact the all primes of form 6k +/- 1 loopLim = (long)Math.sqrt(x); for (k = 5; k <= loopLim; k += 6) { if (x % k == 0 || x % (k + 2) == 0) return false; } return true; } public static long inverseModN(long a, long n) { // uses extended euclidean algorithm giving as + nt = gcd(n, a), // with gcd(n, a) = 1, and s, t discovered. s = 1/a, and t ignored

long s, t, r, sPrev, tPrev, rPrev, temp, q, inverse;

// special key encryption conditions; we will pick some prime e >= 3 for a if (a < 3 || a >= n || !isPrime(a)) return 0; // error

// we are now guaranteed 3 <= a < n and gcd(a, n) = 1;

// initialize working variables s = 0; t = 1; r = n; sPrev = 1; tPrev = 0; rPrev = a;

while (r != 0) { q = rPrev / r;

temp = r; r = rPrev - q * r; rPrev = temp;

temp = s; s = sPrev - q * s; sPrev = temp;

temp = t; t = tPrev - q * t; tPrev = temp; }

inverse = sPrev % n; if (inverse < 0) inverse += n; return inverse; } public static long getSmallRandomPrime() { int index; long lowPrimes[] = { 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541 }; long arraySize = lowPrimes.length;

// pick prime in the above array bet 0 and arraySize - 1 index = (int)( randObject.nextDouble() * arraySize );

return lowPrimes[index]; } public static long getMedSizedRandomPrime() { int index; long medPrimes[] = { 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, }; long arraySize = medPrimes.length;

// pick prime in the above array bet 0 and arraySize - 1 index = (int)(randObject.nextDouble() * arraySize );

return medPrimes[index]; } }

The Base Class: InternetUser

Your base class is named InternetUser.

Public Static Class Constants

Define a full set of limits and defaults, like MAX_NAME_LENGTH, MIN_IP_LENGTH , and DEFAULT_IP for both min, max lengths and default data of each member. names should be between 2 and 50 characters, and IP addresses between 7 and 15 characters. The default name should reveal an undefined string, and the default IP address should be "0.0.0.0", also an indicator of an error or undefined address to the user.

Private Member Data

 String name; String ip; 

Public Methods

Default and 2-parameter constructors.

Mutator and Accessor for each member.

a toString() method that provides a nicely formatted return string for potential screen I/O.

Private Methods

private static validation helpers to filter client parameters. These will support your public methods. You should only test lengths here, since we don't really need to check the number of periods or size of each of the four ints in an IP address. We'll let that slide for this assignment. (If you don't know what an IP address looks like, now's a good time to Google it. We're using IP4, not IP6, format.)

Client Test of Class InternetUser (first part of main())

Instantiate two or more InternetUser objects, some using the default constructor and some using the parameter-taking constructor. Mutate one or more of the members and, after that, use the toString() to assist a screen output so we can see what your objects contain. Next, test one or more accessors. Finally, call two or more mutators and constructors, providing both legal and illegal arguments and checking the return values (thus demonstrating that the mutators do the right thing). Here is a sample run from my test (but you will have to imagine/deduce the source I used and create your own source for your testing, which will produce a different output).

Example Run of InternetUser Class Testing

/* --------------------------------------------------------- Base Class Testing *********************** Name: (undefined) IP Aaddr: 0.0.0.0 Name: dns IP Aaddr: 75.75.75.75 Name: bad IP Aaddr: 0.0.0.0 success as expected --------------------------------------------------------- */ 

The Derived Class: Communicator

Your derived class is named Communicator. Communicator uses the two members already present in the base class; the Communicator's name is the name in the base class, and the ipof Communicator is also the base class -- InternetUser's -- ip. Do not try to store a Communicator's name or ip as new members of the derived class or you will get -20 points -- not desirable by you or me. Duplicating base class data in derived class means you do not understand inheritance, thus the significance of this kind of mistake.

Overview of RSA Encryption Keys and Class Communicator

The Fundamental Data

As we already learned, to encrypt a message, each InternetUser has to also be a Communicator, and to do that, the object has to have more than just a name and an ip; it has to have two encryption keys, a public key and a private key. So the most fundamental aspect of this class are those keys. Each key is a pair of integers, or in our current regime, each key is an IntPairmember object:

 IntPair publicKey; IntPair privateKey; 

Two examples of such pairs are

 publicKey: (23, 703) privateKey: (214, 703)

and

 publicKey: (29, 957779) privateKey: (726591, 957779)

Notice two things about the pair:

The second number in the pair is the same, and

the first number in the pair is a prime number.

While the sameness of the second number is normally considered a bad duplication of data, we'll allow it in RSA emulation since it is part of the formal definition of the two keys and we want to parallel the math as closely as possible.

These two keys will be generated for us with the help of our EncryptionSupport class, so you needn't get too wrapped up in the meaning ... yet. For now, I just want you to understand what the main data for this derived class is, and you now know that. So we move on.

Client-Supplied Constructor or Mutator Values

The client does not actually provide the two integers for these keys. Instead, the client has to only do one thing: select and supply two distinct prime numbers. Any two relatively large primes will do. We'll call the two prime numbers, supplied by the client, p and q, informally, or as members firstPrime and secondPrime. So p = firstPrime and q = secondPrime. I'll use p and qinformally when talking about the two primes, but they are the same as the members firstPrime and secondPrime.

Our constructors and mutators have to check that the client-supplied p and q arguments are prime and distinct and, if so, the constructors must generate the two encryption keys for us using those primes as a foundation. Neither prime is actually part of either encryption key.

It's easy to pick two primes. It's a little harder to produce legal public and private keys, which is why we allow the client to give us two random primes and then use them to build two encryption keys (with the help of the provided methods and the following steps), one public and one private. You'll see the two client-supplied primes, p and q, in the parameter list of some public methods. You'll also see some code that generates the encryption keys using p and q as a starting point. Read on.

Static Class Constants (Both final)

To the existing base class statics, add two more in this derived class:

ERROR_FLAG_NUM - public. Make this equal 0.

MAX_PQ - private. It means the maximum of p as well as the maximum of q (our two members firstPrime and secondPrime). The value to be assigned by your class is the (positive) square root of the largest long in your compiler. That can be found by taking the Java constant symbol, Long.MAX_VALUE defined inside java.util, then taking its square root.

Additional Private Member Data

We already talked about these two:

 IntPair publicKey; IntPair privateKey;

In addition, we need several support long ints:

 long firstPrime; long secondPrime; long n, phi, e, d; 

These will be helpful intermediate variables during the computation of the keys. Normally, we don't allow mere intermediate variables to be awarded the status of class member (see your CS 1A notes) but these have very special meanings and are intrinsic to the process of key encryption.

Public Methods

Four constructors. Use constructor chaining. Here are the signatures:

 Communicator(); Communicator( long firstPrime, long secondPrime ); Communicator( String name, String ip ); Communicator( String name, String ip, long firstPrime, long secondPrime );

Constructors that receive illegal numeric values as reported by the mutator return value tested in the constructor (see requirements of p, q, above) will assign ERROR_FLAG_NUM to all numeric members. Meanwhile, String errors are handled already by chaining to your previsously designed and tested base class constructor. However, if legal numeric arguments are reported by the mutator nothing more need be done:; the mutator will, by the time it returns from the mutator call, have already set all private numeric data based on the two passed and valid primes.

One Mutator for both primes. This is the only simple mutator, and it takes both primes, p and q, because they must be compared to each other. If good values are detected it setsfirstPrime and secondPrime and goes on to call computeBothEncrKeys() to set the keys so they are compatible with these two new valid primes just set. Details, below. Other additional sub-class members members cannot be mutated directly and are not to be readable by clients. You have a method that can be used as a validator already in your EncryptionSupport class, so use that rather than writing a new one. There is more to this than just calling that method. The description above clearly states what that remaining test for p and q is, and can be done in the mutator.

Accessors - one for each key. Only the encryption keys (which are IntPair type) have accessors. Remember, accessor return type always agrees with that of the member.

toString() - a method that returns a String of the entire object's private data in a readable form. Make proper use of method chaining.

Private Methods

boolean computeBothEncrKeys() - This is the method that uses the primes to build encryption keys. It takes the following action (this is how RSA encryption key generation is accomplished):

Generate n = p * q;

Generate phi = ?(n) which is the number of primes smaller than n. ?(n) is called "Euler's totient function," and in the special case where n is the product of two primes (but not generally), ?(n) is simply (p - 1) * (q - 1). [This n becomes the basis for our modular arithmetic coming next, that is, we are about to compute values "mod n," a concept that only those of you who have had upper-division algebra will know. You do not need to know the details since the EncryptionSupport methods will do the work. Read on.]

Compute e. Select a random prime number that is relatively small: between 19 and 541 that has the property that it is LESS THAN phi and it DOES NOT DIVIDE EVENLY into phi. getSmallRandomPrime() in your EncryptionSupport class will give you a prime in the correct range, but you have to make sure that the two latter conditions are met and keep at it until they are, or after a reasonable attempt, declare defeat and return false immediately (some member data will have been changed, but that's okay). If you succeed, the result is the value you assign to member e, and you will return true at the end of the method.

Compute d as the mod n multiplicative inverse of the e just computed. This is done for you by the method inverseModN(e, n) in your EncryptionSupport class.

Set the public key IntPair to (e, n) and the private key IntPair to (d, n).

Return true.

Client Test of Class Communicator (second part of main())

Instantiate four or more Communicator objects, testing each of the four constructors. Give both base class and derived class bad data to some of these. Confirm the operation by showing the objects immediatly. Next, mutate one or more of the objects by calling the one and only mutator, and re-displaying the results. Finally, test your accessors.

Example Test of Communicator Class (done in same run as overall program run)

Derived Class Constructor Testing *************** ---------------- Name: (undefined) IP Aaddr: 0.0.0.0 (p, q) n, phi, e, d: (0, 0) 0, 0, 0, 0 public key(0, 0) private key(0, 0) ---------------- Name: salim lakhani IP Aaddr: 10.1.10.1 (p, q) n, phi, e, d: (0, 0) 0, 0, 0, 0 public key(0, 0) private key(0, 0) ---------------- Name: yan kam IP Aaddr: 127.90.32.14 (p, q) n, phi, e, d: (0, 0) 0, 0, 0, 0 public key(0, 0) private key(0, 0) ---------------- Name: sally IP Aaddr: 0.0.0.0 (p, q) n, phi, e, d: (19, 37) 703, 648, 463, 372 public key(463, 703) private key(372, 703) ---------------- Name: betty IP Aaddr: 1.1.1.1 (p, q) n, phi, e, d: (0, 0) 0, 0, 0, 0 public key(0, 0) private key(0, 0) Derived and Base Class Mutator Testing ********** ---------------- Name: giral IP Aaddr: 0.0.0.0 (p, q) n, phi, e, d: (809, 821) 664189, 662560, 463, 642671 public key(463, 664189) private key(642671, 664189) (more supplied by student) (other tests supplied by student) --------------------------------------------------------- */ 

OPTION B1 - A Stack of InternetUsers (4 Points Extra Credit)

Complete, for submission, OPTION A. Add StackNode and Stack classes, exactly as presented in the Modules. Next, derive IuNode and IuStack classes in the same manner as we derived FloatNode and FloatStack in the modules, but making adjustments, as needed, to have the IuStack work with InternetUsers. Change most methods named showXYZ() by converting them to toString() so that the client, not the methods, does the output. The new methods merely format the strings in preparation for output.

In your client, after you have done the things from OPTION A, design a second main() to create an IuStack. Then push() various InternetUsers onto the stack

Finally, in a loop, pop() everything off the IuStack and print it out as you pop(). Go beyond the end of the Stack so you can confirm that your code does not break when you pop() things off an empty Stack.

Once your assignment is graded and returned, you can view the instructor solution here:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Intelligent Information And Database Systems 12th Asian Conference ACIIDS 2020 Phuket Thailand March 23 26 2020 Proceedings

Authors: Pawel Sitek ,Marcin Pietranik ,Marek Krotkiewicz ,Chutimet Srinilta

1st Edition

9811533792, 978-9811533792

More Books

Students also viewed these Databases questions

Question

denigration of emotional outbursts; being reserved;

Answered: 1 week ago