Open Collections

UBC Theses and Dissertations

UBC Theses Logo

UBC Theses and Dissertations

A three-tiered java application for secure transactions over internet Siddiqui, Ghazala Yasmeen 1999

Your browser doesn't seem to have a PDF viewer, please download the PDF to view this item.

Item Metadata

Download

Media
831-ubc_1999-0381.pdf [ 4.51MB ]
Metadata
JSON: 831-1.0051613.json
JSON-LD: 831-1.0051613-ld.json
RDF/XML (Pretty): 831-1.0051613-rdf.xml
RDF/JSON: 831-1.0051613-rdf.json
Turtle: 831-1.0051613-turtle.txt
N-Triples: 831-1.0051613-rdf-ntriples.txt
Original Record: 831-1.0051613-source.json
Full Text
831-1.0051613-fulltext.txt
Citation
831-1.0051613.ris

Full Text

A THREE-TIERED JAVA APPLICATION FOR SECURE TRANSACTIONS OVER INTERNET By Ghazala Yasmeen Siddiqui B.Eng. N.E.D.University of Engg. and Technology, 1991 M.Sc., California Institute of Technology, 1992 A THESIS SUBMITTED IN PARTIAL FULFILMENT OF THE REQUIREMENTS FOR THE DEGREE OF MASTER OF SCIENCE in THE FACULTY OF GRADUATE STUDIES COMPUTER SCIENCE We accept this thesis as conforming to the required standard THE UNIVERSITY OF BRITISH COLUMBIA June 1999 © Ghazala Yasmeen Siddiqui, 1999 In presenting this thesis in partial fulfillment of the requirements for an advanced degree at the University of British Columbia, I agree that the Library shall make it freely available for reference and study. I further agree that permission for extensive copying of this thesis for scholarly purposes may be granted by the head of my department or by his or her representatives. It is understood that copying or publication of this thesis for financial gain shall not be allowed without my written permission. Computer Science The University of British Columbia 2366 Main Mall Vancouver, Canada V6T 1Z3 Date: Abstract This thesis describes the design, development and implementation of a three-tiered (client/server/database) architecture in which data is transmitted securely, providing both secrecy and authentication. The architecture uses a network security feature called a Key Distribution Center. The system is capable of serving multiple users at a time. A prototype has been developed in the Java programming language. The prototype operates in a PC environment in the Protocol Laboratory in the Computer Science department. Table of Contents Abstract ii List of Figures viii Chapter 1 Introduction 1 1.1 Objective 1 1.2 Scope of Work 1 1.3 Secure Three-tier Architecture 1 1.4 Thesis Contribution 3 1.5 Outline of Thesis 6 Chapter 2 Background 7 2.1 Description of a Three-tier Architecture 7 2.1.2 Advantages of Three-tier Architecture 9 2.1.2.1 Control 10 2.1.2.2 Reliability 10 2.1.2.3 Scalability and Performance 10 2.1.2.4 Flexibility, Growth and Change 11 2.2 Data Security 11 2.2.1 Security Services 11 2.2.2 Security Attacks 12 i i i 2.2.3 Passive Attacks 13 2.2.4 Active Attacks 13 2.2.5 Symmetric Encryption 14 2.2.6 Message Authentication 14 2.2.7 Asymmetric Encryption 16 2.2.8 Key Distribution 17 Chapter 3 Prototype Application 19 3.1 Overview 19 3.2 Communication Protocol 19 3.3 Database Connection 19 3.3.1 JDBC-ODBC Bridge plus ODBC driver 20 3.4 System Platform 20 3.5 Description of Application 21 3.6 Encryption Algorithms 23 3.7 Key Distribution 24 Chapter 4 Java Classes •••• 26 4.1 Class PacketType 26 4.2 Client Classes 26 4.2.1 Jclient 26 4.2.2 JConn 27 4.2.3 JDecoder 27 iv 4.2.4 JDBclient 28 4.2.5 show_pop 28 4.2.6 show_print 28 4.3 Java Server Classes 28 4.3.1 mainserver 29 4.3.2 Jserver 29 4.3.3 JEncoder 29 4.3.4 DBserver 30 4.4 Key Distribution Center Classes 30 4.4.1 mainkey ..: 30 4.4.2 JKey 30 4.4.3 JTicket 31 Chapter 5 Mechanics of a Transaction 32 5.1 Overview 32 5.2 Login Procedure 32 5.2.1 Establishing Connection 32 5.2.2 Verification of Password 35 5.2.3 Exchange of Session and Public keys 36 5.2.4 Loading Menus 40 5.3 Running the Application 41 5.4 Logout or Quit 44 V Chapter 6 Conclusion and Future Work 45 6.1 Conclusion 45 6.1.1 Security against Passive Attacks 45 6.1.2 Security against Active Attacks 45 6.1.3 Centralized Security Management 46 6.1.4 Single Point Authentication 46 6.1.5 Customized Security 47 6.1.6 Scalability 47 6.1.7 Transparency 47 6.1.8 Balanced Network 47 6.2 Suggestions for Future Work 48 6.2.1 Web-based Solution 48 6.2.2 Universal Personal Computing 48 References 50 Appendix 1 Class PacketType 52 Appendix 2 Class Jclient 54 Appendix 3 Class JConn 63 Appendix 4 Class JDecoder r 77 Appendix 5 Class JDBclient 81 Appendix 6 Class show_pop 83 vi f Appendix 7 Class show_print 88 Appendix 8 Class mainserver 90 Appendix 9 Class Jserver 92 Appendix 10 Class JEncoder 105 Appendix 11 Class DBserver '. 110 Appendix 12 Class mainkey 124 Appendix 13 Class JKey 125 Appendix 14 Class JTicket 133 vii List of Figures 1.1 Three-tier Architecture 3 1.2 Three-tier architecture serving Multiple Clients 4 1.3 Three-tier architecture with Multiple Clients 5 2.1 Three-tier model (left) and Two-tier model (right) 9 2.2 Simplified Model of Symmetric Encryption 14 2.3 Message Authentication using a Message Digest 15 2.4 Simplified Model of Public-Key Encryption 16 2.5 Key Distribution Center for Connection-Oriented Protocol 17 3.1 JDBC Components 21 5.1 Login Screen 34 5.2 Sequence of communication commands to establish connection 35 5.3 Delivering Java Server's Public Key 37 5.4 Delivering Shared Session Key 38 5.5 Delivering Client's Public Key 39 5.6 Menu for Clients with "admin" Authorization Level 40 5.7 Menu for Clients with "user" Authorization Level 41 5.8 Display of Query Results 43 viii Chapter 1 Introduction 1.1 Objective The objective of this work is to design, develop and implement novel techniques in data communications. It involves producing a prototype demonstrating these novel techniques. 1.2 Scope of Work The scope of the thesis work is divided into four parts. First, to design a three-tier client/server/database architecture which is capable of handling multiple clients at a time. Second, to apply data security to this three-tier system to achieve secrecy and authentication using security tools like encryption, digital signatures, and message digests. Third, to design a Key Distribution Center for delivering encryption keys between parties exchanging data. Fourth, to develop a prototype for this system. 1.3 Secure Three-tier Architecture To achieve the goals set forth, a three-tier model has been developed in which a client (upper-most tier) connects via a network to a database (lower-most tier) on another machine. The middle tier in this application is a Java Server which receives commands from the client, converts the commands to SQL statements and sends the statements to the database. The database processes the SQL statements and sends the results to the middle tier, which then sends them to the client. 1 The three-tier system design provides control on data transactions, adds reliability on the services available and most of all provides a balanced system in which business operations are well distributed. Data security for the three-tier architecture is achieved by using, digital signatures [20, 21], encryption [17], message digests [18] also called Tickets in this thesis, and well designed key distribution [2]. The digital signature act as a receipt for data delivered, which means that a sender cannot disown data delivered and the recipient cannot deny delivery. It also provides authentication, that is, the origin of the data is correctly identified, only the authentic sender could have sent the data and no one else. It also provides a check to verify that the data was not modified on its way from source to destination. Encryption provides secrecy, that is, if the data was attacked during transmission then the contents of the transaction are not revealed to the attacker. A message digest provides a way to produce a fingerprint of the data without requiring any keys. Since a message digest does not require any keys, it cannot be decrypted. Therefore, if an attacker obtains a message digest then it is of no use to him as he cannot look at the contents. A message digest is also used for authentication. In the prototype message digests are used for password transmission and for validating the identity of a user when collecting keys from a Key Distribution Center. 2 A Key Distribution Center (KDC) is used as a trusted third party. The encryption keys are delivered to a KDC if a pair of users wants to exchange data. The KDC releases keys to only authentic users who present a valid message digest. In this thesis a message digest used to collect encryption keys from the KDC is called a Ticket. 1.4 Thesis Contribution Java [15] was chosen to develop the prototype. The java.net package which comes with Java ™ 1.2 [15] platform supports network (client/server) programming. Java Client * Java Application JDBC-ODBC Key Distribution Centre D B M S Figure 1.1: Three-tier Architecture Also, the Java 1.2 Abstract Window Toolkit (AWT) supports the development of GUI interfaces for client applications. The Oracle Relational Database Management System (RDBMS) is used as the Database Management System (DBMS) in the prototype. The 3 JDBC 2.0 API, which is a part of the Java Development Toolkit (JDK) 1.2, provides a SQL database access interface to higher level applications. Security is provided by using the Java, security package that comes with the Java ™ 2.0 platform, and the javax.crypto package that comes with the Java Cryptographic Extension (JCE) 1.2, to provide the functionality needed for secure network communication. An additional server, the Key Distribution Centre, performs the key distribution between a Java Server and various authorized clients. The architecture developed for this thesis is capable of handling multiple clients. All clients initially contact a "mainserver" which creates a Java Server object for each client. 4 All Java Server objects then contact another server called "mainkey" which creates a Key Distribution Center object for each client. The actual sequence of events is discussed in detail in Chapter 5. Figure 1.2 shows how mainkey and mainserver handle multiple clients. Database Management System Java Server] K D C , Java Server-) K D C 2 Java Server 3 K D C 3 Figure 1.3: Three-tier Architecture with Multiple Clients Figure 1.3 shows the scenario after each client is given the necessary objects to create its three-tier architecture. 1.5 Outline of Thesis This thesis is organised as follows: Chapter 2 provides the background on a three-tier architecture and data security; Chapter 3 describes the prototype implemented in this thesis; Chapter 4 describes the classes and methods written to implement the top-most tier, the middle tier and the Key Exchange; Chapter 5 gives a detailed discussion on the design and working of the whole system; Chapter 6 discusses the future research scope and conclusion. 6 Chapter 2 Background This chapter provides a brief overview on the three-tier architecture and the benefits of using such a system. Also, described in this chapter are the security features that are applied to the three-tier architecture for secure data transmission. 2.1 Description of a Three-tier Architecture A three-tier architecture is a special type of client/server system consisting of three well-defined and separate processes, each running on a different platform: • User Interface, a graphical interface which runs on the client's computer. • Application Server, the functional modules that actually process data. • Database Management System, that stores the data required by the middle tier. The three-tier client/server architecture is replacing the classic two-tier architecture of the past. With the classic two-tier system the client always handles data presentation and the server manages the database system. The primary problem of the two-tier system is that the functional modules applying business logic can be implemented only on either the server or the client system. If it is implemented on the server, the server may become overloaded because it must handle both database inquiries as well as to provide the computational resources to manipulate those inquiries. If the business logic is 7 implemented on the client then typically the client software becomes monolithic and platform dependent. The three-tier client-server architecture provides an additional separation of the business logic from the database and the actual presentation. The three-tier architecture is often called a "thin client" client/server because the part of the system that resides on the user's computer is relatively small ("thin"). Whereas, the two-tier architecture is often called a "fat-client" client/server because the part of the system that resides on the user's computer is relatively large. The two-tier architecture can be acceptable if we have few users and the computer network is small, but it is a major disadvantage that data base operations are communicated over the network because network communications are at least one order of magnitude slower than communication between a computer and an attached hard disk drive. Figure 2.1 shows the two models. The goal of three-tier architecture is to achieve a balanced network. "Balanced" means minimising network traffic while supporting many client stations. That is, by adding a middle layer, the application server, between the client and data source, the client is responsible for the user interface while the application server is responsible for the heavier and more complex processing. By distributing processing power between the client and the application server, developers are able to improve performance, better utilising processing power, and adapt to decentralised IT organisational structures. 8 A three-tier architecture allows users to share resources, such as databases, and to reuse business processing logic. For instance, if there is a program residing on a UNIX application server, this logic can be used by any number of PC-based applications. The application development tools market has established three-tier support as a required feature. Three-tier emerged as a standard as the result of a backlash against two-tier development, which relied on the processing power of the client and ended up "hitting the wall" of scalability and interoperability [13]. Figure 2.1: Three-tier model (left) and Two-tier model (right) 2.1.2 Advantages of Three-tier Architecture A three-tier architecture offers significant advantages in several critical areas [22]. 9 2.1.2.1 Control The three-tier architecture emphasises the role of servers as a focal point for business transactions. The server provides a central control point for the application. It behaves like a firewall between application data and the user interface programs. 2.1.2.2 Reliability An application server is inherently more reliable because the essential services can be replicated on multiple servers for ultra-high availability. The middle-ware can route transactions to one of several instances based on load and availability. 2.1.2.3 Scalability and Performance Desktop performance is enhanced through the use of a thin client and lightweight supporting software. Server performance goes up because of load balancing. Database performance is improved because of minimal number of connections to the database. Scalability is a result of good performance and other factors such as: • Availability of widely scalable hardware platforms. The system can be expanded and new hardware resources, databases and applications can be added laterally and vertically. • Ability to add another server at any time. • Specialisation of servers (database, application, communication etc.). 10 2.1.2.4 Flexibility, Growth and Change Successful applications grow. The three-tier approach allows the addition of new subsystems to an existing three-tier architecture with minimal disruption. For example, a new hardware server may be added if a new application is to be deployed. The architecture minimises the impact if we change desktop hardware or software. Only the user interface changes; the business services and data services remain. The three-tier architecture allows us to modify and add data services with ease. We can also transition gradually to a new DBMS and use DBMSs from more than one vendor at the same time without a gateway. 2.2 Data Security This section briefly describes security services, security attacks, symmetric and asymmetric encryption, key distribution techniques, digital signatures and message digests. 2.2.1 Security Services There are six aspects of security services are described in this section [1, 2,4, 5]. 1. Confidentiality: This requires that the information in a computer system and the information transmitted by a system be accessible only by authorised parties. 2. Authentication: This requires that the origin of a message be correctly identified, with an assurance that the identity is not false. 11 3. Integrity: This requires that computer system assets and transmitted information be capable of modification only by authorised parties. 4. Non-repudiation: This requires that neither the sender nor the receiver of a message be able to deny the transmission. 5. Access Control: This requires that access to information resources be controlled by or for the target system. 6. Availability: This requires that computer system assets are available to authorised parties. 2.2.2 Security Attacks There are four general categories of security attacks, which can either be classified as passive or active [1, 2, 4, 5]. 1. Interruption: Interruption happens when an asset of the system is destroyed or become unavailable or unusable. This is an attack on availability. 2. Interception: Interception occurs when an unauthorised party gains access to an asset. This is an attack on confidentiality. 3. Modification: Modification mean that an unauthorised party not only gains access but tampers with an asset. This is an attack on integrity. 4. Fabrication: Fabrication occurs when an unauthorised party inserts counterfeit objects into the system. This is an attack on authenticity. 12 2.2.3 Passive Attacks Passive attacks are in the nature of eavesdropping on, or monitoring of, transmissions. Passive attacks are very difficult to detect since they do not involve any alteration of the data. There are two common types of passive attacks are described in this section [1,2]. 1. Release of Message Contents: It happens when an opponent captures the sensitive or confidential information (e.g. email) in transmission. 2. Traffic Analysis: This happens when an opponent can observe the pattern of messages. The opponent could determine the location and identity of communicating hosts and could observe the frequency and length of messages being exchanged. 2.2.4 Active Attacks Active attacks involve some modification of the data stream or the creation of a false stream and can be sub-divided into the four categories [1, 2, 4, 5]. 1. Masquerade: A masquerade takes place when one entity pretends to be a different entity. 2. Replay: This involves the passive capture of a data unit and its subsequent retransmission to produce an unauthorised effect. 3. Modification of Messages: This simply means that some portion of a legitimate message is altered, or that messages are delayed or reordered, to produce an unauthorised effect. 4. Denial of Service: It prevents or inhibits the normal use or management of communications facilities. 13 2.2.5 Symmetric Encryption Figure 2.2 shows the symmetric encryption process. The original intelligible message, referred to as plaintext, is converted into random nonsense, referred to as ciphertext. The encryption process consists of an algorithm and a key. The algorithm will produce a different output depending on the specific key being used. Changing the key changes the output of the algorithm [1,2]. Shared Key Plaintext Encryption Algorithm Ciphhertext Decryption Algorithm Plaintext Figure 2.2: Simplified Model of Symmetric Encryption. The security of symmetric encryption depends on the fact that, • It is impractical to decrypt the message on the basis of the ciphertext alone. • Security of symmetric encryption depends on the secrecy of the key, not the secrecy of the algorithm. 2.2.6 Message Authentication Encryption protects against passive attack (eavesdropping). Whereas, protection against active attacks (falsification of data and transaction) is known as message authentication. The authentication technique used for message digest involves the use of a hash function, a public function that maps a message of any length into a fixed length hash value, which 14 serves as the authenticator. A hash function accepts a variable size message M as input and produces a fixed size tag H(M) called message digest as output. The purpose of a hash function, or secure hash function is to produce a "fingerprint" of a file, message or block of data. To authenticate a message, the message digest is sent with the message in such a way that the message digest is authentic [1,2]. Figure 2.3 shows the scheme for message authentication using message digests. When the source of data transmission has to send a message to the destination it calculates the hash function over the concatenation of a secret value (e.g. password) and the message. It then sends the message and the message digest (hash value) to the destination. Because the receiver possesses the secret value, it can re-compute the hash value and verify the message digest and, because the secret value itself is not sent, it is not possible for an attacker to modify and intercepted message [1,2]. Sender Receiver Hash Hash compare Figure 2.3: Message Authentication using a Message Digest 15 2.2.7 Asymmetric Encryption Asymmetric or Public-key cryptography involves the use of two keys, in. contrast to symmetric encryption which uses only one key. The public-key cryptographic algorithm relies on one key for encryption and a different but related key for decryption [1,2]. Figure 2.4 illustrates the public-key encryption process. The essential steps in public-key cryptography are: 1. Each end system in a network generates a pair of keys to be used for encryption and decryption of messages that it will receive. 2. Each system publishes its encryption key by placing it in a public register or file (or with a trusted third party). This is the public key. The companion key is kept private. I A ' s 1 public key A ' s User A private > Plaintext • Encryption Algorithm Ciphertext Decryption Algorithm Plaintext I T s p r R • Figure 2.4: Simplified Model of Public-Key Encryption 3. If A wishes to send a message to B, it encrypts the message using A's private key. When B receives the message, it decrypts it using A's public key. Because the message was encrypted using A's private key, only A could have prepared the message. Therefore, the entire encrypted message serve as a digital signature. Since the message cannot be altered without access to A's private key, therefore the message is authenticated both in terms of the source and in terms of data integrity. 16 2.2.8 Key Distribution For symmetric encryption, the two parties in an exchange must have the same key, and this key must be protected from access by others. Furthermore, frequent key changes are usually desirable to limit the amount of data compromised if an attacker learns the key. Therefore the strength of any cryptographic system rests with the key distribution technique, a term that refers to the means of delivering a key to two parties who wish to exchange data, without allowing others to see the key. Figure 2.5: Key Distribution Center for Connection-oriented Protocol In the implementation of the prototype a key distribution centre is responsible for distributing keys to a pair of users. The keys are delivered to the KDC in encrypted form and are not decrypted by KDC, but are decrypted only by the receiving host. An example illustrating the key distribution is shown in Figure 2.5. The process described in Figure 2.5 involves the following 4 steps: 17 1. Host 1 sends a packet requesting connection. 2. Host 2 generates and delivers keys to KDC for Host 1 to pick up. 3. Host 2 sends message to Host 1 informing it to collect keys. 4. Host 1 collects keys. 18 Chapter 3 Prototype Application 3.1 Overview This chapter briefly describes the prototype, including the type of communication protocol used to connect different tiers, the database connection, the platforms used by the system, the database application developed, the encryption algorithms used to achieve data security and some technical details on the Key Distribution Center. 3.2 Communication Protocol The protocol used for server-to-server and client-to-server communication is User Datagram Protocol (UDP) [7]. This protocol doesn't guarantee that the packets will be delivered and does not guarantee that they will arrive in the order they were sent. Hence it is called an "unreliable protocol". UDP is much faster than TCP [7], which is called a "reliable protocol". 3.3 Database Connection This section describes the way the application server (Java Server) connects to the database. The database in the prototype is Oracle Enterprise for Windows NT Release 8.0.5. Oracle 8 provides a unique object-relational functionality by allowing object-oriented applications to integrate with relational data. 19 Java Database Connectivity in the prototype is provided by using Oracle 8 ODBC Driver Version 8.0.5.5 for OCI 8.0.5.0.0 and Net8 Client 8.0.5.0.0 (ODBC Version 3.0), with the JDBC-ODBC Bridge. The JDBC class libraries attempt to homogenise database access for Java across database platforms, which means that one does not need to worry about the database being used while programming the application. JDBC method calls correspond to logical operations one thinks of doing when gathering data from a database: connect to database, create a statement and execute the query, and look at the result set. There are important components of a JDBC product (Figure 3.1) provided as part of Java Development Kit. The first is the JDBC driver manager, which is the backbone of the JDBC architecture. Its primary function is to connect Java applications to the correct JDBC driver and then to get out of the way. The second is the JDBC-ODBC Bridge, which allows ODBC drivers to be used as JDBC drivers. 3.3.1 JDBC-ODBC Bridge plus ODBC driver The JDBC-ODBC bridge provides JDBC access via ODBC drivers. This kind of a driver is most appropriate for a three-tier architecture where the application server code is written in Java. 3.4 System Platform The current prototype is configured to run in the Department of Computer Science Protocol Laboratory. Three computers are used. The Client executes on a machine 20 (robson-win95.cs.ubc.ca, 142.103.10.55) running Microsoft Windows 95. The server and the database execute on the same machine (sumas-win95.cs.ubc.ca, 142.103.10.61) running Microsoft Windows NT Server. The Key Distribution Centre executes on a machine (grouse.cs.ubc.ca, 142.103.10.57) running Microsoft NT Workstation 4.0. Java Application JDBC API JDBC Driver Manager JDBC-ODBC Bridge Driver ODBC and DB Drivers J JDBC Drivers Proprietary, database access, protocols Figure 3.1: JDBC Components 3.5 Description of Application This section describes the application built to demonstrate the three-tier architecture. The application is designed to keep a profile of job seekers. The users of the application can look at these stored profiles by choosing an appropriate menu item from the menu on the Client. Each menu item is a SQL query. Each job seeker record contains a user ID that acts as the primary key of the table in the database holding these profiles. Each record has eleven fields namely, ID, Last Name, First Name, City, Company, Class, 21 Category, Years of experience, Telephone number, Number of hits on the record, and File number. Each job seeker is represented by a company. The company profiles are also stored in the database. Each company record consists of Company Name, Manager Name, and Contact email. The name of the company is the primary key in the table storing company records. We can join company and job seeker records. There is a query designed in the application that lists the names of all the job seekers represented by a particular company whose name is entered by the user. For each client or user of the application, the database contains a profile consisting of user name, password, authorisation level and the time of the last login. The user name is the primary key of the table storing the user profile. Based on the authorisation level there are two types of menus, namely "user" and "admin", available on the Client. Operations in the user menu can only query the database and retrieve data; they cannot alter the database, that is, they cannot do inserts and updates in the database. In contrast, operations in the admin menu can both query data and insert new records or update stored records in the database. The admin menu in the application can enter new records for job seekers, company and users. It can also delete records of a job seeker with a stored ID, user of a valid user name or a company of a particular name. 22 The application can also retrieve a file of a particular name from the Java Server. 3.6 Encryption Algorithms This section describes the algorithms used in the prototype for producing message digests, digital signatures, encryption keys and data encryption. Data Encryption Algorithm Standard (DES) is used for symmetric encryption [17]. DES is used to encrypt data in order to provide secrecy, so that if the data is attacked on its way from source to destination, then its contents are not revealed to the attacker. The Secure Hash Algorithm (SHA) is used to generate message digests [18]. SHA1 [19] is a revision of SHA and differs slightly from SHA. The Digital Signature Algorithm (DSA) is used for the key pair generation in the asymmetric encryption process [20]. SHAlwithDSA [21] is used as a signature algorithm, which uses SHA-1 message digest algorithm and DSA to create and verify DSA digital signatures. In addition, SHA1PRNG is supplied by the SUN provider [16], which is used to introduce randomness in public-private key pair generation in the prototype implementation, where PRNG is the Pseudo-Random Number Generation algorithm. In the prototype a message digest is used to transmit passwords and to prepare Tickets from a user name and password. JCE 1.2 provides the functionality for a message digest, which does not require any keys and so the Client can transmit a password in cryptic 23 form without using any keys as the Client receives keys from the Java Server only after its password is verified. 3.7 Key Distribution The Java Server in the prototype produces a Public-Private key pair for generating and verifying digital signatures, and produces Session keys for encrypting and decrypting. Typically, these keys are used for the duration of a logical connection, such as a virtual circuit or transport connection, and then are discarded. A Key Distribution Center is used as a trusted third party to mediate in the key exchange and distribution when a Client wants to retrieve data from the Java Server. In the Java Cryptographic Extension (JCE) 1.2, an opaque key representation is defined in which one has no direct access to the key - just the three methods: getAlgorithm, getEncoded, and getFormat. This is in contrast to transparent representation in which the access to key material is possible. For the implementation of the prototype the opaque representation is used where the keys are encoded according to a standard format (such as X.509 or PKCS#8) when transmitting the keys to some other party. In JCE 1.2, the KeyFactory class is provided for conversions between opaque cryptographic keys and key specifications. Key factories are bi-directional that is, they allow us to build an opaque key object from a given key specification, or to retrieve the underlying key material of a key object. Multiple compatible key specifications exist for the same key. For instance, a DSA public key may be specified by its components (y, p, q 24 and g), that is, DSAPublicKeySpec or it may be specified using its DER encoding according to the X.509 standard, that is, X509EncodedKeySpec. KDC uses Tickets (message digests) because it requires some mechanism to verify that the keys placed by the Java Server are released to an authentic Client. The Java Server delivers the Ticket (message digest) along with the keys to the KDC. The data used by the Java Server to produce the message digest for the Ticket is based on the user name and password of the Client. The combination of the username and password used to produce data for the Ticket depends on the session ID. Each client is assigned a session ID when it logs into the system. The Client also creates the same Ticket (message digest) and presents it to the KDC to collect the keys. The KDC upon receiving the Tickets compares the two message digests which are not decrypted by KDC, and if the two Tickets are identical then the KDC releases the keys to the Client. The Java Server has to send its Public key and the Session key to the Client, whereas the Client has to send its Public key to the Java Server. Both Public and Session keys are transmitted in encrypted form. The KDC keeps the keys in encrypted form until the Client collects them by presenting a valid Ticket. Upon receiving the encoded keys the Client generates the key specifications and then generates a Keyfactory and uses the key specifications to generate the Session and Public keys. The Java Server performs the same operations to extract Client's Public key from key specifications. 25 Chapter 4 Prototype Classes The objects written to implement the prototype are briefly described in this chapter. 4.1 Class PacketType The class PacketType is used by all, that is, the Client, Java Server and Key Distribution Centre. It contains the numeric codes to describe the type of packet and this code is attached as the first two bytes of the data input stream in each datagram packet. The class PacketType is shown in appendix 1. 4.2 Client Classes Following is a brief description of classes used by the Client program. The code for these classes is show in appendices 2 to 7. 4.2.1 Jclient Jclient handles the Graphical User Interface for the Client program. This class includes methods for: creating menu bars, menu items, and buttons; loading an appropriate menu based on the user's authorization level received from a Java Server; handling events triggered by the selection of a menu item from a menu or the click of a button by the user; opening and closing dialog boxes for data input; submitting a user request to JConn; getting results of a database query or file retrieval from JConn; handling log out by 26 displaying the appropriate panels, text fields, and labels and removing the menu bar; quitting the Client program. 4.2.2 JConn JConn performs all the client/server programming for the Client program. This class contains methods for: connecting to a Java Server; requesting a Java Server to login; making datagram packets to be sent to a desired location which in this application can be either Java Server or Key Distribution Centre (KDC); retrieving ciphertext or plaintext from incoming datagram packets; requesting database query or file from Java Server; sending a digital signature of the input data; receiving a digital signature of the plaintext from Java Server; requesting keys from Java Server; reading Public and Session keys from KDC; sending Client's Public key; printing results obtained from the Java Server; logging out or quiting the program. 4.2.3 JDecoder JDecoder takes care of the security implementation on the Client side for the three-tier architecture. It is responsible for decoding Session and Public keys received from the Key Distribution Centre; decoding and verifying digital signatures; decoding encoded data received from the Java Server; generating Client's Public key and encoding it; making message digests and tickets; and generating digital signatures for data sent by Client to Java Server. 27 4.2.4 JDBclient JDBclient contain methods which are called to read text entered by user in the text fields in the Client Program. 4.2.5 show_pop show_pop is responsible for popping up of a window for user input when the user selects an item from the menu bar. This class also takes care of displaying appropriate text fields and labels for the menu selection. Once data is entered the user clicks on either the "OK" button or "Cancel" button in this window and then the control is returned from this window and hence this class. When user clicks on "OK" button the data read from the text fields is encrypted, used to create a digital signature, packed in a datagram packet, tagged with appropriate code and sent to the Java Server. The Client first sends the digital signature and then the encrypted data. However, if the user clicks on "Cancel" the dialog box closes and no datagram packet is sent to the Java Server. 4.2.6 show_print When the Client program is ready to display query results obtained from the Java Server, then show_print class is used to pop up a window to display the result. Once the user clicks on the "close" button in this window the control is returned to the Jclient class. 4.3 Java Server Classes This section includes a short description of the classes required to run the Java Server. The code for these classes is shown in appendices 8 to 11. 28 4.3.1 mainserver In order to run Java Server this class is executed as all clients contact mainserver. This class is responsible for accepting connection from clients; finding free ports to create Jserver and Key Distribution Centre (KDC); assigning session identification number and ports for Java Server and KDC objects for the client making the request; creating a Jserver object for the Client requesting a connection. 4.3.2 Jserver This class is responsible for all client/server communication between a Client, a Java Server and a Key Distribution Centre (KDC) on the Java Server side. This class is responsible for connecting to the Client after it is created by the mainserver class; connecting to the mainkey and KDC; making datagram packets to be sent to the desired destination; retrieving data from datagram packets received from Client and KDC; receiving requests from Clients; authenticating a Client requesting access to database; receiving a Client request to send Public and Session keys, executing queries or sending files; sending encoded query results or files and digital signature to Client; sending Public and Session keys and ticket to KDC and receiving its response; reading a Client's Public key from KDC. 4.3.3 JEncoder JEncoder takes care of the security implementation on the Java Server side for the three-tier architecture. It is responsible for generating Public-Private key pair and Session key; generating digital signature; encoding query results; decoding user input data; encoding 29 Public and Session keys; making and verifying message digests; decoding Public key; decoding and verifying digital signature. 4.3.4 DBserver This class handles the database connection part for the Java Server. It is responsible for opening a database connection; creating SQL query statements and executing them against the database; writing results obtained from the database from queries to a buffer for Java Server. 4.4 Key Distribution Centre Classes Finally, this section includes a brief description of the classes required to run the Key Distribution Centre (KDC). The code for these classes is shown in appendices 12 to 14. 4.4.1 mainkey All Jserver objects after creation connect to mainkey first and upon receiving this request mainkey creates a JKey object at the specified port on the KDC side. After this first connection the Client and Java Server will contact only the newly created JKey object. So, the purpose of mainkey class is just to create the JKey object on Java Server's request. 4.4.2 JKey This class is responsible for all client/server communication between Client, Java Server and Key Distribution Centre (KDC) on the KDC side. It is responsible for establishing a 30 connection with Java Server and Client for which the JKey object was created; receiving requests from Client and Java Server; retrieving data from incoming datagram packets received from Java Server and Client; making outgoing datagram packets to either Java Server or Client; writing Public and Session keys and Tickets received from Java Server; verifying Ticket received from the Client; releasing Public and Session keys to the Client; writing Client's Public key; releasing Client's Public key to Java Server. 4.4.3 JTicket JTicket handles the security features for KDC which includes verifying and comparing two Tickets. The Java Server writes a Ticket along with keys for Client at KDC. And when a Client requests a KDC to release the keys and presents Ticket to collect them, the JTicket object verifies if the Client presented a valid Ticket or not. If it did the KDC will release the keys. 31 Chapter 5 Mechanics of a Transaction 5.1 Overview This chapter discusses the operations involved in making a transaction in the prototype implemented for this project. The prototype has the ability to handle multiple clients but in this chapter the sequence of actions between a single Client, a Java Server and Key Distribution Centre are presented. In order to start the transaction we need to start a "mainserver" on the Java Server machine and "mainkey" on Key Distribution Centre (KDC) machine. The Client and the two servers communicate to retrieve data or files from the Java Server, or to update, query or insert data into the database. This chapter describes the steps involved in performing these tasks. 5.2 Login Procedure Figure 5.1 shows the screen that appears on a client's machine when it executes the client program, that is Jclient. 5.2.1 Establishing Connection The Client is expected to enter its assigned user name and password and hit the "Login" button. Figure 5.2 shows the sequence of events that take place following the pressing of 32 Login button. The client program (class Jclient) when started created an object named "JConn" that handles the communication on the client's behalf, whereas the object "Jclient" handles the GUI interface. The object Jclient calls "GetAuthO" procedure when the "Login" button is pressed, which makes a connection by sending a request to the server running at port 2000 on the Java Server host machine (refer to label "1" in Figure 5.2). This server is called "mainserver" and is responsible for creating a Java Server Object (class Jserver) for the client and assigning client_ID and ports for the client to communicate with the Java Server and the Key Distribution Centre (KDC). The Jserver Object contacts "mainkey" which is a server running on port 2005 on the host machine where KDC is supposedly located and requests it to create a JKey object at a specified port to perform the KDC functionality (refer to label "2" in Figure 5.2). The "mainkey" creates a KDC object (class JKey) and sends a confirmation. The Jserver then makes the connection with JKey (refer to label "3" in Figure 5.2). And if the connection is established JKey sends a confirmation to Jserver (refer to label "4" in Figure 5.2). Next, the Jserver contacts the client, specifically the object Jconn, sending the Client_ID and the ports for Jserver and JKey (refer to label "5" in Figure 5.2). This Client_ID is assigned for the duration of the session and so the same client gets a different ClientJD every time it logs into the mainserver. The Jserver then creates two objects, namely "DBserver" and "JEncoder". The DBserver class handles database operations which includes opening connection to the database, creating SQL statements, executing the SQL statements, and obtaining results of the queries from the database. The query results obtained are then passed on to the Jserver object. The JEncoder class 33 handles security on Java Server side. The Jserver is now ready to accept a login request (GetAuthRequestQ) from client. [ • ^ C l i e n t P i o g i a Enter User Name j slddiqui Enter Password I "3 Lcpm | Log C'.,l I l i f t Sl.nl | |B-WVA 1 gj,lhVA Figure 5.1: Login Screen Next the JKey object contacts the client by sending a message that a KDC connection is ready for the client (refer to label "6" in Figure 5.2). JKey then creates an object JTicket whose function is to verify whether the Ticket presented by client to collect keys is valid or not. This is done by matching the message digests (Tickets) obtained from the Java Server and the Client. The JKey is now ready and waits for a request from Jserver. The client after receiving the confirmation from JKey creates two objects namely JDecoder and JDBclient. The class JDecoder handles all the cryptographic operations for client including creating and comparing message digests, encryption and decryption, 34 generating and verifying digital signatures and generating a client's public key. The class JDBclient reads data from text fields in the GUI display. Figure 5.2: Sequence of communication commands to establish connection. 5.2.2 Verification of Password Now the connections between JKey (Key Distribution Centre), JConn (Client) and Jserver (Java Server) are established. Next, step in the GetAuth() procedure is to request for verification of password and user name and to do this the method "requestAuth()" is called, which creates a message digest of the password and then sends a packet which carries the user name in clear and password as a message digest to the Java Server. The client program now waits for a reply from the Jserver. The Jserver which is waiting for a login request from client, upon receiving the packet from the client and identifying it to be a password verification packet calls on the "executeAuth()" method of DBserver class. The DBserver class then queries the database and retrieves the password and user authorisation level for the user name which was send in clear by the client. Next, the password retrieved from the database and the message digest of the password send by client are verified by the "VerifyMessageDigestQ" Client (JConn) K D C 35 method in JEncoder class. If this methods returns true, then GetAuthRequest() determines the level for this user as there are currently two types of users defined in the database that is, "user" and "admin". Jserver then sends client its authorisation level and confirmation that its password was verified. This completes the tasks assigned to GetAuth() in Jclient class. The Login button now is labelled "Logged in" and is disabled for the entire session. 5.2.3 Exchange of Session and Public Keys Although the connection establishment and login procedure is now complete, the client still needs to go through the process of exchanging Public Keys and receiving a Session Key from Jserver. To the user it would appear that it is still part of the login process as the user is not expected to initiate any actions to receive keys. The GetKeys() method of JConn class is called by Jclient for key exchange. The first step in GetKeys() is to check if this client has already accepted keys from a Java Server in the current session and if it already has the keys then GetKeys() will not do anything. Otherwise, it will generate a "Ticket" which is a message digest that uses a combination of user name and password of the user (or client). The configuration of the "TicketData" depends on ClientJD. Next, in GetKeys() the client sends a packet to the Java Server requesting it to send the Java Server's Public key. When this packet is received by the Java Server it first 36 generates a "Ticket" and sends it to the Key Distribution Centre (KDC) and receives confirmation from KDC that the Ticket was successfully received. Second, the Java Server generates a Public-Private Key pair and encodes the Public key. Third, the Java Server sends the encoded Public key to KDC and receives confirmation back that the key was accepted. Once, a confirmation is received from KDC the Java Server sends a message to the Client that a Public key with a Ticket is waiting for Client at KDC to be collected. So the next step in GetKeysQ is the collection of Public Key from KDC by (1) R E Q _ P U B K E Y (2) WRITE_TICKET (3) A C K J T I C K E T (4) W R I T E J P U B K E Y (5) A C K J P U B K E Y (6) A C K _ P U B K E Y (7) R E A D _ P U B K E Y (with TICKET) (8) A C K J P U B K E Y Key Distribution Center Figure 5.3: Delivering Java Server's Public Key Client. The client sends a packet to KDC requesting the Public key and attaches the "Ticket" in this packet. The KDC opens the packet detaches the Ticket and then calls on "VerifyTicketO" and if the Ticket that client send matches with the one Java Server delivered earlier, then KDC gives the Public key to the client by attaching it to a packet. Upon receiving the encoded Public key from KDC the client decodes the Public key by calling "DecodePublicKeyQ". 37 Figure 5.3 shows the steps involved in generating and delivering the Public key by Java Server to the Client via the Key Distribution Centre. Key Distribution Center Figure 5.4: Delivering Shared Session Key A similar sequence of events takes place for the retrieval of the Session key. That is, the client requests the session key from the Java Server as seen in GetKeys(). The Java Server upon receiving the request from the Client generates the Session key and then encodes the Session key. The Java Server then delivers the encoded key to the KDC and sends a message to Client to pick it up. The Client then sends a packet to KDC and attaches the Ticket to it. KDC first detaches the ticket and verifies it by comparing it with the ticket saved at KDC by Java Server. If the ticket is verified then K D C gives the Session key to the Client. As before, the Client upon receiving the Session key decodes it by calling DecodeSessionKeyQ. 38 Figure 5.4 shows the steps involved in generating and delivering of Session key. The only difference between figures 5.3 and 5.4 is that the Java Server only sends the ticket to the KDC one time. Key Distribution Center Figure 5.5: Delivering Client's Public Key Next, the Client has to send its Public key to the Java Server which it achieves by calling SendPubkeyO in GetKeys(). In order to do this, the Client first generates a Public-Private key pair and then encodes the Public key. Secondly, the Client prepares a packet attaching the encoded Public key and requests the KDC to save this Public key. The KDC saves the Public key and sends a confirmation to Client that KDC has saved the Public key. Thirdly, the Client contacts the Java Server to collect the Public key from KDC. The Java Server then sends a request to KDC to collect Client's Public key and in response, the KDC sends the encoded key. Upon receiving the Public key from KDC the Java Server decodes it by calling DecodePublicKeyQ and if all goes well it sends a 39 confirmation to the Client that the Java Server has successfully received the Client's Public key. Figure 5.5 shows the sequence of events that take place to achieve the transfer of Client's Public key to Java Server. This completes the exchange of keys and so the tasks assigned to GetKeys() are successfully achieved. Waiting for Password Verification .. Client starting-Password Verified. Making Ticket... Requesting Public Key... Sending Ticket.. Public Key Received. Requesting Session Key... Sending Ticket-Session Key Received. Public Key Sent to Server. Sending Ticket... Ready... Input Window Enter New ID: | 112 Enter LAST NAME: | 8mith Enter FIRST NAME: j John Enter CITY: j Vancouver Enter COMPANY: | UBC Enter CLASS: | C12 Enter CATEGORY: | Systems Enter YRS: I.J.? Enter TELNO: j (604)123-4567 Enter FILE_NO: j P00Q112) J C l r . i l I — • ..u»on. " I %• - v . v*: our 3 S . . , . | JRJA -A | g. !A,A [ g g WA I p., • I ? , t . , , p , . ^ || ^  ,„,,„, W l n „ f ,^ Figure 5.6: Menu for Clients with "admin" Authorisation Level 5.2.4 Loading Menus Next, based on the authorisation level, that is whether the Client is a "user" or "admin' an appropriate menu appears on the screen. Figure 5.6 and 5.7 shows the two menus. 40 5.3 Running the Application In this section we will illustrate the steps involved in retrieving data from the database. The Client has the option to either "Logout" from the application or "Quit" the application any time it wants. The difference between the two options is that when a Client logs out the screen shown in Figure 5.1 appears whereas when Client quits, it means that the Client is no longer running. RpTijfl Flit Lite Waiting for Password Verification... Client starting... Password Verified. Making Ticket... Requesting Public Key... Sending Ticket... Public Key Received. Requesting Session Key... Sending Ticket.. Session Key Received. Public Key Sent to Server. Sending Ticket... Ready... Li gi^lnpul Window jEnter LAST NAME: Enter FIRST NAME: Peter| S S I Loj Out si m Figure 5.7: Menu for Clients with "user" Authorisation Level The Client selects an item from the menu and a dialog box pops up with appropriate number of text fields and labels as shown in figures 5.6 and 5.7. The Client then enters the data and hits "OK". This event triggers a series of actions which starts with a call to 41 ReadText(), a method to read text from text fields. Next, the request is submitted by a call to a function SubmitRequest() which calls the method RequestQueryO in JConn. The function RequestQueryO first sends the digital signature of the text read from the textfields to the Java Server and receives a confirmation of the delivery. It then encodes the input text using the shared session key by calling the function EncodeFile() and sends the resulting Ciphertext to the Java Server. The Java Server upon receiving the encoded request first decodes the query data by calling DecodeFile() and then decodes the digital signature by calling DecodeSignature() and then calls VerifySignature() to determine if the data sent is authentic. If the data is authentic then the Java Server calls the method ExecuteQueryO in the DBserver object. The method ExecuteQueryO calls an appropriate function based on the query requested by the Client. All the executeQ#() functions create query statements for the database, execute them, receive the resultant objects from the database, and then write the results to a buffer for the Java Server. The Java Server upon successfully executing the query sends a confirmation to the Client. The Client then calls on GetResults(), which in turn calls on GetEncodedResult() to retrieve the query results from the Java Server. When the Java Server receives the request to deliver results to the Client, it calls the method GetClientEncodeRequest() which first encodes the query results, then chops the results into several packets, and then starts sending the packets of encoded results to the Client. These packets are received by the client in the method retrieveCipherPacket(). There is a flag called "transferComplete" 42 which has a value of zero if the packet sent by the Java Server is not the last packet and has a value of one if the packet sent by the Java Server is the last packet. Once the Client successfully receives all packets it calls the method GetSign() to get the digital signature of the data just received. The Java Server processes this request by calling the method GetClientSignRequest(). This method first calls GenerateSignature() and then pack the digital signature in a packet and sends it to the Client. If the Client successfully receives this digital signature, it first calls DecodeFile() to decode the data earlier received and then calls DecodeSignature() to decode the signature and finally calls VerifySignature() to check if the query results obtained by the Java Server are authentic. 13 Waiting for P a s s w o r d Verification .. Cl ient starting... P a s s w o r d Verified. Making Ticket . . . R e q u e s t i n g Publ ic Key... S e n d i n g T icket . . Publ ic Key Rece ived . R e q u e s t i n g S e s s i o n Key... S e n d i n g T icket . . S e s s i o n K e y R e c e i v e d . Public Key Sent to Server, S e n d i n g T i c k e t -Ready. . . Ready.. . . S e g e m e n t # 1 received S e g e m e n t * 2 received Segement#3 received Final S e g e m e n t R e c e i v e d • Transfer C o m p l e t e Verify Signature.. . . signature verifies: true J2' P i i n t W i n d o ' : P R I N T I N G R E S U L T S ' ID= 108 L A S T N A M E = N e l s o n F I R S T N A M E = Peter CITY = Vancouver C O M P A N Y = T E C H N I A C C A T E G O R Y = G r a p h i c s Y R S = 15 T E L . N O = (604)555-7654 N O _ H I T S = 9 LJ JB i n Iff I SP I r r a w P i w j | y M f w i n i . | | r > „ n t W i Figure 5.8: Display of Query Results 43 If all goes well, then the Client program calls printResult() and another window pops out displaying the query results. Figure 5.8 shows the screen shot of the Client program displaying the results. The user can click on "close" to shut down the print window. This completes the series of actions prompted by the menu selection. The user can now select another item from the menu bar. 5.4 Logout or Quit The user can either logout or quit the program anytime they want. When a user logs out the Client program calls DoLogOff() method in Jclient class. This method first calls NewUser() method of JConn class which resets the state for Session key, Public keys, and authorisation to false and then calls the method GoodBye(). The method GoodBye() sends a message to the Java Server that the Client is logging out and when Java Server receives the good bye message it changes the user's access authority to false. If the GoodBye() procedure is completed successfully then the NewUser() procedure completes in DoLogOff(). Next, the menu bar is removed and the login screen appears which was shown in figure 5.1. In contrast, when the user hits the "Quit" button the application is closed and to use the application again the user has to re-load the Client program. 44 Chapter 6 Conclusion and Future Work 6.1 Conclusion The following conclusions can be drawn about the three-tier architecture and data security scheme implemented in the prototype. 6.1.1 Security against Passive Attacks Encryption provides protection against passive attacks as the attacker monitoring the communication between the Java Server and the Client would not be able to look at the contents of the packets. Since the Session key used to encrypt data and the actual packet take different routes from the Java Server to the Client, an attacker can only capture either an encrypted packet or a Session key. One without the other is of no use to the attacker. 6.1.2 Security against Active Attacks Digital signatures can detect active attacks since they are generated by an asymmetric encryption technique. The source (Java Server or Client) produces a digital signature using its private key and the destination (Client or Java Server) decodes it using the source's Public key thereby ensuring that the data packets coming in are from an authentic source. Also, if a packet is missing, that is if a packet is captured by the attacker then the digital signature will not verify the data and the destination will reject the data considering it corrupt. 45 6.1.3 Centralized Security Management Security for multiple access paths to a common information resource, the database, is achieved by providing centralized security management. The system developed in this work is capable of providing this central control in the form of the Java Server. All clients connect to a Java Server which controls access to all databases and servers from which data is to be retrieved. 6.1.4 Single Point Authentication The scheme developed in the thesis work provides single point authentication and access control security for applications. For instance, a user may have an account for Oracle authentication and an access control for the client/server application. As a result, the administrative requirements have just been doubled for the user. At the same time, the outboard of authentication from the RDBMS to applications means that the access control and auditing for the actions of user must also be cared for by the application. Extend this scenario to five or ten additional applications and the burden of security administration increases exponentially. In the three-tiered security architecture developed, we only have one user account for both the application and the Oracle RDBMS. As a result, the user is responsible for maintaining only a single password. Also, audit trails detailing a user's activity are available at a single location. This ultimately reduces the number of security administration points within the system thereby making it more feasible to execute a broader distributed security administration strategy. 46 6.1.5 Customized Security The data protection applied in this work is quite flexible and can be customized very easily to meet the requirements of individual users. For instance, it is possible to provide users with a choice from different algorithms for generating message digests, digital signatures and ciphertext. Also, the configuration for a ticket needed to collect Public and Session keys can be specified by the user. 6.1.6 Scalability One of the goals of this project was to architect and develop a highly scalable and secure three-tiered internet solution. The design of the implemented work is scalable as we can add more power at the application (Java Server) or database level without touching the client application. 6.1.7 Transparency It was demonstrated in this work that the window-based application can run transparently across a three-tier architecture made up of Windows 95, Windows NT Workstation, Windows NT Server and Oracle 8 Database. 6.1.8 Balanced Network The three-tier architecture developed in this work presents a balanced network. This is done by adding the middle tier, that is the Java Server between the Client and the database. The Client is responsible for the user interface while the Java Server is responsible for the heavier and more complex processing. By distributing processing 47 power between the Client and the Java Server, we are able to improve performance, to better utilize processing power and to decentralize the system. 6.2 Suggestions for Future Work Some suggestions and ideas for future work are listed in this section. 6.2.1 Web-based Solution The prototype in this thesis is window-based which can be converted to a web-based secure three-tiered solution using applet technology. The only changes needed are in the Jclient, show_pop and show_print classes as they handle the user interface on the Client side. 6.2.2 Universal Personal Computing The security scheme designed in this project for the three-tier architecture can be extended to benefit other systems. It would be an interesting project to apply the security scheme to Universal Personal Computing [14]. Universal Personal Computing (UPC) makes a user's personal computing environment (PCE) move with the user who may use any terminal available to connect to the internet. In order to use UPC the mobile user must log into the foreign terminal using a Logical User Identifier (LUI). The user also needs to retrieve its personalized computing environment (PCE). PCE is a set of objects that a mobile user wants to use at the foreign location. 48 Security tools like message digests, digital signatures and encryption can be used in UPC. For instance, we can create a message digest of LUI when the mobile user wants to log into the foreign terminal. The PCE profile can be transmitted in encrypted form. The real challenge in implementing security to UPC would be to incorporate the tools discussed in this report in CORBA. These tools should be used to supplement the security features that already exists for CORBA objects. 49 References 1. William Stallings, "Data And Computer Communications", Prentice Hall, Inc., 1997. 2. William Stallings, "Network and Internetwork Security: Principles and Practice", Prentice Hall Inc., 1995. 3. Andrew S. Tanenbaum, "Computer Networks", Prentice Hall, Inc., 1996. 4. Patrick W. Dowd and John T. McHenry, "Network Security: It's Time to Take It Seriously", IEEE Computer, September 1998. 5. Christopher W. Klaus, "Network Security: Anything But Bulletproof, Internet Security Systems Inc., November 1996. 6. Randall J. Atkinson, "Toward a More Secure Internet", IEEE Computer, January 1997. 7. Bruce Eckel, "Thinking in Java", Prentice Hall Inc., 1998. 8. Elliotte Rusty Harold, "Java Secrets", IDG Books Worldwide Inc, 1997. 9. Edward Honour, "Oracle How-To", The Waite group Inc., 1996. 10. Monica Pawlan, "Java™ 2 Cryptography Extension: Encrypting and Decrypting Data", Sun Microsystems, January 1999. 11. "Java™ 2 Cryptography Architecture: API Specifications and References", Sun Microsystems, October 1998. 12. Maydene Fisher, "JDBC Database Access", Sun Microsystems, 1999. 13. "JDBC™ Guide: Getting Started", Sun Microsystems, March 1997. 50 14. Victor Leung, Jinsong Zhu, Maria Toro, Son Vuong, Thong Huynh, and Kangming Liu, "Universal Personal Computing on the Internet: Architecture and Implementation", University of British Columbia, March 1998. 15. Java™ Development Toolkit Version 1.2, Developed by Sun Microsystems, Inc., Copyright (c) 1994-1998 Sun Microsystems, Inc. 16. Java™ Cryptography Extension (JCE 1.2 RC1), Developed by Sun Microsystems, Inc., Copyright (c) 1994-1998 Sun Microsystems, Inc. 17. The Data Encryption Algorithm Standard (DES), Federal Information Processing Standard 46 (FTPS PUB 46), National Institute of Standards and Technology (NIST), 1994. 18. The Secure Hash Algorithm (SHA), Federal Information Processing Standard (FTPS PUB 180), National Institute of Standards and Technology (NIST), 1993. 19. The Secure Hash Algorithm-1 (SHA1), Federal Information Processing Standard (FTPS PUB 180-1), National Institute of Standards and Technology (NIST), 1994. 20. The Digital Signature Algorithm (DSA), Federal Information Processing Standard (NIST FTPS 186), National Institute of Standards and Technology (NIST), 1993. 21. SHAlwithDSA, Federal Information Processing Standard (NIST HPS 186), National Institute of Standards and Technology (NIST), 1993. 22. Gary Long, "Advantages of a 3-tier Architecture for Distributed TP", Magna Software Corporation, 1997. 51 / /Appendix 1: PacketType.j ava public c l a s s PacketType{ public s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t p u b l i c s t a t i c f i n a l i n t p u blic s t a t i c f i n a l i n t SEND_FILE = 1; REQ_PUBKEY = 2; REQ_SESKEY = 3; SEND_SIGN = 5; ACK = 6; ERROR = 7; READ_PUBKEY = 8; READ_SESKEY = 9; WRITE_PUBKEY = 10; WRITE_SESKEY = 11; ACK_PUBKEY = 12; ACK_SESKEY = 13; ACK_SIGN SEND_Q1 = SEND_Q2 = SEND_Q3 = SEND_Q4 = SEND_Q5 = SEND_Q6 = SEND_Q7 = SEND_Q8 = SEND_Q9 = SEND_Q10 SEND_Q11 SEND_Q12 SEND_Q13 SEND_Q14 SEND_Q15 SEND_Q16 SEND_Q17 SEND_Q18 SEND_Q19 SEND_Q20 SEND_Q21 SEND_Q22 SEND_Q23 SEND_Q24 GOOD_BYE = 14 15 16 17 18 19 20 21 22 23 = 24 = 25 = 26 = 27 = 28 = 29 = 30 = 31 = 32 = 33 = 34 = 35 = 36 = 37 = 38 = 39 SEND_ENCODED_FILE = 40; SEND_ENCODED_QUERY =41 VERIFY_PW = 42; ACK_PW = 43; VALID_USER = 44; VALID_ADMIN = 45; ACK_QUERY = 46; DATA_S ERVER = 47; KEY_SERVER = 48; CIPHER = 49; EMPTY_PKT = 4; INIT_DATAPORT = 2000; INIT_KEYPORT = 2.005; SEND_SERVER_PORT = 50; S END_CLIENT_PORT = 51; READY = 52; 52 public s t a t i c f i n a l i n t VERIFY_TICKET = 53 public s t a t i c f i n a l i n t WRITE_TTCKET = 54; pub l i c s t a t i c f i n a l i n t ACK_TICKET = 81; pub l i c s t a t i c f i n a l i n t ACK_SERV_PK = 82; 53 / /Appendix 2: J c l i e n t . J a v a i m p o r t j ava.awt.*; i m p o r t j a v a . i o . * ; i m p o r t j a v a . n e t . * ; i m p o r t j a v a . u t i l . * ; i m p o r t j a v a . l a n g . * ; p u b l i c c l a s s J c l i e n t e x t e n d s Frame{ p r i v a t e s t a t i c J D B c l i e n t CDB; p r i v a t e s t a t i c JConn conn; p r i v a t e s t a t i c show_pop pop_up; p r i v a t e S t r i n g mname = "MessDig"; p u b l i c s t a t i c S t r i n g UserName; p u b l i c s t a t i c S t r i n g PW; p u b l i c s t a t i c S t r i n g T i c k e t D a t a ; p u b l i c s t a t i c b o o l e a n R e a d F i l e = f a l s e ; p u b l i c s t a t i c b o o l e a n ReadQuery = f a l s e ; p u b l i c s t a t i c b o o l e a n ACK_AUTH = f a l s e ; p u b l i c s t a t i c i n t MENU_LEVEL=0; p u b l i c s t a t i c b o o l e a n GO_AHEAD = f a l s e ; p u b l i c s t a t i c i n t QueryNum; p r i v a t e s t a t i c S t r i n g F i leName; S t r i n g [ ] Q u e r y l t e m s = {"Record o f ID", "Record o f Name", "No H i t s o f ID"}; S t r i n g [ ] R e c o r d l t e m s = {"By ID", "By Name", "By C a t e g o r y " , "By C i t y " , "By C a t e g o r y and Y r s " , "By Company name"} S t r i n g [ ] Showltems = { " T o t a l R e c o r d s " , "Show ALL R e c o r d s " , "Show ALL Companies", "Show ALL U s e r s " } ; S t r i n g [ ] D e l e t e l t e m s = {"Delete R e c o r d " , " D e l e t e Company", " D e l e t e U s e r " } ; p u b l i c s t a t i c S t r i n g s i ; p u b l i c s t a t i c S t r i n g s2; p u b l i c s t a t i c S t r i n g s 3 ; p u b l i c s t a t i c S t r i n g s4; p u b l i c s t a t i c S t r i n g s 5 ; p u b l i c s t a t i c S t r i n g s 6; p u b l i c s t a t i c S t r i n g s 7 ; p u b l i c s t a t i c S t r i n g s 8 ; p u b l i c s t a t i c S t r i n g s9; p u b l i c s t a t i c S t r i n g s l O ; p r i v a t e s t a t i c P a n e l p3; p r i v a t e P a n e l p i ; p r i v a t e s t a t i c D i a l o g pop_d; p r i v a t e s t a t i c T e x t F i e l d t i l = new T e x t F i e l d ( " ", 2 0 ) ; p r i v a t e s t a t i c T e x t F i e l d t l 2 = new T e x t F i e l d ( " " , 2 0 ) ; p r i v a t e s t a t i c L a b e l 111 = new L a b e l ( " E n t e r U s e r Name"); p r i v a t e s t a t i c L a b e l 112 = new L a b e l ( " E n t e r P a s s w o r d " ) ; p u b l i c s t a t i c T e x t A r e a a = new T e x t A r e a ( " ", 40, 1 ) ; p u b l i c i n t MENU_TYPE; MenuBar UserMenu = new MenuBar(); MenuBar AdminMenu = new MenuBar(); MenuBar EmptyMenu = new MenuBar(); 54 Menu q = new Menu("Query"); Menu upd = new Menu("Update"); Menu i = new M e n u ( " I n s e r t " ) ; Menu a d _ f = new M e n u ( " F i l e " ) ; Menu u s _ f = new M e n u ( " F i l e " ) ; Menu r = new Menu("Record"); Menu u s r = new Menu("User"); Menu de = new M e n u ( " D e l e t e " ) ; Menu ad_sh = new Menu("Show"); Menu u s _ s h = new Menu("Show"); B u t t o n e = new B u t t o n ( " Q u i t " ) ; B u t t o n 1 = new B u t t o n ( " L o g i n " ) ; B u t t o n l o f f = new B u t t o n ( " L o g O u t " ) ; p u b l i c s t a t i c B u t t o n OK = new B u t t o n ( " O K " ) ; p u b l i c s t a t i c B u t t o n c = new B u t t o n ( " C a n c e l " ) ; p u b l i c J c l i e n t ( ) { conn = new J C o n n ( ) ; CDB = new J D B c l i e n t O ; s e t L a y o u t ( n e w B o r d e r L a y o u t ( ) ) ; i n t c o u n t e r ; f o r ( c o u n t e r = 0 ; c o u n t e r < Q u e r y l t e m s . l e n g t h ; counter++){ q.add(new M e n u l t e m ( Q u e r y l t e m s [ c o u n t e r ] ) ) ; i f ( ( c o u n t e r + 1 ) % 2 == 0) q . a d d S e p a r a t o r ( ) ; } f o r ( c o u n t e r = 0 ; c o u n t e r < R e c o r d l t e m s . l e n g t h ; counter++){ r.add(new M e n u l t e m ( R e c o r d l t e m s [ c o u n t e r ] ) ) ; i f ( ( c o u n t e r + 1 ) % 2 == 0) r . a d d S e p a r a t o r ( ) ; } f o r ( c o u n t e r = 0 ; c o u n t e r < S h o w l t e m s . l e n g t h ; counter++){ ad_sh.add(new M e n u l t e m ( S h o w l t e m s [ c o u n t e r ] ) ) ; us_sh.add(new M e n u l t e m ( S h o w l t e m s [ c o u n t e r ] ) ) ; i f ( ( c o u n t e r + 1 ) % 2 == 0 ) { a d _ s h . a d d S e p a r a t o r ( ) ; u s _ s h . a d d S e p a r a t o r ( ) ; } f o r ( c o u n t e r = 0 ; c o u n t e r < D e l e t e l t e m s . l e n g t h ; counter++){ de.add(new M e n u l t e m ( D e l e t e l t e m s [ c o u n t e r ] ) ) ; i f ( ( c o u n t e r + 1 ) % 2 == 0) d e . a d d S e p a r a t o r ( ) ; } usr.add(new M e n u l t e m ( " L a s t L o g i n " ) ) ; u s_f.add(new M e n u l t e m ( " F i l e By Name")); ad_f.add(new M e n u l t e m ( " F i l e By Name")); upd.add(new Menultem("Update CATEGORY")); upd.add(new Menultem("Update C L A S S " ) ) ; i.add(new M e n u l t e m ( " I n s e r t R e c o r d " ) ) ; i.add(new M e n u l t e m ( " I n s e r t U s e r " ) ) ; 55 i.add(new M e n u l t e m ( " I n s e r t Company")); AdminMenu.add(q); UserMenu.add(r) ; AdminMenu.add(ad_f) ; UserMenu. add ( u s _ f ) •; U s e r M e n u . a d d ( u s r ) ; AdminMenu.add(upd); AdminMenu.add(i); AdminMenu.add(de) ; AdminMenu.add(ad_sh); UserMenu.add(us_sh); p3 = new P a n e l ( ) ; p 3 . s e t L a y o u t ( n e w G r i d L a y o u t ( 2 , 1, 2, 2 ) ) ; p3 . a d d ( 1 1 1 ) ; p 3 . a d d ( t i l ) ; p 3 . a d d ( 1 1 2 ) ; p 3 . a d d ( t l 2 ) ; t l 2 . s e t E c h o C h a r ( ' * ' ) ; a d d ( " N o r t h " , p3) ; P a n e l p2 = new P a n e l ( ) ; p 2 . s e t L a y o u t ( n e w G r i d L a y o u t ( 1 , 3, 50, 5 0 ) ) ; p 2 . a d d ( 1 ) ; p2 . a d d d o f f ) ; p 2 . a d d ( e ) ; a d d ( " S o u t h " , p 2 ) ; a . s e t E d i t a b l e ( f a l s e ) ; a d d ( " C e n t e r " , a ) ; p u b l i c v o i d s h o w _ d i a l o g ( i n t n){ pop_up = new show_pop(); p o p _ u p . r e s i z e ( 4 0 0 , 3 0 0 ) ; p o p _ u p . a d d P a n e l ( n ) ; p o p _ u p . s e t V i s i b l e ( t r u e ) ; pop_d = new D i a l o g ( p o p _ u p ) ; pop_d.show(); } p u b l i c s t a t i c v o i d c l o s e _ d i a l o g ( ) { p o p _ u p . R e s e t T e x t ( ) ; p o p _ u p . s e t V i s i b l e ( f a l s e ) ; } p u b l i c s t a t i c v o i d p r i n t R e s u l t ( ) { t r y { s h o w _ p r i n t . p _ a . a p p e n d T e x t ( " \ n \ n * * * * * * * * * *» + P R I N T I N G R E S U L T S * * * * * " + " * * * *\n\n") ,-D a t a l n p u t S t r e a m i n = new D a t a l n p u t S t r e a m ( new F i l e l n p u t S t r e a m ( J C o n n . p T e x t ) ) ; S t r i n g c u r r e n t L i n e ; w h i l e ( ( c u r r e n t L i n e = i n . r e a d L i n e ( ) ) != n u l l ) { s h o w _ p r i n t . p _ a . a p p e n d T e x t ( c u r r e n t L i n e + " \ n " ) ; } } c a t c h ( E x c e p t i o n e ) { sh o w _ p r i n t . p _ a . a p p e n d T e x t ( " E r r o r from p r i n t R e s u l t : " + e + " \ n " ) ; 56 } } p u b l i c b o o l e a n h a n d l e E v e n t ( E v e n t e v t ) { i f ( e v t . i d == Event.WINDOW_DESTROY) S y s t e m . e x i t ( 0 ) ; e l s e r e t u r n s u p e r . h a n d l e E v e n t ( e v t ) ; r e t u r n t r u e ; p u b l i c v o i d D o L o g O f f ( ) { i f ( c o n n . N e w U s e r ( ) == t r u e ) { ACK_AUTH = f a l s e ; GO_AHEAD = f a l s e ; R e a d F i l e = f a l s e ; ReadQuery = f a l s e ; MENU_LEVEL = 0; setMenuBar(EmptyMenu); 1 . s e t E n a b l e d ( t r u e ) ; 1 . s e t L a b e l ( " L o g i n " ) ; t i l . s e t T e x t ( " " ) ; t l 2 . s e t T e x t ( " " ) ; 111. s e t T e x t ( " E n t e r U s e r Name"); 112. s e t T e x t ( " E n t e r P a s s w o r d " ) ; a . s e t T e x t ( " " ) ; p3 = new P a n e l ( ) ; p 3 . s e t L a y o u t ( n e w G r i d L a y o u t ( 2 , 1, 2, 2 ) ) ; p3.add(111); p 3 . a d d ( t i l ) ; p 3 . a d d (112); p 3 . a d d ( t l 2 ) ; t l 2 . s e t E c h o C h a r ( ' * ' ) ; a d d ( " N o r t h " , p 3 ) ; } } p u b l i c s t a t i c v o i d S u b m i t R e q u e s t ( ) { i f ( R e a d Q u e r y == t r u e ) { i f ( ( Q u e r y N u m == PacketType.SEND_Q11 || QueryNum == PacketType.SEND_Q13 | QueryNum PacketType.SEND_Q1 || QueryNum == PacketType.SEND_Q7 jj QueryNum == PacketType.SEND_Q3 || QueryNum == PacketType. SEND_Q4 j.j QueryNum == PacketType.SEND_Q5 j j QueryNum == PacketType.SEND_Q19 j | QueryNum == PacketType.SEND_Q20 j j QueryNum == PacketType.SEND_Q21 || QueryNum == PacketType.SEND_Q8 | j QueryNum == PacketType.SEND_Q9 j j QueryNum == PacketType.SEND_Q10 || QueryNum == PacketType.SEND_Q18) && GO_AHEAD == t r u e ) C D B . W r i t e l L i n e O ; e l s e i f (QueryNum == PacketType.SEND_Q2 || QueryNum 57 PacketType.SEND_Q14 ||QueryNum == PacketType.SEND_Q12 j j QueryNum == PacketType.SEND_Q6) C D B . W r i t e 2 L i n e s ( ) ; e l s e i f (QueryNum == PacketType.SEND_Q16 || QueryNum == PacketType.SEND_Q17) C D B . W r i t e 3 L i n e s ( ) ; e l s e i f (QueryNum == PacketType.SEND_Q15) C D B . W r i t e l O L i n e s ( ) ; e l s e GO_AHEAD = f a l s e ; if(GO_AHEAD == t r u e ) { c o n n . S i g n F i l e = JConn.qname; GO_AHEAD = conn.RequestQuery(QueryNum); } e l s e { a.appendText("Unknown Query, T r y A g a i n ! " ) GO_AHEAD = f a l s e ; } } e l s e i f ( R e a d F i l e == t r u e ) { if(GO_AHEAD == t r u e ) { FileName = s i ; GO_AHEAD = c o n n . R e q u e s t F i l e ( F i l e N a m e ) ; } e l s e { a.appendText("Unknown Query, T r y A g a i n ! " ) GO_AHEAD = f a l s e ; } } e l s e { a . a p p e n d T e x t ( " U n s u c c e s s f u l i n R e q u e s t i n g F i l e \ n GO_AHEAD = f a l s e ; } } p u b l i c s t a t i c v o i d G e t R e s u l t s ( ) { i f ( R e a d F i l e == t r u e && GO_AHEAD == t r u e ) GO_AHEAD = co n n . G e t E n c o d e d R e s u l t ( PacketType.SEND_ENCODED_FILE); e l s e i f ( R e a d Q u e r y == t r u e && GO_AHEAD == t r u e ) GO_AHEAD = c o n n . G e t E n c o d e d R e s u l t ( PacketType.SEND_ENCODED_QUERY); e l s e { a.appendText("Cannot R e a d ' R e s u l t s \ n " ) ; GO_AHEAD = f a l s e ; } if(GO_AHEAD == t r u e ) GO_AHEAD = c o n n . G e t S i g n ( ) ; } p u b l i c b o o l e a n a c t i o n ( E v e n t e v t , O b j e c t a r g ) { i f ( e v t . t a r g e t . e q u a l s ( e ) ) c o n n . Q u i t _ P r o g r a m e ( ) ; e l s e i f ( e v t . t a r g e t . e q u a l s ( 1 ) ) { s i = new S t r i n g ( t i l . g e t T e x t ( ) ) . t r i m ( ) ; s2 = new S t r i n g ( t l 2 . g e t T e x t ( ) ) . t r i m ( ) ; 58 UserName = s i ; PW = s2; a.setText("Waiting for Password V e r i f i c a t i o n ...\n" ACK_AUTH = conn.GetAuthO ; if(ACK_AUTH == true){ 1.setLabel("Logged i n " ) ; 1. setEnabled(false) ; i f ( c o n n . C l i e n t _ I D >= 10 && conn.Client _ I D < 20){ TicketData = new String(UserName + PW + UserName); } else i f ( c o n n . C l i e n t _ I D >= 20 && conn.Client _ I D < 30){ TicketData = new String(PW + UserName + PW) ; } else{ TicketData = new String(UserName + UserName + PW); } GO_AHEAD = conn.GetKeys(); LoadMenu(); remove(p3); a. appendText (" \n\nReady \n\n") ; } else a.setText( "Password NOT V e r i f i e d - Try Again....\n"); } else i f ( e v t . t a r g e t . e q u a l s ( l o f f ) ) { DoLogOff() ; } else i f ( e v t . t a r g e t instanceof Menultem){ if(arg.equals("Record of ID")){ QueryNum=PacketType.SEND_Q11; ReadQuery = true; show_dialog(1); } else if(arg.equals("Record of Name")){ QueryNum=PacketType.SEND_Q2; ReadQuery = true; show_dialog(2); } else if(arg.equals("No Hits of ID")){ QueryNum = PacketType.SEND_Q13; ReadQuery = true; show_dialog(1); } else if(arg.equals("Update CATEGORY")){ QueryNum = PacketType.SEND_Q14; 59 ReadQuery = t r u e ; s h o w _ d i a l o g ( 2 ) ; } e l s e i f ( a r g . e q u a l s ( " U p d a t e CLASS")){ QueryNum = PacketType.SEND_Q12; ReadQuery = t r u e ; s h o w _ d i a l o g ( 2 ) ; } e l s e i f ( a r g . e q u a l s ( " F i l e By Name")){ R e a d F i l e = t r u e ; s h o w _ d i a l o g ( 1 ) ; } e l s e i f ( a r g . e q u a l s ( " I n s e r t U s e r " ) ) { QueryNum = PacketType.SEND_Q16; ReadQuery = t r u e ; s h o w _ d i a l o g ( 3 ) ; } e l s e i f ( a r g . e q u a l s ( " I n s e r t Company")){ QueryNum = PacketType.SEND_Q17; ReadQuery = t r u e ; s h o w _ d i a l o g ( 3 ) ; ' } e l s e i f ( a r g . e q u a l s ( " I n s e r t R e c o r d " ) ) { QueryNum = PacketType.SEND_Q15; ReadQuery = t r u e ; s h o w _ d i a l o g ( 1 0 ) ; } e l s e i f ( a r g . e q u a l s ( " B y I D " ) ) { QueryNum = PacketType.SEND_Q1; ReadQuery = t r u e ; s h o w _ d i a l o g ( 1 ) ; } e l s e i f ( a r g . e q u a l s ( " D e l e t e R e c o r d " ) ) { QueryNum = PacketType.SEND_Q19; ReadQuery = t r u e ; s h o w _ d i a l o g ( 1 ) ; } e l s e i f ( a r g . e q u a l s ( " D e l e t e U s e r " ) ) { QueryNum = PacketType.SEND_Q20; ReadQuery = t r u e ; s h o w _ d i a l o g ( 1 ) ; } e l s e i f ( a r g . e q u a l s ( " D e l e t e Company")){ QueryNum = PacketType.SEND_Q21; ReadQuery = t r u e ; s h o w _ d i a l o g ( 1 ) ; } 60 e l s e i f ( a r g . e q u a l s ( " T o t a l R e c o r d s " ) ) { QueryNum = PacketType.SEND_Q8; ReadQuery = t r u e ; s h o w _ d i a l o g ( 0 ) ; } e l s e i f ( a r g . e q u a l s ( " S h o w ALL U s e r s " ) ) { QueryNum = PacketType.SEND_Q9; ReadQuery = t r u e ; s h o w _ d i a l o g ( 0 ) ; } e l s e i f ( a r g . e q u a l s ( " S h o w ALL Companies")){ QueryNum = PacketType.SEND_Q10; ReadQuery = t r u e ; s h o w _ d i a l o g ( 0 ) ; } e l s e i f ( a r g . e q u a l s ( " S h o w ALL R e c o r d s " ) ) { QueryNum = PacketType.SEND_Q18; ReadQuery = t r u e ; s h o w _ d i a l o g ( 0 ) ; } e l s e i f ( a r g . e q u a l s ( " B y Name")){ QueryNum = PacketType.SEND_Q2; ReadQuery = t r u e ; s h o w _ d i a l o g ( 2 ) ; } e l s e i f ( a r g . e q u a l s ( " B y C a t e g o r y " ) ) ! QueryNum = PacketType.SEND_Q3; ReadQuery = t r u e ; s h o w _ d i a l o g ( 1 ) ; } e l s e i f ( a r g . e q u a l s ( " B y C i t y " ) ) { QueryNum = PacketType.SEND_Q4; ReadQuery = t r u e ; s h o w _ d i a l o g ( 1 ) ; } e l s e i f ( a r g . e q u a l s ( " B y Company name")){ QueryNum = PacketType.SEND_Q5; ReadQuery = t r u e ; s h o w _ d i a l o g ( 1 ) ; } e l s e i f ( a r g . e q u a l s ( " B y C a t e g o r y and Y r s " ) ) { QueryNum = PacketType.SEND_Q6; ReadQuery = t r u e ; s h o w _ d i a l o g ( 2 ) ; } e l s e i f ( a r g . e q u a l s ( " L a s t L o g i n " ) ) { QueryNum = PacketType.SEND_Q7; ReadQuery = t r u e ; 61 s h o w _ d i a l o g ( 1 ) ; } e l s e { pop. .up 11. s e t T e x t ( a r g . t o S t r i n g ( ) pop. .up 12. s e t T e x t ( a r g . t o S t r i n g ( ) pop. -UP 13. s e t T e x t ( a r g . t o S t r i n g ( ) pop. .up 14. s e t T e x t ( a r g . t o S t r i n g ( ) pop. .up 15. s e t T e x t ( a r g . t o S t r i n g ( ) pop. .up 16. s e t T e x t ( a r g . t o S t r i n g ( ) pop. .up 17. s e t T e x t ( a r g . t o S t r i n g ( ) pop. .up 18. s e t T e x t ( a r g . t o S t r i n g ( ) pop. .up 19. s e t T e x t ( a r g . t o S t r i n g ( ) pop. -up 1 1 0 . s e t T e x t ( a r g . t o S t r i n g ( } e l s e r e t u r n s u p e r . a c t i o n ( e v t , a r g ) ; r e t u r n t r u e ; p u b l i c v o i d LoadMenu(){ if(ACK_AUTH == t r u e ) { if(MENU_LEVEL == PacketType.VALID_ADMIN) setMenuBar(AdminMenu); if(MENU_LEVEL == PacketType.VALID_USER) setMenuBar(UserMenu); } } p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { J c l i e n t MainWin = new J c l i e n t ( ) ; M a i n W i n . r e s i z e ( 6 0 0 , 4 0 0 ) ; M a i n W i n . s e t V i s i b l e ( t r u e ) ; } } 62 / / A p p e n d i x 3 : JConn.j ava i m p o r t j a v a . n e t . * ; i m p o r t j a v a . i o . * ; i m p o r t J a v a . u t i l . * ; i m p o r t j ava.awt.*; p u b l i c c l a s s JConn{ p r i v a t e DatagramPacket d g l n P k t ; p r i v a t e DatagramPacket dgOutPkt; p r i v a t e DatagramSocket S e r v e r S o c k e t ; p r i v a t e DatagramSocket K e y S o c k e t ; p r i v a t e i n t r e m o t e P o r t ; p r i v a t e i n t S e r v e r P o r t ; p r i v a t e i n t K e y P o r t ; p r i v a t e I n e t A d d r e s s h o s t A d d r e s s ; p r i v a t e I n e t A d d r e s s r emoteAddress; p r i v a t e I n e t A d d r e s s KeyAddress; p r i v a t e I n e t A d d r e s s S e r v e r A d d r e s s ; p r i v a t e i n t t r a n s f e r C o m p l e t e ; p r i v a t e i n t OutCode; p r i v a t e i n t opcode; p r i v a t e i n t d a t a L e n ; . p r i v a t e D a t a l n p u t S t r e a m d a t a l n ; p u b l i c s t a t i c i n t C l i e n t _ I D ; p r i v a t e S t r i n g c T e x t ; p u b l i c s t a t i c S t r i n g p T e x t ; p r i v a t e S t r i n g sname; p r i v a t e S t r i n g cname; p u b l i c s t a t i c S t r i n g qname; p r i v a t e S t r i n g pkname; p r i v a t e S t r i n g serv_pkname; p r i v a t e S t r i n g skname; p r i v a t e S t r i n g signame; p u b l i c S t r i n g S i g n F i l e ; p r i v a t e J D ecoder dec-p r i v a t e J D B c l i e n t DBC; p r i v a t e l o n g seed = 1999; p r i v a t e b y t e [ ] b u f = new b y t e [ 1 0 0 0 ] ; p r i v a t e s t a t i c b y t e [ ] f i l e n a m e ; p r i v a t e i n t toRead; p r i v a t e F i l e l n p u t S t r e a m f i s ; p u b l i c s t a t i c b o o l e a n ACK_RECIEVED=true; p u b l i c s t a t i c i n t USE_LEVEL = 0; p r i v a t e b o o l e a n SES_KEY = f a l s e ; p r i v a t e b o o l e a n PUB_KEY = f a l s e ; p r i v a t e b o o l e a n KEY_AUTH = f a l s e ; p u b l i c s t a t i c b o o l e a n AUTH = f a l s e ; p r i v a t e b o o l e a n MAKE_MD = f a l s e ; p r i v a t e S t r i n g c h o i c e ; p r i v a t e i n t c l i e n t _ b i n d K e y ; p r i v a t e i n t c l i e n t _ b i n d S e r v e r ; p r i v a t e b o o l e a n CONNECTION_READY = f a l s e ; p u b l i c v o i d m a k e R e q u e s t ( I n e t A d d r e s s d e s t , i n t d e s t P o r t ) { t r y { B y t e A r r a y O u t p u t S t r e a m d a t a b u f l = 63 new B y t e A r r a y O u t p u t S t r e a m ( ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f 1 . w r i t e ( O u t C o d e ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f l . w r i t e ( t r a n s f e r C o m p l e t e ) ; i f ( O u t C o d e == PacketType.SEND_FILE){ d a t a b u f 1 . w r i t e ( f i l e n a m e ) ; } e l s e i f ( O u t C o d e == PacketType.READ_PUBKEY || OutCode == PacketType.READ_SESKEY){ J c l i e n t . a . a p p e n d T e x t ( " S e n d i n g T i c k e t . . . \ n " d a t a b u f 1 . w r i t e ( d e c . p h a s h , 0, d e c . h a s h L e n ) ; } e l s e i f ( O u t C o d e == PacketType.VERIFY_PW || OutCode == PacketType.SEND_Q1 || OutCode == PacketType.SEND_Q2 j j OutCode == PacketType.SEND_Q3 j j OutCode == PacketType.SEND_Q4 || OutCode == PacketType.SEND_Q5 || OutCode == PacketType.SEND_Q6 jj OutCode == PacketType.SEND_Q7 || OutCode == PacketType.SEND_Q11 j | OutCode == PacketType.SEND_Q12 j j OutCode == PacketType.SEND_Q13 j j OutCode == PacketType.SEND_Q14 j j OutCode == PacketType.SEND_Q15 j j OutCode == PacketType.SEND_Q16 || OutCode == PacketType.SEND_Q17 j j OutCode == PacketType.SEND_Q8 || OutCode == PacketType.SEND_Q9 jj OutCode == PacketType.SEND_Q10 || OutCode == PacketType.SEND_Q18 || OutCode == PacketType.SEND_Q19 || OutCode == PacketType.SEND_Q20 || OutCode == PacketType.SEND_Q21){ S t r i n g Q u e r y F i l e = qname; i f ( O u t C o d e != PacketType.VERIFY_PW){ d e c . E n c o d e F i l e ( Q u e r y F i l e , cname); Q u e r y F i l e = cname; } F i l e l n p u t S t r e a m q i n = new F i l e l n p u t S t r e a m ( Q u e r y F i l e ) b y t e [ ] q = new b y t e [ q i n . a v a i l a b l e ( ) ] ; q i n . r e a d ( q ) ; q i n . c l o s e ( ) ; d a t a b u f l . w r i t e ( q ) ; } i f ( O u t C o d e == PacketType.VERIFY_PW){ d a t a b u f 1 . w r i t e ( d e c . p h a s h , 0, d e c . h a s h L e n ) ; } b y t e [ ] b u f l = d a t a b u f 1 . t o B y t e A r r a y ( ) ; . dgOutPkt = new D a t a g r a m P a c k e t ( b u f 1 , b u f l . l e n g t h , d e s t , d e s t P o r t ) ; } c a t c h ( E x c e p t i o n e){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r : " + e + " \ n " ) ; 64 } } p u b l i c v o i d makeOutPacket(byte[] b, i n t o_code, i n t f l a g , InetAddress dest, i n t destPort){ tr y { ByteArrayOutputStream databuf = new ByteArrayOutputStream (-) ; dat a b u f . w r i t e ( 0 ) ; databuf.write(o_code); d a t a b u f . w r i t e ( 0 ) ; d a t a b u f . w r i t e ( f l a g ) ; d a t a b u f . w r i t e ( b ) ; byte[] buf2 = databuf.toByteArray(); dgOutPkt = new DatagramPacket(buf2, buf2.length, dest, d e s t P o r t ) ; }catch(Exception e){ S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e ) ; } } p u b l i c v o i d requestAuth(){ tr y { dec.MakeMessageDigest(Jclient.PW); BufferedWriter bwin = new BufferedWriter( new FileWriter(qname)); bwin.write(Jclient.UserName, 0, Jclient.UserName.length()); bwin.newLine(); b w i n . c l o s e ( ) ; }catch(Exception e){ Jclient.a.appendText("Error: " + e ) ; } } p u b l i c v o i d retrieveACKPacket(){ t r y { byte[] buf2 = new byte[1000]; remotePort = d g l n P k t . g e t P o r t ( ) ; remoteAddress = dglnPkt.getAddress(); d a t a l n = new DatalnputStream( new ByteArraylnputStream(dglnPkt.getData())); dataLen = dglnPkt.getLength(); opcode = da t a l n . r e a d S h o r t ( ) ; transferComplete = da t a l n . r e a d S h o r t ( ) ; if(opcode == PacketType.ACK_QUERY || opcode == PacketType.SEND_CLIENT_PORT || opcode == PacketType.READY){ ACK_RECIEVED=true; Jclient.a.appendText("\nReady....\n"); } e l s e if(opcode == PacketType.ACK){ ACK_RECIEVED=true; } e l s e if(opcode == PacketType.ACK_PUBKEY && remotePort == ServerPort){ 65 ACK_RECIEVED=true; J c l i e n t . a . a p p e n d T e x t ( " R e q u e s t i n g P u b l i c K e y . . . \ n " ) ; } e l s e i f ( o p c o d e == PacketType.ACK_SESKEY && r e m o t e P o r t == S e r v e r P o r t ) { ACK_RECIEVED=true; J c l i e n t . a . a p p e n d T e x t ( " R e q u e s t i n g S e s s i o n K e y . . . \ n " ) ; } e l s e i f ( o p c o d e == PacketType.ACK_SIGN && r e m o t e P o r t == S e r v e r P o r t ) { ACK_RECIEVED=true; J c l i e n t . a . a p p e n d T e x t ( " V e r i f y S i g n a t u r e . . . . \ n " ) d a t a l n . r e a d F u l l y ( b u f 2 , 0, d a t a L e n - PacketType.EMPTY_PKT); F i l e O u t p u t S t r e a m s o u t = new F i l e O u t p u t S t r e a m ( s n a m e ) ; s o u t . w r i t e ( b u f 2 , 0, d a t a L e n - PacketType.EMPTY_PKT); s o u t . c l o s e ( ) ; d e c . D e c o d e S i g n a t u r e ( s n a m e ) ; } e l s e i f ( o p c o d e == PacketType.ACK_PW){ ACK_RECIEVED=true; J c l i e n t . a . a p p e n d T e x t ( " P a s s w o r d V e r i f i e d . \ n " ) ; i f ( t r a n s f e r C o m p l e t e == PacketType.VALID_USER){ USE_LEVEL = PacketType.VALID_USER; J c l i e n t . M E N U _ L E V E L = PacketType.VALID_USER; } e l s e i f ( t r a n s f e r C o m p l e t e == PacketType.VALID_ADMIN){ USE_LEVEL = PacketType.VALID_ADMIN,• Jc l i e n t . M E N U _ L E V E L = PacketType.VALID_ADMIN; } e l s e { J c l i e n t . a . a p p e n d T e x t ( "INVALID USER LEVEL\n"); S y s t e m . e x i t ( 1 ) ; } } e l s e i f ( o p c o d e == PacketType.ACK_PUBKEY && r e m o t e P o r t == K e y P o r t ) { ACK_RECIEVED=true; PUB_KEY = t r u e ; J c l i e n t . a . a p p e n d T e x t ( " P u b l i c Key R e c e i v e d . \ n " ) b y t e t] pbuf = new b y t e [ d a t a L e n - PacketType.EMPTY_PKT] d a t a l n . r e a d F u l l y ( p b u f , 0, d a t a L e n - PacketType.EMPTY_PKT); F i l e O u t p u t S t r e a m pout = new F i l e O u t p u t S t r e a m ( p k n a m e ) ; p o u t . w r i t e ( p b u f , 0, d a t a L e n - PacketType.EMPTY_PKT); p o u t . c l o s e ( ) ; 66 d e c . D e c o d e P u b l i c K e y ( p k n a m e ) ; } e l s e i f ( o p c o d e == P a c k e t T y p e . A C K _ S E S K E Y && r e m o t e P o r t == K e y P o r t ) { A C K _ R E C I E V E D = t r u e ; SES_KEY = t r u e ; J c l i e n t . a . a p p e n d T e x t ( " S e s s i o n Key R e c e i v e d . \ n " ) ; b y t e [ ] s b u f = new b y t e [ d a t a L e n - P a c k e t T y p e . E M P T Y _ P K T ] d a t a l n . r e a d F u l l y ( s b u f , 0, d a t a L e n - P a c k e t T y p e . E M P T Y _ P K T ) ; F i l e O u t p u t S t r e a m s k o u t = new F i l e O u t p u t S t r e a m ( s k n a m e ) ; s k o u t . w r i t e ( s b u f , 0, d a t a L e n - P a c k e t T y p e . E M P T Y _ P K T ) ; s k o u t . c l o s e ( ) ; d e c . D e c o d e S e s s i o n K e y ( s k n a m e ) ; } e l s e { A C K _ R E C I E V E D = f a l s e ; J c l i e n t . a . a p p e n d T e x t ( " E r r o r O c c u r e d i n ACK R e t r i e v a l \ n " ) ; S y s t e m . e x i t(1); } } c a t c h ( E x c e p t i o n e){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r from r e t r i e v e A C K P a c k e t : + e + " \ n " ) ; } } p u b l i c b o o l e a n r e t r i e v e C i p h e r P a c k e t ( ) { i n t s eg = 1; t r y { t r a n s f e r C o m p l e t e = 0; F i l e O u t p u t S t r e a m out = new F i l e O u t p u t S t r e a m ( c T e x t ) ; w h i l e ( t r a n s f e r C o m p l e t e == 0){ S e r v e r S o c k e t . r e c e i v e ( d g l n P k t ) ; b y t e [ ] buf2 = new b y t e[1000]; r e m o t e P o r t = d g l n P k t . g e t P o r t ( ) ; r e m o t e A d d r e s s = d g l n P k t . g e t A d d r e s s ( ) ; d a t a l n = new D a t a I n p u t S t r e a m ( new B y t e A r r a y l n p u t S t r e a m ( d g l n P k t . g e t D a t a ( ) ) ) ; i n t opcode = d a t a l n . r e a d S h o r t ( ) ; J c l i e n t . a . a p p e n d T e x t ( " S e g e m e n t # " + seg + " r e c e i v e d \ n " ) ; seg++; t r a n s f e r C o m p l e t e = d a t a l n . r e a d S h o r t ( ) ; d a t a L e n = d g l n P k t . g e t L e n g t h ( ) ; i f ( o p c o d e == P a c k e t T y p e . A C K ) { AC K _ R E C I E V E D = t r u e ; J c l i e n t . a . a p p e n d T e x t ( " F i n a l Segement " + " R e c e i v e d - T r a n s f e r C o m p l e t e \ n " ) o u t . c l o s e ( ) ; r e t u r n t r u e ; } 67 else if(opcode == PacketType.SEND_ENCODED_FILE opcode == PacketType.SEND_ENCODED_QUERY){ ACK_RECIEVED=true; dataln.readFully(buf2, 0, dataLen - PacketType.EMPTY_PKT); out.write(buf2, 0, dataLen - PacketType.EMPTY_PKT); } else{ ACK_RECIEVED=false; Jclient.a.appendText( "Error i n Ciphertext R e t r i e v a l \ n " ) ; return f a l s e ; } } return f a l s e ; }catch(Exception e){ Jclient.a.appendText("Error: " + e + "\n"); return f a l s e ; } } pu b l i c boolean RequestFile(String FileToRequest){ try{ Jclient.a.appendText("File Requested: " + FileToRequest + "\n"); filename = new byte[FileToRequest.length() +1]; FileToRequest.getBytes(0, FileToRequest.length(), filename,0); dglnPkt = new DatagramPacket(buf, buf.length); OutCode = PacketType.SEND_FILE; transferComplete = 1; makeRequest(ServerAddress, ServerPort); ServerSocket.send(dgOutPkt); ACK_RECIEVED = f a l s e ; ServerSocket.receive(dglnPkt); retrieveACKPacket(); if(ACK_RECIEVED == true) return true; else return f a l s e ; }catch(Exception e){ Jclient.a.appendText("Error: " + e + "\n"); return f a l s e ; } } public boolean SendSign(){ try{ dec.GenerateSignature(SignFile, signame); FilelnputStream s f i s = new FilelnputStream(signame); byte[] s = new b y t e [ s f i s . a v a i l a b l e ( ) ] ; s f i s . r e a d ( s ) ; s f i s . c l o s e ( ) ; transferComplete = 1; makeOutPacket(s, PacketType.ACK_SIGN, 68 t r a n s f e r C o m p l e t e , S e r v e r A d d r e s s , S e r v e r P o r t ) S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; ACK_RECIEVED = f a l s e ; S e r v e r S o c k e t . r e c e i v e ( d g l n P k t ) ; r e t r i e v e A C K P a c k e t ( ) ; if(ACK_RECIEVED == t r u e ) r e t u r n t r u e ; e l s e r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ) { J c l i e n t . a . a p p e n d T e x t ( " E r r o r : " + e + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n R e q u e s t Q u e r y ( i n t code){ . t r y { OutCode = code; i f ( c o d e == PacketType.VERIFY_PW){ r e q u e s t A u t h ( ) ; } e l s e i f ( c o d e == PacketType.SEND_Q1 || code == PacketType SEND. -Q2 1 1 code == PacketType SEND_ -Q3 1 1 code == PacketType SEND_ -Q4 | | code == PacketType SEND_ -Q5 1 1 code == PacketType SEND_ -Q6 1 1 code == PacketType SEND. -Q7 1 1 code == PacketType SEND. - Q l l 1 1 code == PacketType SEND. -Q12 1 1 code == PacketType SEND. -013 | | code == PacketType SEND. -Q14 | | code == PacketType SEND. -Q15 | | code == PacketType SEND. _Q16 j j code == PacketType SEND. -Q17 j | code == PacketType SEND. -Q8 ||. code == PacketType SEND. -Q9 1 1 code == PacketType SEND. - Q i o I I code == PacketType SEND. .Q18 j | code == PacketType SEND. _Q19 | j code == PacketType SEND. -Q20 j | code == PacketType SEND. .Q21) { OutCode = code; i f ( O u t C o d e == PacketType.SEND_Q12 OutCode == PacketType.SEND_Q14 || OutCode == PacketType.SEND_Q15 j j OutCode == PacketType.SEND_Q16 j j OutCode == PacketType.SEND_Q17 jj OutCode == PacketType.SEND_Q19 jj OutCode == PacketType.SEND_Q20 j j OutCode == PacketType.SEND_Q21){ i f ( S e n d S i g n ( ) == t r u e ) J c l i e n t . a . a p p e n d T e x t ( " \ n D i g i t a l S i g n a t u r e S e n t \ n " ) ; e l s e OutCode = PacketType.ERROR; } 69 } e l s e { OutCode = PacketType.ERROR; r e t u r n f a l s e ; } d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; t r a n s f e r C o m p l e t e = 1; m a k e R e q u e s t ( S e r v e r A d d r e s s , S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; ACK_RECIEVED = f a l s e ; S e r v e r S o c k e t . r e c e i v e ( d g l n P k t ) ; r e t r i e v e A C K P a c k e t ( ) ; if(ACK_RECIEVED == t r u e ) r e t u r n t r u e ; e l s e r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r : " + e + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n R e q u e s t K e y ( i n t code){ t r y { if(ACK_RECIEVED == t r u e ) { d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) OutCode = code; m a k e R e q u e s t ( S e r v e r A d d r e s s , S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; ACK_RECIEVED = f a l s e ; S e r v e r S o c k e t . r e c e i v e ( d g l n P k t ) ; r e t r i e v e A C K P a c k e t ( ) ; if(ACK_RECIEVED == t r u e ) r e t u r n t r u e ; e l s e r e t u r n f a l s e ; } e l s e { J c 1 i e n t . a . a p p e n d T e x t ( " S o r r y ! c a n n o t REQUEST KEY\n"); r e t u r n f a l s e ; } } c a t c h ( E x c e p t i o n e){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r : " + e + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n R e a d K e y f i n t code){ t r y { if(ACK_RECIEVED == t r u e ) { d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) OutCode = code; t r a n s f e r C o m p l e t e = 1; makeRequest(KeyAddress, K e y P o r t ) ; K e y S o c k e t . s e n d ( d g O u t P k t ) ; 70 ACK_RECIEVED = f a l s e ; K e y S o c k e t . r e c e i v e ( d g l n P k t ) ; r e t r i e v e A C K P a c k e t ( ) ; if(ACK_RECIEVED == t r u e ) r e t u r n t r u e ; } e l s e J c l i e n t . a . a p p e n d T e x t ( " S o r r y ! cannot READ KEY\n"); r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e l ) { J c l i e n t . a . a p p e n d T e x t ( " E r r o r : " + e l + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c v o i d W r i t e P u b K e y ( ) { t r y { d e c . G e n e r a t e K e y P a i r ( s e e d ) ; d e c . E n c o d e P u b l i c K e y ( s e r v _ p k n a m e ) ; t r a n s f e r C o m p l e t e = 1; OutCode = PacketType.WRITE_PUBKEY; F i l e l n p u t S t r e a m k e y f i s = new F i l e l n p u t S t r e a m ( s e r v _ p k n a m e ) ; b y t e [ ] encKey = new b y t e [ k e y f i s . a v a i l a b l e ( ) ] ; k e y f i s . r e a d ( e n c K e y ) ; k e y f i s . c l o s e ( ) ; m a k e O u t P a c k e t ( e n c K e y , O u t C o d e , t r a n s f e r C o m p l e t e , K e y A d d r e s s , K e y P o r t ) ; K e y S o c k e t . s e n d ( d g O u t P k t ) ; ACK_RECIEVED = f a l s e ; d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; K e y S o c k e t . r e c e i v e ( d g l n P k t ) ; r e t r i e v e A C K P a c k e t ( ) ; i f ( o p c o d e == PacketType.ACK){ J c l i e n t . a . a p p e n d T e x t ( " P u b l i c Key Sent t o S e r v e r . \ n " ) ; ACK_RECIEVED = t r u e ; } } c a t c h ( E x c e p t i o n e){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r From Writ e P u b K e y : " + e ) ; } } p u b l i c b o o l e a n SendPubKey(){ t r y { i f(ACK_RECIEVED == t r u e ) { W r i t e P u b K e y ( ) ; d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; OutCode = PacketType.READ_PUBKEY; m a k e R e q u e s t ( S e r v e r A d d r e s s , S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; ACK_RECIEVED = f a l s e ; S e r v e r S o c k e t . r e c e i v e ( d g l n P k t ) ; r e t r i e v e A C K P a c k e t ( ) ; if(ACKJRECIEVED == t r u e ) r e t u r n t r u e ; 71 e l s e r e t u r n f a l s e ; } e l s e { J c l i e n t . a . a p p e n d T e x t ( " S o r r y ! c a n n o t SEND PUBLIC KEY\n"); • r e t u r n f a l s e ; } } c a t c h ( E x c e p t i o n e ){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r : " + e + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n G e t E n c o d e d R e s u l t ( i n t code){ t r y { if(ACK_RECIEVED == t r u e ) { . d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) OutCode = code; t r a n s f e r C o m p l e t e = 1; m a k e R e q u e s t ( S e r v e r A d d r e s s , S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; ACK_RECIEVED = f a l s e ; i f ( r e t r i e v e C i p h e r P a c k e t ( ) == t r u e ) r e t u r n t r u e ; e l s e r e t u r n f a l s e ; } e l s e { J c l i e n t . a . a p p e n d T e x t ( " S o r r y ! cannot r e q u e s t r e s u l t s \ n " ) ; r e t u r n f a l s e ; } } c a t c h ( E x c e p t i o n e2){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r from G e t E n c o d e d R e s u l t : + e2 + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n G e t S i g n ( ) { t r y { if(ACK_RECIEVED == t r u e ) { d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) OutCode = PacketType.SEND_SIGN; t r a n s f e r C o m p l e t e = 1; m a k e R e q u e s t ( S e r v e r A d d r e s s , S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; ACK_RECIEVED = f a l s e ; S e r v e r S o c k e t . r e c e i v e ( d g l n P k t ) ; r e t r i e v e A C K P a c k e t ( ) ; i f(ACK_RECIEVED == t r u e ) { d e c . D e c o d e F i l e ( c T e x t , p T e x t ) ; d e c . D e c o d e S i g n a t u r e ( s n a m e ) ; d e c . V e r i f y S i g n a t u r e ( p T e x t ) ; r e t u r n t r u e ; 72 } r e t u r n f a l s e ; } e l s e { J c l i e n t . a . a p p e n d T e x t ( " S o r r y ! c a n n o t REQUEST SIGNATURE\n"); r e t u r n f a l s e ; } } c a t c h ( E x c e p t i o n e ){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r from G e t S i g n : " + e + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n G e t K e y s ( ) { t r y { i f ( S E S _ K E Y == f a l s e && PUB_KEY == f a l s e ) { J c l i e n t . a . a p p e n d T e x t ( " M a k i n g T i c k e t . . . \ n " ) ; d e c . M a k e M e s s a g e D i g e s t ( J c l i e n t . T i c k e t D a t a ) ; if(RequestKey(PacketType.REQ_PUBKEY) == t r u e ) { if(ReadKey(PacketType.READ_PUBKEY) == t r u e ) { i f(RequestKey(PacketType.REQ_SESKEY ) == t r u e ) { i f ( R e a d K e y ( PacketType.READ_SESKEY) == t r u e ) { i f ( S e n d P u b K e y ( ) == t r u e ) r e t u r n t r u e ; e l s e r e t u r n f a l s e ; } e l s e { J c l i e n t . a . a p p e n d T e x t ( " E r r o r : r e a d i n g " + "SESSION KEY\n"); r e t u r n f a l s e ; } } e l s e { J c l i e n t . a . a p p e n d T e x t ( " E r r o r : r e q u e s t i n g SESSION" + " KEY\n"); r e t u r n f a l s e ; } } e l s e { J c l i e n t . a . a p p e n d T e x t ( " E r r o r : " + " r e a d i n g PUBLIC K E Y \ n " ) ; r e t u r n f a l s e ; } } e l s e { J c l i e n t . a . a p p e n d T e x t ( " E r r o r : r e q u e s t i n g " + " PUBLIC KEY\n"); 73 r e t u r n f a l s e ; } } else{ Jclient.a.appendText( "\n\nKEYS already present\n"); r e t u r n t r u e ; } }catch(Exception e2){ Jclient.a.appendText("Error: " + e2 + "\n"),-r e t u r n f a l s e ; } } p u b l i c v o i d Quit_Programe(){ tr y { if(GoodBye() == true) System.exit(1); e l s e Jclient.a.appendText("Cannot Quit !!!!\n"); }catch(Exception e){ Jclient.a.appendText("Error: " + e + "\n"); } } p u b l i c boolean GoodBye(){ tr y { OutCode = PacketType.GOOD_BYE; transferComplete = 1; makeRequest(ServerAddress, S e r v e r P o r t ) ; ServerSocket.send(dgOutPkt); ACK_RECIEVED = f a l s e ; S e rverSocket.receive(dglnPkt); retrieveACKPacket(); if(ACK_RECIEVED == true){ Jclient.a.appendText( "\n*** GOODBYE ** * \ n " ) ; r e t u r n t r u e ; } r e t u r n f a l s e ; }catch(Exception e){ Jclient.a.appendText("Error: " + e + "\n"); r e t u r n f a l s e ; } } p u b l i c boolean NewUser(){ tr y { AUTH = f a l s e ; KEY_AUTH = f a l s e ; SES_KEY = f a l s e ; PUB_KEY = f a l s e ; MAKE_MD = f a l s e ; if(GoodBye() == true){ dec = new JDecoder(); DBC = new J D B c l i e n t O ; 74 r e t u r n t r u e ; } e l s e r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r : " + e + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n G e t A u t h ( ) { t r y { if(CONNECTION_READY == f a l s e ) G e t C o n n e c t i o n ( ) ; ReguestQuery(PacketType.VERIFY_PW); if(ACK_RECIEVED == t r u e ) { r e t u r n t r u e ; } e l s e { J c l i e n t . a . a p p e n d T e x t ( " P a s s w o r d NOT v a l i d \ n " ) ; r e t u r n f a l s e ; } } c a t c h ( E x c e p t i o n e ){ J c l i e n t . a . a p p e n d T e x t ( " E r r o r from G e t A u t h : " + e + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c v o i d C o n n e c t T o S e r v e r ( ) { t r y { S e r v e r P o r t = 2000; S e r v e r S o c k e t = new D a t a g r a m S o c k e t ( ) ; h o s t A d d r e s s = InetAddress.getByName("142.103.10.55") KeyAddress = InetAddress.getByName("142.103.10.57"); S e r v e r A d d r e s s = InetAddress.getByName("142.103.10.61"); OutCode = PacketType.SEND_CLIENT_PORT; m a k e R e q u e s t ( S e r v e r A d d r e s s , S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; S y s t e m . o u t . p r i n t l n ( "Send c o n n e c t i o n t o s e r v e r " ) ; S e r v e r S o c k e t . r e c e i v e ( d g l n P k t ) ; d a t a l n = new D a t a l n p u t S t r e a m ( new B y t e A r r a y l n p u t S t r e a m ( d g l n P k t . g e t D a t a ( ) ) ) ; C l i e n t _ I D = d a t a l n . r e a d S h o r t ( ) ; c l i e n t _ b i n d S e r v e r = d a t a l n . r e a d S h o r t ( ) ; c l i e n t _ b i n d K e y = d a t a l n . r e a d S h o r t ( ) ; S e r v e r P o r t = d a t a l n . r e a d S h o r t ( ) ; K e y P o r t = d a t a l n . r e a d S h o r t ( ) ; S e r v e r S o c k e t . c l o s e ( ) ; c l i e n t _ b i n d S e r v e r += 1025; c l i e n t _ b i n d K e y += 1025; S e r v e r P o r t += 1025; K e y P o r t += 1025; S e r v e r S o c k e t = new D a t a g r a m S o c k e t ( c l i e n t _ b i n d S e r v e r ) 75 KeySocket = new D a t a g r a m S o c k e t ( c l i e n t _ b i n d K e y ) ; S y s t e m . o u t . p r i n t l n ( " S e r v e r p o r t = " + S e r v e r P o r t + ", C l i e n t _ I D = " + C l i e n t _ I D ) ; S y s t e m . o u t . p r i n t l n ( " S e r v e r B i n d e d @ " + c l i e n t _ b i n d S e r v e r + " Key B i n d e d @ " + c l i e n t _ b i n d K e y ) ; d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ;; K e y S o c k e t . r e c e i v e ( d g l n P k t ) ; d a t a l n = new D a t a I n p u t S t r e a m ( new B y t e A r r a y l n p u t S t r e a m ( d g l n P k t . g e t D a t a ( ) ) ) opcode = d a t a l n . r e a d S h o r t ( ) ; i f ( o p c o d e == PacketType.READY){ System.out.println("CONNECTION READY !!!!!") CONNECTION_READY = t r u e ; } } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From Connect: " + e) ; } } p u b l i c J C o n n ( ) { } p u b l i c v o i d G e t C o n n e c t i o n ( ) { t r y { CONNECTION_READY = f a l s e ; S y s t e m . o u t . p r i n t l n ( " C o n n e c t i n g t o s e r v e r " ) ; C o n n e c t T o S e r v e r ( ) ; J c l i e n t . a . a p p e n d T e x t ( " C l i e n t s t a r t i n g . . . \ n " ) ; c T e x t = C l i e n t _ I D + " C i p h e r T e x t " ; p T e x t = C l i e n t _ I D + " P l a i n T e x t " ; sname = C l i e n t _ I D + " S i g n " ; cname = C l i e n t _ I D + " c i p h q u e r y " ; qname = C l i e n t _ I D + " q u e r y l n " ; pkname = C l i e n t _ I D + "PK"; skname = C l i e n t _ I D + "SK"; serv_pkname = C l i e n t _ I D + "serv_PK"; signame = C l i e n t _ I D + " s e r v _ s i g " ; dec = new J D e c o d e r ( ) ; DBC = new J D B c l i e n t O ; } c a t c h ( E x c e p t i o n e ) { J c 1 i e n t . a . a p p e n d T e x t ( "FAILURE IN CONNECTION, QUITING\n" + e ) ; e . p r i n t S t a c k T r a c e ( ) ; S y s t e m . e x i t ( 1 ) ; } } } 76 //Appendix 4 : J D e c o d e r . j ava i m p o r t j a v a . i o . * ; i m p o r t J a v a . s e c u r i t y . * ; i m p o r t J a v a . s e c u r i t y . s p e c . * ; i m p o r t j a v a . l a n g . * ; i m p o r t j a v a x . c r y p t o . C i p h e r ; i m p o r t j a v a x . c r y p t o . K e y G e n e r a t o r ; i m p o r t j a v a x . c r y p t o . S e c r e t K e y ; i m p o r t j a v a x . c r y p t o . S e c r e t K e y F a c t o r y ; i m p o r t j a vax.crypto.spec.DESKeySpec; i m p o r t j a v a x . c r y p t o . C i p h e r l n p u t S t r e a m ; i m p o r t j a v a x . c r y p t o . C i p h e r O u t p u t S t r e a m ; c l a s s JDecoder{ p u b l i c P r o v i d e r sunJCE; p u b l i c s t a t i c i n t s; p u b l i c Key S e s s i o n K e y ; p u b l i c P u b l i c K e y pubKey; p u b l i c P u b l i c K e y servPK; p u b l i c P r i v a t e K e y p r i v ; p u b l i c S i g n a t u r e s i g ; p u b l i c b y t e [ ] s i g T o V e r i f y ; p u b l i c b y t e [ ] r e a l S i g ; p u b l i c b y t e [ ] pkey; p u b l i c b y t e [ ] phash; p u b l i c i n t hashLen; p u b l i c J D e c o d e r ( ) { t r y { sunJCE = new c o m . s u n . c r y p t o . p r o v i d e r . S u n J C E ( ) ; s = S e c u r i t y . a d d P r o v i d e r ( s u n J C E ) ; } c a t c h ( E x c e p t i o n g){ J c l i e n t . a . a p p e n d T e x t ( " P r o v i d e r E r r o r : "'+ g + "\n") } } p u b l i c v o i d D e c o d e S e s s i o n K e y ( S t r i n g sname){ t r y { F i l e l n p u t S t r e a m f i s = new F i l e l n p u t S t r e a m ( s n a m e ) ; b y t e [ ] encSKey = new b y t e [ f i s . a v a i l a b l e ( ) ] ; f i s . r e a d ( e n c S K e y ) ; f i s . c l o s e ( ) ; DESKeySpec sesKeySpec = new DESKeySpec(encSKey); S e c r e t K e y F a c t o r y s k e y F a c t o r y = S e c r e t K e y F a c t o r y . g e t l n s t a n c e ( " D E S " ) ; S e s s i o n K e y = s k e y F a c t o r y . g e n e r a t e S e c r e t ( s e s K e y S p e c ) } c a t c h ( E x c e p t i o n e) { J c l i e n t . a . a p p e n d T e x t ( "Caught E x c e p t i o n " + e . t o S t r i n g O + " \ n " ) ; } } p u b l i c v o i d D e c o d e P u b l i c K e y ( S t r i n g pname){ t r y { F i l e l n p u t S t r e a m f i s = new F i l e l n p u t S t r e a m ( p n a m e ) ; b y t e [ ] encKey = new b y t e [ f i s . a v a i l a b l e ( ) ] ; 77 f i s . r e a d ( e n c K e y ) ; f i s . c l o s e ( ) ; X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); K e y F a c t o r y k e y F a c t o r y = K e y F a c t o r y . g e t l n s t a n c e ( " D S A " "SUN"); pubKey = k e y F a c t o r y . g e n e r a t e P u b l i c ( p u b K e y S p e c ) ; } c a t c h ( E x c e p t i o n e) { J c l i e n t . a . a p p e n d T e x t ( "Caught E x c e p t i o n " + e . t o S t r i n g ( ) + " \ n " ) ; } } p u b l i c v o i d D e c o d e S i g n a t u r e ( S t r i n g SigName) { t r y { F i l e l n p u t S t r e a m s i g f i s = new F i l e l n p u t S t r e a m ( S i g N a m e ) ; s i g T o V e r i f y = new b y t e [ s i g f i s . a v a i l a b l e ( ) ] ; s i g f i s . r e a d ( s i g T o V e r i f y ) ; • s i g f i s . c l o s e ( ) ; s i g = S i g n a t u r e . g e t l n s t a n c e ( " S H A l w i t h D S A " , "SUN"); s i g . i n i t V e r i f y ( p u b K e y ) ; } c a t c h ( E x c e p t i o n e) { J c l i e n t . a . a p p e n d T e x t ( "Caught E x c e p t i o n " + e . t o S t r i n g ( ) + " \ n " ) ; } } p u b l i c v o i d D e c o d e F i l e ( S t r i n g I n F i l e N a m e , S t r i n g O u t F i l e N a m e ) { t r y { C i p h e r d e c i p h e r = C i p h e r . g e t l n s t a n c e ( " D E S " , "SunJCE") b y t e [ ] b u f f e r = new b y t e [ 1 0 0 ] ; I h p u t S t r e a m i n ; O u t p u t S t r e a m o u t ; i n t b y t e s _ r e a d ; d e c i p h e r . i n i t ( C i p h e r . DECRYPT_MODE,' S e s s i o n K e y ) ; i n = new C i p h e r I n p u t S t r e a m ( new F i l e l n p u t S t r e a m ( I n F i l e N a m e ) , d e c i p h e r ) ; o u t = new F i l e O u t p u t S t r e a m ( O u t F i l e N a m e ) ; w h i l e ( ( b y t e s _ r e a d = i n . r e a d ( b u f f e r ) ) != -1){ o u t . w r i t e ( b u f f e r , 0, b y t e s _ r e a d ) ; }; i n . c l o s e ( ) ; o u t . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e) { J c l i e n t . a . a p p e n d T e x t ( "Caught E x c e p t i o n " + e . t o S t r i n g ( ) + " \ n " ) ; } } p u b l i c v o i d V e r i f y S i g n a t u r e ( S t r i n g PlainName){ t r y { F i l e l n p u t S t r e a m P l ' a i n f i s = new F i l e l n p u t S t r e a m ( P l a i n N a m e ) ; b y t e [ ] b u f f e r l = new b y t e [ 1 0 0 ] ; . i n t l e n ; 78 w h i l e ( P l a i n f i s . a v a i l a b l e ( ) != 0){ l e n = P l a i n f i s . r e a d ( b u f f e r l ) ; s i g . u p d a t e ( b u f f e r l , 0, l e n ) ; }; P l a i n f i s . c l o s e ( ) ; b o o l e a n v e r i f i e s = s i g . v e r i f y ( s i g T o V e r i f y ) ; J c l i e n t . a . a p p e n d T e x t ( " s i g n a t u r e v e r i f i e s : " + v e r i f i e s + " \ n " ) ; } c a t c h ( E x c e p t i o n e) { J c l i e n t . a . a p p e n d T e x t ( "Caught E x c e p t i o n " + e . t o S t r i n g O + " \ n " ) ; } } p u b l i c v o i d M a k e M e s s a g e D i g e s t ( S t r i n g pw){ t r y { M e s s a g e D i g e s t sha = M e s s a g e D i g e s t . g e t l n s t a n c e ( " S H A " ) b y t e [ ] pbuf = new b y t e [ p w . l e n g t h ( ) + 1]; pw.getBytes ( 0 , p w . l e n g t h ( ) , p b u f, 0); s h a . u p d a t e ( p b u f ) ; b y t e [ ] h a sh = s h a . d i g e s t ( ) ; hashLen = h a s h . l e n g t h ; phash = hash; } c a t c h ( E x c e p t i o n e)" { J c l i e n t . a . a p p e n d T e x t ( "Caught E x c e p t i o n " + e . t o S t r i n g ( ) ) ; } } p u b l i c v o i d E n c o d e F i l e ( S t r i n g f i l e n a m e , S t r i n g O u t F i l e ) { t r y { C i p h e r c i p h e r = C i p h e r . g e t l n s t a n c e ( " D E S " , "SunJCE"); c i p h e r . i n i t ( C i p h e r . E N C R Y P T _ M O D E , S e s s i o n K e y ) ; I n p u t S t r e a m i n ; Out p u t S t r e a m o u t ; b y t e [ ] b u f f e r = new byte [100]; i n t b y t e s _ r e a d ; i n = new F i l e l n p u t S t r e a m ( f i l e n a m e ) ; out = new C i p h e r O u t p u t S t r e a m ( new F i l e O u t p u t S t r e a m ( O u t F i l e ) , c i p h e r ) ; w h i l e ( ( b y t e s _ r e a d = i n . r e a d ( b u f f e r ) ) != -1){ o u t . w r i t e ( b u f f e r , 0, b y t e s _ r e a d ) ; }; i n . c l o s e ( ) ; o u t . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( "Caught e x c e p t i o n " + e . t o S t r i n g ( ) ) ; } } p u b l i c v o i d G e n e r a t e K e y P a i r ( l o n g u s e r s e e d ) { t r y { K e y P a i r G e n e r a t o r P a i r G e n = K e y P a i r G e n e r a t o r . g e t l n s t a n c e ( " D S A " , "SUN"); l o n g s e e d = u s e r s e e d ; SecureRandom random = 79 SecureRandom.getlnstance("SHA1PRNG", "SUN") random.setSeed(seed) ; PairGen.initialize(1024, random); KeyPair p a i r = PairGen.generateKeyPair(); p r i v = p a i r . g e t P r i v a t e ( ) ; servPK = p a i r . g e t P u b l i c ( ) ; }catch (Exception e){ System.err.println( "Caught exception " + e . t o S t r i n g ( ) ) ; } } public void EncodePublicKey(String pkname){ try{ pkey = servPK.getEncoded(); FileOutputStream pkeyfos = new FileOutputStream(pkname); pkeyfos.write(pkey); pkeyfos.close() ; }catch (Exception e){ System.err.println( "Caught exception " + e . t o S t r i n g ( ) ) ; } } public void GenerateSignature(String I n f i l e , S t r i n g O u t f i l e ) { try{ Signature dsa = Signature.getlnstance("SHAlwithDSA "SUN"); d s a . i n i t S i g n ( p r i v ) ; FilelnputStream f i s l = new FilelnputStream(Infile) byte[] b u f f e r l = new byte[100]; i n t len; w h i l e ( f i s l . a v a i l a b l e ( ) != 0){ len = f i s l . r e a d ( b u f f e r l ) ; dsa.update(bufferl, 0, len) ; }; f i s l . c l o s e ( ) ; r e a l S i g = dsa.sign(); FileOutputStream sigfos = new FileOutputStream(Outfile); s i g f o s . w r i t e ( r e a l S i g ) ; s i g f o s . c l o s e (); }catch (Exception e){ System.err.println( "Caught exception " + e . t o S t r i n g ( ) ) ; } } } 80 //Appendix 5: JDBclient.Java i m p o r t j a v a . i o . * ; p u b l i c c l a s s J D B c l i e n t { p u b l i c J D B c l i e n t ( ) { } p u b l i c v o i d W r i t e l L i n e ( ) { t r y { B u f f e r e d W r i t e r bwin = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J C o n n . q n a m e ) ) ; b w i n . w r i t e ( J c l i e n t . s i , 0, J c l i e n t . s i . l e n g t h ( ) ) b w i n . n e w L i n e ( ) ; b w i n . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e ) ; } } p u b l i c v o i d W r i t e 2 L i n e s ( ) { t r y { B u f f e r e d W r i t e r bwin = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J C o n n . q n a m e ) ) ; b w i n . w r i t e ( J c l i e n t . s i , 0, J c l i e n t . s i . l e n g t h ( ) ) b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s 2 , 0, J c l i e n t . s 2 . l e n g t h ( ) ) b w i n . n e w L i n e ( ) ; b w i n . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e ) ; } } p u b l i c v o i d W r i t e 3 L i n e s ( ) { t r y { B u f f e r e d W r i t e r b w i n = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J C o n n . q n a m e ) ) ; b w i n . w r i t e ( J c l i e n t . s i , 0, J c l i e n t . s i . l e n g t h ( ) ) b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s 2 , 0, J c l i e n t . s 2 . l e n g t h ( ) ) b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s 3 , 0, J c l i e n t . s 3 . l e n g t h ( ) ) b w i n . n e w L i n e ( ) ; b w i n . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e ) ; } 1 p u b l i c v o i d W r i t e l O L i n e s ( ) { t r y { B u f f e r e d W r i t e r b w i n = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J C o n n . q n a m e ) ) ; b w i n . w r i t e ( J c l i e n t . s i , 0, J c l i e n t . s i . l e n g t h ( ) ) b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s 2 , 0, J c l i e n t . s 2 . l e n g t h ( ) ) b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s 3 , 0, J c l i e n t . s 3 . l e n g t h ( ) ) 81 b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s b w i n . n e w L i n e ( ) ; b w i n . w r i t e ( J c l i e n t . s l O , 0, J c l i e n t . s l O . l e n g t h ( ) ) b w i n . n e w L i n e ( ) ; b w i n . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e) ; } 0, J c l i e n t s4 l e n g t h ( ) ) 0, J c l i e n t s5 l e n g t h ( ) ) o, J c l i e n t s6 l e n g t h ( ) ) 0, J c l i e n t s7 l e n g t h ( ) ) 0, J c l i e n t s8 l e n g t h ( ) ) 0, J c l i e n t s9 l e n g t h ( ) ) 82 //Appendix 6: show_pop.j ava i m p o r t j ava.awt.*; i m p o r t j a v a . i o . * ; i m p o r t j a v a . n e t . * ; i m p o r t j a v a . u t i l . * ; i m p o r t j a v a . l a n g . * ; . p u b l i c c l a s s show_pop e x t e n d s Frame{ T e x t F i e l d T e x t F i e l d T e x t F i e l d T e x t F i e l d T e x t F i e l d T e x t F i e l d T e x t F i e l d T e x t F i e l d T e x t F i e l d T e x t F i e l d L a b e l 11 L a b e l L a b e l L a b e l L a b e l L a b e l L a b e l L a b e l L a b e l L a b e l p r i v a t e p r i v a t e p r i v a t e p r i v a t e p r i v a t e p r i v a t e p r i v a t e p r i v a t e p r i v a t e 12 : 13 : 14 : 15 : 16 • 17 : 18 : 19 : 110 t l = t 2 = t 3 = t 4 = t 5 = t6 = t 7 = t 8 = t 9 = t l O = new = new = new = new = new = new new T e x t F i e l d ( " new T e x t F i e l d ( " new T e x t F i e l d ( " new T e x t F i e l d ( " new T e x t F i e l d ( " new T e x t F i e l d ( " new T e x t F i e l d ( " new T e x t F i e l d ( " new T e x t F i e l d ( " = new T e x t F i e l d ( L a b e l ( L a b e l ( L a b e l ( L a b e l ( L a b e l ( L a b e l ( : new L a b e l ( : new L a b e l ( : new L a b e l ( = new L a b e l ( P; p0 P a n e l P a n e l P a n e l P a n e l P a n e l P a n e l P a n e l P i P2 p3 p4 p5 2 0 ) 2 0 ) 2 0 ) 2 0 ) 2 0 ) 2 0 ) 2 0 ) 2 0 ) 2 0 ) , 2 0 ) s t a t i c s h o w _ p r i n t p r i n t _ p o p _ u p ; s t a t i c D i a l o g pop_p; p u b l i c s t a t i c B u t t o n b _ p r i n t = new B u t t o n ( " P r i n t " p u b l i c show_pop(){ p = new P a n e l ( ) ; p . s e t L a y o u t ( n e w G r i d L a y o u t ( 1 , 2, 50, 5 0 ) ) ; p . a d d ( J c l i e n t . O K ) ; p . a d d ( J c l i e n t . c ) ; a d d ( " S o u t h " , p ) ; p u b l i c s t a t i c v o i d s h o w _ p r i n t _ d i a l o g ( ) { p r i n t _ p o p _ u p = new s h o w _ p r i n t ( ) ; p r i n t _ p o p _ u p . r e s i z e ( 4 0 0 , 300); p r i n t _ p o p _ u p . s e t V i s i b l e ( t r u e ) ; J c l i e n t . p r i n t R e s u l t ( ) ; pop_p = new D i a l o g ( p r i n t _ p o p _ u p ) ; pop_p.show(); 83 p u b l i c s t a t i c v o i d c l o s e _ p r i n t _ d i a l o g ( ) { p r i n t _ p o p _ u p . R e s e t P a n e l ( ) ; p r i n t _ p o p _ u p . s e t V i s i b l e ( f a l s e ) ; } p u b l i c b o o l e a n a c t i o n ( E v e n t e v t , O b j e c t a r g ) { i f ( e v t . t a r g e t . e q u a l s ( J c l i e n t . O K ) ) { i f ( J c l i e n t . A C K _ A U T H == t r u e ) { R e a d T e x t ( ) ; J c l i e n t . S u b m i t R e q u e s t ( ) ; J c l i e n t . G e t R e s u l t s ( ) ; i f ( J c l i e n t . G O _ A H E A D == t r u e ) { J c l i e n t . R e a d F i l e = f a l s e ; J c l i e n t . R e a d Q u e r y = f a l s e ; s h o w _ p r i n t _ d i a l o g ( ) ; } e l s e { J c l i e n t . a . a p p e n d T e x t ( "CANNOT RETRIEVE RESULT !!!!\n") Jclient.GO_AHEAD = f a l s e ; } J c l i e n t . c l o s e _ d i a l o g ( ) ; r e m o v e ( t h i s ) ; } e l s e { J c l i e n t . a . a p p e n d T e x t ( " L o g i n U n s u c c e s s f u l ! ! ! T r y A g a i n ...\n Jclient.GO_AHEAD = f a l s e ; J c l i e n t . c l o s e _ d i a l o g ( ) ; r e m o v e ( t h i s ) ; } } e l s e i f ( e v t . t a r g e t . e q u a l s ( J c l i e n t . c ) ) { J c l i e n t . c l o s e _ d i a l o g ( ) ; r e m o v e ( t h i s ) ; } e l s e r e t u r n s u p e r . a c t i o n ( e v t , a r g ) ; r e t u r n t r u e ; } p u b l i c b o o l e a n h a n d l e E v e n t ( E v e n t e v t ) { i f ( e v t . i d == Event.WINDOW_DESTROY) S y s t e m . e x i t ( 0 ) ; e l s e r e t u r n s u p e r . h a n d l e E v e n t ( e v t ) ; r e t u r n t r u e ; } p u b l i c v o i d R e s e t T e x t ( ) { t l . s e t T e x t ( " " ) ; t 2 . s e t T e x t ( " "),• t 3 . s e t T e x t ( " " ) ; t 4 . s e t T e x t ( " " ) ; t 5 . s e t T e x t ( " " ) ; t 6 . s e t T e x t ( " " ) ; 84 t 7 . s e t T e x t ( " ") ; t 8 . s e t T e x t ( " " ) ; t 9 . s e t T e x t ( " " ) ; t l O . s e t T e x t ( " " ) ; 11. s e t T e x t ( " " ) ; 12. s e t T e x t ( " " ) ; 13. s e t T e x t (" " ) ; 14. s e t T e x t ( " " ) ; ' 15. s e t T e x t ( " ") ; 16. s e t T e x t ( " ") ; 17. s e t T e x t ( " " ) ; . 18. s e t T e x t ( " " ) ; 19. s e t T e x t ( " " ) ; 1 1 0 . s e t T e x t ( " " ) ; remove(p2); remove(p) ; } p u b l i c v o i d a d d P a n e l ( i n t n){ i f ( n == 0){ p2 = new P a n e l ( ) ; p 2 . s e t L a y o u t ( n e w FlowLayout(FlowLayout.CENTER, 10, 1 0 ) ) ; L a b e l m e s l = new L a b e l ( " P r e s s OK"); p 2 . a d d ( m e s l ) ; a d d ( " C e n t e r " , p 2 ) ; } e l s e { i f (n == 1) addOne(); i f (n == 2) addTwo(); i f (n == 3) addThree() i f (n == 10) a d d T e n ( ) ; } } p u b l i c v o i d addOne(){ p2 = new P a n e l ( ) ; p 2 . s e t L a y o u t ( n e w G r i d L a y o u t ( 1 , 2, 2, 2 ) ) ; p 2 . a d d ( l l ) ; p 2 . a d d ( t l ) ; a d d ( " N o r t h " , p 2 ) ; a d d T e x t ( ) ; p u b l i c v o i d addTwo(){ p2 = new P a n e l ( ) ; p 2 . s e t L a y o u t ( n e w G r i d L a y o u t ( 2 , 2, 2, 2)); p 2 . a d d ( l l ) ; p 2 . a d d ( t l ) ; p 2 . a d d ( 1 2 ) ; p 2 . a d d ( t 2 ) ; a d d ( " N o r t h " , p 2 ) ; a d d T e x t ( ) ; 85 } p u b l i c v o i d a d d T h r e e ( ) { p2 = new P a n e l ( ) ; p 2 . s e t L a y o u t ( n e w G r i d L a y o u t ( 3 , 2, 2, 2 ) ) ; p 2 . a d d ( l l ) ; p 2 . a d d ( t l ) ; p 2 . a d d ( 1 2 ) ; p 2 . a d d ( t 2 ) ; p 2 . a d d ( 1 3 ) ; p 2 . a d d ( t 3 ) ; a d d ( " N o r t h " , p 2 ) ; a d d T e x t ( ) ; } p u b l i c v o i d addTen(){ p2 = new P a n e l ( ) ; p 2 . s e t L a y o u t ( n e w G r i d L a y o u t ( 1 0 , 2, 2, 2 ) ) ; p 2 . a d d ( 1 1 ) ; p 2 . a d d ( t l ) ; p 2 . a d d ( 1 2 ) ; p2 . add (t2) ,-p 2 . a d d ( 1 3 ) ; p 2 . a d d ( t 3 ) ; p 2 . a d d ( 1 4 ) ; p 2 . a d d ( t 4 ) ; p 2 . a d d ( 1 5 ) ; p 2 . a d d ( t 5 ) ; p 2 . a d d ( 1 6 ) ; p 2 . a d d ( t 6 ) ; p 2 . a d d ( 1 7 ) ; p 2 . a d d ( t 7 ) ; p 2 . a d d ( 1 8 ) ; p 2 . a d d ( t 8 ) ; p 2 . a d d ( 1 9 ) ; p 2 . a d d ( t 9 ) ; p 2 . a d d ( 1 1 0 ) ; p 2 . a d d ( t l O ) ; a d d ( " N o r t h " , p 2 ) ; a d d T e x t ( ) ; } p u b l i c v o i d a d d T e x t ( ) { i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q11 || J c l i e n t . Q u e r y N u m == PacketType.SEND_Q12 J c l i e n t . Q u e r y N u m == PacketType.SEND_Q13 J c l i e n t . Q u e r y N u m == PacketType.SEND_Q14 J c l i e n t . Q u e r y N u m == PacketType.SEND_Q15 J c l i e n t . Q u e r y N u m == PacketType.SEND_Q1 | J c l i e n t . Q u e r y N u m == PacketType.SEND_Q19) 1 1 . s e t T e x t ( " E n t e r I D : " ) ; i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q2){ 11. s e t T e x t ( " E n t e r LAST NAME:"); 12. s e t T e x t ( " E n t e r FIRST NAME:"); } i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q14) 86 1 2 . s e t T e x t ( " E n t e r NEW C a t e g o r y : " ) ; i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q12) 1 2 . s e t T e x t ( " E n t e r NEW CLASS:"); i f ( J c l i e n t . R e a d F i l e == t r u e ) 1 1 . s e t T e x t ( " E n t e r F i l e Name:"); i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q16){ 11. s e t T e x t ( " E n t e r USER_ID:"); 12. s e t T e x t ( " E n t e r PASS_WD:"); 13. s e t T e x t ( " E n t e r AUTH_LEVEL:"); } i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q17){ 11. s e t T e x t ( " E n t e r Company name:"); 12. s e t T e x t ( " E n t e r Manager name:"); 13. s e t T e x t ( " E n t e r C o n t a c t a d d r e s s : " ) ; } i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q15){ 11. s e t T e x t ( " E n t e r New I D : " ) ; 12. s e t T e x t ( " E n t e r LAST NAME:"); 13. s e t T e x t ( " E n t e r FIRST NAME:"); 14. s e t T e x t ( " E n t e r C I T Y : " ) ; 15. s e t T e x t ( " E n t e r COMPANY:"); 16. s e t T e x t ( " E n t e r CLASS:"); 17. s e t T e x t ( " E n t e r CATEGORY:"); 18. s e t T e x t ( " E n t e r YRS:"); 19. s e t T e x t ( " E n t e r TEL.NO:"); 1 1 0 . s e t T e x t ( " E n t e r FILE_NO:"); } i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q20 || J c l i e n t . Q u e r y N u m == PacketType.SEND_Q7) 1 1 . s e t T e x t ( " E n t e r USER_NAME:"); i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q21) 1 1 . s e t T e x t ( " E n t e r Company Name: ") ; i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q3) 1 1 . s e t T e x t ( " E n t e r CATEGORY:"); i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q4) 1 1 . s e t T e x t ( " E n t e r C I T Y : " ) ; i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q5) 1 1 . s e t T e x t ( " E n t e r Company:"); i f ( J c l i e n t . Q u e r y N u m == PacketType.SEND_Q6){ 11. s e t T e x t ( " E n t e r CATEGORY:"); 12. s e t T e x t ( " E n t e r YRS: " ) ; } } p u b l i c v o i d R e a d T e x t ( ) { J c l i e n t s i = new S t r i n g ( t l . g e t T e x t ( ) ) . t r i m ( ) J c l i e n t s2 = new S t r i n g ( t 2 . g e t T e x t ( ) ) . t r i m ( ) J c l i e n t s3 = new S t r i n g ( t 3 g e t T e x t ( ) ) . t r i m ( ) J c l i e n t s4 = new S t r i n g ( t 4 g e t T e x t ( ) ) . t r i m ( ) J c l i e n t s5 = new S t r i n g ( t 5 g e t T e x t ( ) ) . t r i m ( ) J c l i e n t s6 = new S t r i n g ( t 6 g e t T e x t ( ) ) . t r i m ( ) J c l i e n t s7 = new S t r i n g ( t 7 g e t T e x t ( ) ) . t r i m ( ) J c l i e n t s8 = new S t r i n g ( t 8 g e t T e x t ( ) ) . t r i m ( ) J c l i e n t s9 = new S t r i n g ( t 9 g e t T e x t ( ) ; . t r i m ( ) J c l i e n t s l O = new S t r i n g ( t l O . g e t T e x t ) ) . t r i m } 87 //Appendix 7: show_print. J a v a i m p o r t j ava.awt.*; i m p o r t j a v a . i o . * ; i m p o r t j a v a . n e t . * ; i m p o r t j a v a . u t i l . * ; i m p o r t j a v a . l a n g . * ; p u b l i c c l a s s s h o w _ p r i n t e x t e n d s Frame{ p r i v a t e P a n e l p; p r i v a t e P a n e l p i ; p r i v a t e P a n e l p2; p u b l i c s t a t i c T e x t A r e a p_a = new T e x t A r e a ( " ", 40, 1) p u b l i c c l o s e _ p a n e l p _ c l o s e ; p u b l i c c l a s s c l o s e _ b u t t o n e x t e n d s B u t t o n { p u b l i c c l o s e _ b u t t o n ( ) { s e t S i z e ( 5 0 , 2 0 ) ; s e t L a b e l ( " C l o s e " ) ; } } p u b l i c c l a s s c l o s e _ p a n e l e x t e n d s P a n e l { p u b l i c c l o s e _ b u t t o n b _ c l o s e ; p u b l i c c l o s e _ p a n e l ( ) { b _ c l o s e = new c l o s e _ b u t t o n ( ) ; s e t L a y o u t ( n e w B o r d e r L a y o u t ( ) ) ; a d d ( " C e n t e r " , b _ c l o s e ) ; } } p u b l i c s h o w _ p r i n t ( ) { p _ c l o s e = new c l o s e _ p a n e l ( ) ; a d d ( " S o u t h " , p _ c l o s e ) ; p _ a . s e t E d i t a b l e ( f a l s e ) ; a d d ( " C e n t e r " , p _ a ) ; } p u b l i c b o o l e a n a c t i o n ( E v e n t e v t , O b j e c t a r g ) { i f ( e v t . t a r g e t . e q u a l s ( p _ c l o s e . b _ c l o s e ) ) { s h o w _ p o p . c l o s e _ p r i n t _ d i a l o g ( ) ; r e m o v e ( t h i s ) ; } e l s e r e t u r n s u p e r . a c t i o n ( e v t , a r g ) ; r e t u r n t r u e ; } p u b l i c b o o l e a n h a n d l e E v e n t ( E v e n t e v t ) { i f ( e v t . i d == Event.WINDOW_DESTROY) S y s t e m . e x i t ( 0 ) ; e l s e r e t u r n s u p e r . h a n d l e E v e n t ( e v t ) ; r e t u r n t r u e ; } p u b l i c v o i d R e s e t P a n e l ( ) { p _ a . s e t T e x t ( " " ) ; 88 r e m o v e ( p _ c l o s e ) / /Appendix 8: m a i n s e r v e r . j ava i m p o r t j a v a . n e t . * ; i m p o r t j a v a . i o . * ; i m p o r t j a v a . u t i l . * ; i m p o r t j ava.awt.*; i m p o r t J a v a . l a n g . T h r e a d ; p u b l i c c l a s s m a i n s e r v e r e x t e n d s Thread{ p r i v a t e DatagramPacket d g l n P k t ; p r i v a t e DatagramSocket main_soc; p r i v a t e b y t e [ ] b u f = new b y t e [ 1 0 0 ] ; p r i v a t e i n t [ ] p o r t _ i n _ u s e = new i n t [ 1 0 0 ] ; p r i v a t e i n t C l i e n t P o r t ; p r i v a t e I n e t A d d r e s s C l i e n t A d d r e s s ; p r i v a t e i n t num_ports; p r i v a t e i n t c l i e n t _ b i n d S e r v e r , c l i e n t _ b i n d K e y , s e r v e r _ b i n d C l i e n t , s e r v e r _ b i n d K e y , k e y _ b i n d C l i e n t , k e y _ b i n d S e r v e r ; p u b l i c i n t S e a r c h ( i n t n ) { t r y { f o r ('int j =0; j<100;j++){ i f ( p o r t _ i n _ u s e [ j ] == n) r e t u r n j ; } r e t u r n -1; } c a t c h ( E x c e p t i o n e l ) { S y s t e m . e r r . p r i n t l n ( " E r r o r i n s e a r c h : " + e l ) r e t u r n -1; } } p u b l i c i n t F i n d F r e e P o r t ( ) { t r y { b o o l e a n p o r t _ f o u n d = t r u e ; i n t n e x t _ p o r t = 0; w h i l e ( p o r t _ f o u n d == t r u e ) { n e x t _ p o r t = 1025 + num_ports; i f ( ( S e a r c h ( n e x t _ p o r t ) == -1) && n e x t _ p o r t != C l i e n t P o r t ) { num_ports++; r e t u r n n e x t _ p o r t ; } e l s e num_ports++; } r e t u r n -1; } c a t c h ( E x c e p t i o n e2){ S y s t e m . e r r . p r i n t l n ( " E r r o r i n s e a r c h : " + e2) r e t u r n -1; } } p u b l i c v o i d G e t P o r t s ( ) { t r y { . c l i e n t _ b i n d S e r v e r = F i n d F r e e P o r t ( ) ; 90 c l i e n t _ b i n d K e y = F i n d F r e e P o r t ( ) ; s e r v e r _ b i n d C l i e n t = F i n d F r e e P o r t ( ) ; s e r v e r _ b i n d K e y = F i n d F r e e P o r t ( ) ; k e y _ b i n d C l i e n t = F i n d F r e e P o r t ( ) ; k e y _ b i n d S e r v e r = F i n d F r e e P o r t ( ) ; } c a t c h ( E x c e p t i o n e3){ S y s t e m . e r r . p r i n t l n ( " E r r o r i n G e t P o r t s : " + e 3 ) ; S y s t e m . e x i t ( 1 ) ; } } p u b l i c m a i n s e r v e r ( ) { i n t i =0; num_ports = 0; t r y { w h i l e ( t r u e ) { S y s t e m . o u t . p r i n t l n ( " J a v a M S e r v e r S t a r t e d " ) ; main_soc = new D a t a g r a m S o c k e t ( 2 0 0 0 ) ; d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; m a i n _ s o c . r e c e i v e ( d g l n P k t ) ; i n t c i d = 10 + i ; C l i e n t P o r t = d g l n P k t . g e t P o r t ( ) ; C l i e n t A d d r e s s = d g l n P k t . g e t A d d r e s s ( ) ; b o o l e a n f l a g = t r u e ; w h i l e ( f l a g == t r u e ) { G e t P o r t s ( ) ; i f ( c l i e n t _ b i n d S e r v e r != -1 && c l i e n t _ b i n d K e y != -1 && s e r v e r _ b i n d C l i e n t != -1 && s e r v e r _ b i n d K e y != -1 && k e y _ b i n d C l i e n t != -1 && k e y _ b i n d S e r v e r != -1){ new J s e r v e r ( c l i e n t _ b i n d S e r v e r , c l i e n t _ b i n d K e y , s e r v e r _ b i n d C l i e n t , s e r v e r _ b i n d K e y , k e y _ b i n d C l i e n t , k e y _ b i n d S e r v e r , C l i e n t P o r t , C l i e n t A d d r e s s , c i d ) . s t a r t ( ) ; f l a g = f a l s e ; } } i++; S y s t e m . o u t . p r i n t l n ( "JavaM S e r v e r r e a d y f o r n e x t r e q u e s t . . . " ) ; m a i n _ s o c . c l o s e ( ) ; } } c a t c h ( E x c e p t i o n e ){ e . p r i n t S t a c k T r a c e ( ) ; . S y s t e m . e x i t ( 1 ) ; } } p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { new m a i n s e r v e r ( ) ; } 91 //Appendix 9: J s e r v e r . J a v a i m p o r t j a v a . n e t . * ; i m p o r t j a v a . i o . * ; i m p o r t j a v a . u t i l . * ; i m p o r t j ava.awt.*; i m p o r t j a v a . l a n g . T h r e a d ; p u b l i c c l a s s J s e r v e r e x t e n d s Thread{ p r i v a t e DatagramPacket d g l n P k t ; p r i v a t e DatagramPacket dgOutPkt; p r i v a t e DatagramSocket C l i e n t S o c k e t ; p r i v a t e DatagramSocket K e y S o c k e t ; p r i v a t e i n t C l i e n t P o r t ; p r i v a t e i n t K e y P o r t ; p r i v a t e i n t C l i e n t K e y P o r t ; p r i v a t e I n e t A d d r e s s C l i e n t A d d r e s s ; p r i v a t e I n e t A d d r e s s KeyAddress; p r i v a t e i n t t r a n s f e r C o m p l e t e ; p r i v a t e i n t OutCode; p r i v a t e i n t opcode; p r i v a t e i n t d a t a L e n ; p r i v a t e i n t C l i e n t _ I D ; p r i v a t e D a t a l n p u t S t r e a m d a t a l n ; p r i v a t e S t r i n g F i l e T o E n c o d e ; p r i v a t e J E n c o d e r enc; p r i v a t e D B s e r v e r DBS; p r i v a t e l o n g seed = 1999; p r i v a t e b y t e [ ] b u f = new b y t e [ 1 0 0 0 ] ; p r i v a t e i n t toRead; p r i v a t e F i l e l n p u t S t r e a m f i s ; p r i v a t e i n t O f f s e t ; p r i v a t e b o o l e a n ACK_RECIEVED=true; p r i v a t e S t r i n g c u r r e n t _ u s e r n a m e ; p r i v a t e i n t c u r r e n t _ u s e r s t a t u s ; p r i v a t e S t r i n g c u r r e n t _ u s e r L e v e l ; p r i v a t e S t r i n g c u r r e n t _ u s e r P W ; p r i v a t e b o o l e a n SES_KEY = f a l s e ; p r i v a t e b o o l e a n PUB_KEY = f a l s e ; p r i v a t e b o o l e a n USE_AUTH = f a l s e ; p r i v a t e b o o l e a n CONNECTION_READY = f a l s e p r i v a t e i n t c l i e n t _ b i n d S e r v e r ; p r i v a t e i n t c l i e n t _ b i n d K e y ; p r i v a t e i n t s e r v e r _ b i n d C l i e n t ; p r i v a t e i n t s e r v e r _ b i n d K e y ; p r i v a t e i n t k e y _ b i n d C l i e n t ; p r i v a t e i n t k e y _ b i n d S e r v e r ; p r i v a t e S t r i n g M D f i l e ; p u b l i c s t a t i c S t r i n g q f i l e ; p r i v a t e S t r i n g S i g n F i l e ; p r i v a t e S t r i n g p T e x t ; p r i v a t e S t r i n g s T e x t ; p r i v a t e S t r i n g c T e x t ; p r i v a t e S t r i n g c f i l e ; p r i v a t e S t r i n g pkname; p r i v a t e S t r i n g serv_pkname; p r i v a t e S t r i n g skname; 92 p r i v a t e S t r i n g signame; p u b l i c v o i d m a k e A C K P a c k e t ( i n t ocode, I n e t A d d r e s s d e s t , i n t d e s t P o r t ) { i f ( o c o d e != PacketType.ACK_PW && ocode != PacketType.SEND_SERVER_PORT && ocode != PacketType.SEND_CLIENT_PORT) t r a n s f e r C o m p l e t e = 1; t r y { B y t e A r r a y O u t p u t S t r e a m d a t a b u f l = new B y t e A r r a y O u t p u t S t r e a m ( ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f l . w r i t e ( o c o d e ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f l . w r i t e ( t r a n s f e r C o m p l e t e ) ; i f ( o c o d e == PacketType.WRITE_TICKET) d a t a b u f 1 . w r i t e ( e n c . h a s h , 0, enc.hashLen) b y t e t ] b u f l = d a t a b u f 1 . t o B y t e A r r a y ( ) ; dgOutPkt = new D a t a g r a m P a c k e t ( b u f 1 , b u f l . l e n g t h , d e s t , d e s t P o r t ) ; } c a t c h ( E x c e p t i o n e){ USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e ) ; } } p u b l i c v o i d m a k e O u t P a c k e t ( b y t e [ ] b, i n t o_code, i n t f l a g , I n e t A d d r e s s d e s t , i n t d e s t P o r t ) { t r y { B y t e A r r a y O u t p u t S t r e a m d a t a b u f = new B y t e A r r a y O u t p u t S t r e a m ( ) ; d a t a b u f . w r i t e ( 0 ) ; d a t a b u f . w r i t e ( o _ c o d e ) ; d a t a b u f . w r i t e ( 0 ) ; d a t a b u f . w r i t e ( f l a g ) ; d a t a b u f . w r i t e ( b ) ; b y t e [ ] buf 2 = d a t a b u f . t o B y t e A r r a y ( ) ; dgOutPkt = new D a t a g r a m P a c k e t ( b u f 2 , b u f 2 . l e n g t h , d e s t , d e s t P o r t ) ; } c a t c h ( E x c e p t i o n e ) { USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e ) ; } } p u b l i c v o i d R e t r i e v e C l i e n t P a c k e t ( ) { t r y { d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; C l i e n t S o c k e t . r e c e i v e ( d g l n P k t ) ; C l i e n t P o r t = d g l n P k t . g e t P o r t ( ) ; C l i e n t A d d r e s s = d g l n P k t . g e t A d d r e s s ( ) ; d a t a l n = new D a t a l n p u t S t r e a m ( new B y t e A r r a y l n p u t S t r e a m ( d g l n P k t . g e t D a t a ( ) ) ) ; d a t a L e n = d g l n P k t . g e t L e n g t h ( ) ; opcode = d a t a l n . r e a d S h o r t ( ) ; t r a n s f e r C o m p l e t e = d a t a l n . r e a d S h o r t ( ) ; } c a t c h ( E x c e p t i o n e ) { 93 USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r From G e t C l i e n t R e q u e s t : " + e ) ; p u b l i c v o i d R e t r i e v e K e y S e r v e r P a c k e t ( ) { t r y { d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; K e y S o c k e t . r e c e i v e ( d g l n P k t ) ; K e y P o r t = d g l n P k t . g e t P o r t ( ) ; KeyAddress = d g l n P k t . g e t A d d r e s s ( ) ; d a t a l n = new D a t a I n p u t S t r e a m ( new B y t e A r r a y l n p u t S t r e a m ( d g l n P k t . g e t D a t a ( ) ) ) ; d a t a L e n = d g l n P k t . g e t L e n g t h ( ) ; opcode = d a t a l n . r e a d S h o r t ( ) ; t r a n s f e r C o m p l e t e = d a t a l n . r e a d S h o r t ( ) ; } c a t c h ( E x c e p t i o n e ){ USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r from G e t C l i e n t F i l e R e q u e s t : " + e ) ; } } p u b l i c v o i d G e t A u t h R e q u e s t ( ) { t r y { i n t code = PacketType.ACK_PW; b y t e [ ] b u f l = new byte[1000],-R e t r i e v e d i e n t P a c k e t () ; i f ( o p c o d e == PacketType.VERIFY_PW && ACK_RECIEVED == t r u e ) { S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s L o g i n Request To J a v a S e r v e r " ) D B S . e x e c u t e A u t h ( d a t a l n ) ; c u r r e n t _ u s e r n a m e = DBS.UserName; c u r r e n t _ u s e r L e v e l = D B S . U s e r L e v e l ; c u r r e n t _ u s e r P W = DBS.UserPW; i f ( e n c . V e r i f y M e s s a g e D i g e s t ( c u r r e n t _ u s e r P W , d a t a l n ) == t r u e ) { USE_AUTH = t r u e ; i f ( c u r r e n t _ u s e r L e v e l . c o m p a r e T o ( "user") == 0) t r a n s f e r C o m p l e t e = PacketType.VALID_USER; e l s e i f ( c u r r e n t _ u s e r L e v e l . c o m p a r e T o ( "admin") == 0) t r a n s f e r C o m p l e t e = PacketType.VALID_ADMIN; e l s e t r a n s f e r C o m p l e t e = 0; } } e l s e code = PacketType.ERROR; makeACKPacket(code, C l i e n t A d d r e s s , C l i e n t P o r t ) ; C l i e n t S o c k e t . s e n d ( d g O u t P k t ) ; } c a t c h ( E x c e p t i o n e){ 94 USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r From GetAuthRequest: " + e) p u b l i c v o i d G e t C l i e n t R e q u e s t ( ) { t r y { i n t code = PacketType.ACK; R e t r i e v e C l i e n t P a c k e t ( ) ; i n t r e s = 0; i f ( o p c o d e == PacketType.SEND_FILE && ACK_RECIEVED == t r u e ) { b y t e [ ] b u f l = new b y t e [ 1 0 0 0 ] ; S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s F i l e Request To J a v a S e r v e r " ) ; d a t a l n . r e a d F u l l y ( b u f l , PacketType.EMPTY_PKT, d a t a L e n - PacketType.EMPTY_PKT); F i l e T o E n c o d e = new S t r i n g ( b u f l , PacketType.EMPTY_PKT, d a t a L e n - P a c k e t T y p e . E M P T Y _ P K T ) . t r i m ( ) ; t r a n s f e r C o m p l e t e = 1; S y s t e m . o u t . p r i n t l n ( "FILE TO ENCODE: " + F i l e T o E n c o d e ) ; } e l s e i f ( o p c o d e == PacketType.GOOD_BYE && ACK_RECIEVED == t r u e ) { code = PacketType.ACK; USE_AUTH = f a l s e ; } e l s e i f ( o p c o d e == PacketType.REQ_PUBKEY){ i f ( G e t C l i e n t P u b K e y R e q u e s t ( ) == t r u e ) { i f ( G e t K e y S e r v e r P K R e s p o n s e ( ) == t r u e ) { code = PacketType.ACK_PUBKEY; t r a n s f e r C o m p l e t e = C l i e n t K e y P o r t ; } e l s e . code = PacketType.ERROR; } e l s e code = PacketType.ERROR; } e l s e i f ( o p c o d e == PacketType.REQ_SESKEY){ G e t C l i e n t S e s K e y R e q u e s t ( ) ; i f ( G e t K e y S e r v e r S K R e s p o n s e ( ) == t r u e ) code = PacketType.ACK_SESKEY; e l s e code = PacketType.ERROR; } e l s e i f ( ( o p c o d e == PacketType.SEND_ENCODED_FILE || opcode == PacketType.SEND_ENCODED_QUERY) && ACK_RECIEVED == t r u e && SES_KEY == t r u e && PUB_KEY == t r u e ) { • G e t C l i e n t E n c o d e R e q u e s t ( ) ; } e l s e i f ( o p c o d e == PacketType.READ_PUBKEY){ i f ( R e a d K e y ( ) == t r u e ) 95 code = PacketType.ACK; • e l s e code = PacketType.ERROR; } e l s e i f ( o p c o d e == PacketType.ACK_SIGN){ S y s t e m . o u t . p r i n t l n ( " W r i t i n g D i g i t a l S i g n a t u r e " ) ; b y t e [ ] q l b u f = new byte [ 1 0 0 0 ] ; d a t a l n . r e a d F u l l y ( q l b u f , 0, d a t a L e n - PacketType.EMPTY_PKT); F i l e O u t p u t S t r e a m f q o u t = new F i l e O u t p u t S t r e a m ( s i g n a m e ) ; f q o u t . w r i t e ( q l b u f , 0, d a t a L e n - PacketType.EMPTY_PKT); f q o u t . c l o s e ( ) ; code = PacketType .ACK;, } e l s e i f ( o p c o d e == PacketType.SEND_SIGN && ACK_RECIEVED == t r u e && SES_KEY == t r u e && PUB_KEY == t r u e ) { G e t C l i e n t S i g n R e q u e s t ( ) ; } e l s e i f ( i s Q u e r y ( o p c o d e ) == t r u e ) { S y s t e m . o u t . p r i n t l n ( " R e t r i e v i n g Q u e r y . . " ) ; b y t e t ] q l b u f = new by t e [ 1 0 0 0 ] ; d a t a l n . r e a d F u l l y ( q l b u f , 0, d a t a L e n - PacketType.EMPTY_PKT); F i l e O u t p u t S t r e a m f q o u t = new F i l e O u t p u t S t r e a m ( c T e x t ) ; f q o u t . w r i t e ( q l b u f , 0, d a t a L e n - PacketType.EMPTY_PKT); f q o u t . c l o s e ( ) ; e n c . D e c o d e F i l e ( c T e x t , p T e x t ) ; F i l e l n p u t S t r e a m f q i n = new F i l e l n p u t S t r e a m ( p T e x t ) ; b y t e [ ] q2buf = new b y t e [ f q i n . a v a i l a b l e ( ) ] ; f q i n . r e a d ( q 2 b u f ) ; f q i n . c l o s e ( ) ; b o o l e a n GO_AHEAD = f a l s e ; i f ( o p c o d e == PacketType.SEND_Q12 || opcode == PacketType SEND_ -Q14 | opcode == PacketType SEND_ -Q15 j opcode == PacketType SEND. -Q16 j opcode == Packet T y p e SEND. -Q17 j opcode == PacketType SEND. -Q19 | opcode == PacketType SEND. _Q20 | opcode == PacketType SEND. .Q21) { System o u t . p r i n t l n ( " V e r i f y i n g D i g i t a l S i g n a t u r e " ) ; e n c . D e c o d e S i g n a t u r e ( s i g n a m e ) ; i f ( e n c . V e r i f y S i g n a t u r e ( p T e x t ) == t r u e ) GO_AHEAD = t r u e ; e l s e GO_AHEAD = f a l s e ; } e l s e GO_AHEAD = t r u e ; 96 if(GO_AHEAD == t r u e ) { D a t a l n p u t S t r e a m n e w l n = new D a t a l n p u t S t r e a m ( new B y t e A r r a y I n p u t S t r e a m ( q 2 b u f ) ) ; i f ( D B S . E x e c u t e Q u e r y ( n e w l n , opcode, ACK_RECIEVED) == t r u e ) { code = PacketType.ACK_QUERY; S y s t e m . o u t . p r i n t l n ( " S u c c e s s f u l l y E x e c u t e d Query ...") } e l s e code = PacketType.ERROR; } e l s e code = PacketType.ERROR; } e l s e { code = PacketType.ERROR; } i f ( o p c o d e != PacketType.SEND_ENCODED_FILE && opcode != PacketType.SEND_ENCODED_QUERY && opcode != PacketType.SEND_SIGN){ S y s t e m . o u t . p r i n t l n ( "Sending ACK from G e t C l i e n t r e q u e s t " ) ; makeACKPacket(code, C l i e n t A d d r e s s , C l i e n t P o r t ) C l i e n t S o c k e t . s e n d ( d g O u t P k t ) ; } } c a t c h ( E x c e p t i o n e ){ USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r From G e t C l i e n t R e q u e s t : " + e ) ; } } p u b l i c b o o l e a n ReadKey(){ t r y { d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; OutCode = PacketType.READ_PUBKEY; t r a n s f e r C o m p l e t e = 1; makeACKPacket(OutCode, KeyAddress, K e y P o r t ) ; K e y S o c k e t . s e n d ( d g O u t P k t ) ; ACK_RECIEVED = f a l s e ; R e t r i e v e K e y S e r v e r P a c k e t ( ) ; i f ( o p c o d e == PacketType.ACK){ ACK_RECIEVED=true; S y s t e m . o u t . p r i n t l n ( " P u b l i c Key R e c e i v e d . \ n " ) ; b y t e [ ] pbuf = new b y t e [ d a t a L e n - PacketType.EMPTY_PKT] d a t a l n . r e a d F u l l y ( p b u f , 0 , d a t a L e n - PacketType.EMPTY_PKT); F i l e O u t p u t S t r e a m pout = new F i l e O u t p u t S t r e a m ( s e r v _ p k n a m e ) ; p o u t . w r i t e ( p b u f , 0 , d a t a L e n - PacketType.EMPTY_PKT); p o u t . c l o s e ( ) ; e n c . D e c o d e P u b l i c K e y ( s e r v _ p k n a m e ) ; r e t u r n t r u e ; 97 } e l s e { S y s t e m . o u t . p r i n t I n ( " S o r r y ! c a n n o t READ KEY\n" r e t u r n f a l s e ; } } c a t c h ( E x c e p t i o n e l ) { S y s t e m . o u t . p r i n t l n ( " E r r o r : " + e l + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n S e n d T i c k e t ( ) { t r y { S y s t e m . o u t . p r i n t l n ( " S e n d i n g T i c k e t " ) ; S t r i n g T i c k e t D a t a ; i f ( C l i e n t _ I D >= 10 && C l i e n t _ I D < 20){ T i c k e t D a t a = new S t r i n g ( c u r r e n t _ u s e r n a m e + c u r r e n t _ u s e r P W + c u r r e n t _ u s e r n a m e ) ; ) e l s e i f ( C l i e n t _ I D >= 20 && C l i e n t _ I D < 30){ T i c k e t D a t a = new S t r i n g ( c u r r e n t _ u s e r P W + c u r r e n t _ u s e r n a m e + c u r r e n t _ u s e r P W ) ; } e l s e { T i c k e t D a t a = new S t r i n g ( c u r r e n t _ u s e r n a m e + c u r r e n t _ u s e r n a m e + c u r r e n t _ u s e r P W ) ; } e n c . M a k e M e s s a g e D i g e s t ( T i c k e t D a t a ) ; makeACKPacket(PacketType.WRITE_TICKET, KeyAddress, K e y P o r t ) ; K e y S o c k e t . s e n d ( d g O u t P k t ) ; R e t r i e v e K e y S e r v e r P a c k e t ( ) ; i f ( o p c o d e == PacketType.ACK_TICKET) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e l ) { S y s t e m . o u t . p r i n t l n ( " E r r o r : " + e l + " \ n " ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n G e t C l i e n t P u b K e y R e q u e s t ( ) { t r y { S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s P u b l i c Key R e q u e s t " ) ; i f ( S e n d T i c k e t ( ) == t r u e ) { e n c . G e n e r a t e K e y P a i r ( s e e d ) ; e n c . E n c o d e P u b l i c K e y ( p k n a m e ) ; t r a n s f e r C o m p l e t e = 1; OutCode = PacketType.WRITE_PUBKEY; F i l e l n p u t S t r e a m k e y f i s = new F i l e l n p u t S t r e a m ( p k n a m e ) ; b y t e [ ] encKey = new b y t e [ k e y f i s . a v a i l a b l e ( ) ] ; k e y f i s . r e a d ( e n c K e y ) ; k e y f i s . c l o s e ( ) ; m a k e O u t P a c k e t ( e n c K e y , O u t C o d e , t r a n s f e r C o m p l e t e KeyAddress, K e y P o r t ) ; ACK_REC1EVED = f a l s e ; 98 K e y S o c k e t . s e n d ( d g O u t P k t ) ; r e t u r n t r u e ; } r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ) { USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r From GetPubKeyRequest: " + e ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n G e t K e y S e r v e r P K R e s p o n s e ( ) { t r y { R e t r i e v e K e y S e r v e r P a c k e t ( ) ; S y s t e m . o u t . p r i n t l n ( "Key S e r v e r ' s P u b l i c Key Response"),-i f ( o p c o d e == PacketType.ACK_PUBKEY){ ACK_RECIEVED = t r u e ; PUB_KEY = t r u e ; r e t u r n t r u e ; } r e t u r n f a l s e ; ) c a t c h ( E x c e p t i o n e ) { USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r From GetKeyServerPKResponse: " + e ) ; r e t u r n f a l s e ; } } p u b l i c v o i d G e t C l i e n t S e s K e y R e q u e s t ( ) { t r y { S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s S e s s i o n Key Request") e n c . G e n e r a t e S e s s i o n K e y ( ) ; e n c .EncodeSessionKey(skname); t r a n s f e r C o m p l e t e = 1; OutCode = PacketType.WRITE_SESKEY; F i l e l n p u t S t r e a m s k e y f i s = new F i l e l n p u t S t r e a m ( s k n a m e ) ; b y t e [ ] encSKey = new b y t e [ s k e y f i s . a v a i l a b l e ( ) ] ; s k e y f i s . r e a d ( e n c S K e y ) ; s k e y f i s . c l o s e ( ) ; makeOutPacket(encSKey, OutCode, t r a n s f e r C o m p l e t e , K eyAddress, K e y P o r t ) ; K e y S o c k e t . s e n d ( d g O u t P k t ) ; } c a t c h ( E x c e p t i o n e){ USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r From G e t C l i e n t S e s K e y R e q u e s t : " + e ) ; } } p u b l i c b o o l e a n G e t K e y S e r v e r S K R e s p o n s e ( ) { t r y { R e t r i e v e K e y S e r v e r P a c k e t ( ) ; S y s t e m . o u t . p r i n t l n ( 99 "Key S e r v e r ' s S e s s i o n Key Re s p o n s e " ) ; i f ( o p c o d e == PacketType.ACK_SESKEY){ ACK_RECIEVED = t r u e ; SES_KEY = t r u e ; r e t u r n t r u e ; } r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ) { USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r From GetKeyServerSKResponse: " + e) ; r e t u r n f a l s e ; } } p u b l i c v o i d G e t C l i e n t E n c o d e R e q u e s t ( ) { t r y { S y s t e m . o u t . p r i n t l n ( " C l i e n t r e q u e s t f o r E n c r y p t e d D a t a " ) ; i f ( o p c o d e == PacketType.SEND_ENCODED_FILE){ e n c . E n c o d e F i l e ( F i l e T o E n c o d e , c f i l e ) ; S i g n F i l e = F i l e T o E n c o d e ; } e l s e { e n c . E n c o d e F i l e ( q f i l e , c f i l e ) ; S i g n F i l e = q f i l e ; } f i s = new F i l e l n p u t S t r e a m ( c f i l e ) ; b y t e U b u f f e r = n u l l ; w h i l e ( f i s . a v a i l a b l e ( ) != 0){ i n t l e n = 100; i f ( f i s . a v a i l a b l e ( ) < l e n ) { l e n = f i s . a v a i l a b l e ( ) ; } b u f f e r = new b y t e [ l e n ] ; i n t 1 = f i s . r e a d ( b u f f e r , 0, l e n ) ; i f ( o p c o d e == PacketType.SEND_ENCODED_FILE) OutCode=PacketType.SEND_ENCODED_FILE; e l s e OutCode=PacketType.SEND_ENCODED_QUERY; t r a n s f e r C o m p l e t e = 0; m a k e O u t P a c k e t ( b u f f e r , O u t C o d e , t r a n s f e r C o m p l e t e , C l i e n t A d d r e s s , C l i e n t P o r t ) ; C l i e n t S o c k e t . s e n d ( d g O u t P k t ) ; } f i s . c l o s e ( ) ; OutCode=PacketType.ACK; t r a n s f e r C o m p l e t e = 1; m a k e O u t P a c k e t ( b u f f e r , OutCode, t r a n s f e r C o m p l e t e , C l i e n t A d d r e s s , C l i e n t P o r t ) ; C l i e n t S o c k e t . s e n d ( d g O u t P k t ) ; } c a t c h ( E x c e p t i o n e ) { USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r From G e t C l i e n t E n c o d e R e q u e s t : " + e ) ; } } 100 p u b l i c v o i d G e t C l i e n t S i g n R e q u e s t ( ) { t r y { S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s S i g n a t u r e R e q u e s t " ) ; e n c . G e n e r a t e S i g n a t u r e ( S i g n F i l e , s i g n a m e ) ; F i l e l n p u t S t r e a m s f i s = new F i l e l n p u t S t r e a m ( s i g n a m e ) b y t e [ ] s = new b y t e [ s f i s . a v a i l a b l e ( ) ] ; s f i s . r e a d ( s ) ; s f i s . c l o s e ( ) ; t r a n s f e r C o m p l e t e = 1 ; OutCode = PacketType.ACK_SIGN; makeOutPacket(s, OutCode, t r a n s f e r C o m p l e t e , C l i e n t A d d r e s s , C l i e n t P o r t ) ; C l i e n t S o c k e t . s e n d ( d g O u t P k t ) ; } c a t c h ( E x c e p t i o n e ) { USE_AUTH = f a l s e ; S y s t e m . e r r . p r i n t l n ( " E r r o r From G e t C l i e n t S i g n R e q u e s t : " + e) ; } } p u b l i c b o o l e a n i s Q u e r y ( i n t code){ t r y { i f ( c o d e == PacketType.SEND_Q1 || code == PacketType SEND. -Q2 1 1 code == PacketType SEND. -Q3 1 1 code == PacketType SEND. -Q4 1 1 code == PacketType SEND. -Q5 1 1 code == PacketType SEND. -Q6 1 1 code == PacketType SEND. _Q7 j | code == PacketType SEND. _Q11 | code == PacketType SEND. -Q12 j code == PacketType SEND. _Q13 | code == PacketType SEND. -Q14 | code == PacketType SEND. -Q15 j code == PacketType SEND. -Q16 j code == PacketType SEND. _Q17 j code == PacketType SEND. -Q8 1 1 code == PacketType SEND. -Q9 1 1 code == PacketType SEND. -Q10 j code == PacketType SEND. _Q18 | code == PacketType SEND. .Q19 | code == PacketType SEND. .Q20 j code == PacketType SEND. -Q21) r e t u r n t r u e ; e l s e r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From i s Q u e r y : " + e ) ; r e t u r n f a l s e ; } } p u b l i c v o i d r u n ( ) { t r y { if(CONNECTION_READY == t r u e ) { w h i l e ( t r u e ) { 101 DBS = new D B s e r v e r ( ) ; enc = new J E n c o d e r ( ) ; G e t A u t h R e q u e s t ( ) ; while(USE_AUTH == t r u e ) { G e t C l i e n t R e q u e s t ( ) ; } S y s t e m . o u t . p r i n t l n ( "Java S e r v e r S t a r t e d " ) ; } } e l s e S y s t e m . o u t . p r i n t l n ( "CONNECTION NOT READY FOR CLIENT_ID = " + C l i e n t _ I D ) ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From r u n : " + e) ; S y s t e m . e x i t ( 1 ) ; } } p u b l i c v o i d C o nnectToKey(){ t r y { KeyAddress = InetAddress.getByName("142.103.10.57 K e y P o r t = 2005; i n t kbs = k e y _ b i n d S e r v e r - 1025; i n t kbc = k e y _ b i n d C l i e n t - 1025; i n t sbk = s e r v e r _ b i n d K e y - 1025; i n t cbk = c l i e n t _ b i n d K e y - 1025; B y t e A r r a y O u t p u t S t r e a m d a t a b u f l = new B y t e A r r a y O u t p u t S t r e a m ( ) ; d a t a b u f 1 . w r i t e ( 0 ) ; d a t a b u f l . w r i t e ( C l i e n t _ I D ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f l . w r i t e ( k b s ) ; d a t a b u f 1 . w r i t e ( 0 ) ; d a t a b u f l . w r i t e ( k b c ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f 1 . w r i t e ( s b k ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f l . w r i t e ( c b k ) ; b y t e [ ] b u f l = d a t a b u f l . t o B y t e A r r a y ( ) ; dgOutPkt = new D a t a g r a m P a c k e t ( b u f 1 , b u f l . l e n g t h , KeyAddress, K e y P o r t ) ; K e y S o c k e t . s e n d ( d g O u t P k t ) ; R e t r i e v e K e y S e r v e r P a c k e t ( ) ; i f ( o p c o d e == PacketType.READY){ System.out.println("CONNECTION READY MM!" CONNECTION_READY = t r u e ; makeACKPacket(PacketType.READY, KeyAddress, K e y P o r t ) ; K e y S o c k e t . s e n d ( d g O u t P k t ) ; } e l s e S y s t e m . o u t . p r i n t l n ( "CANNOT OPEN KEY CONNECTION"); } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r From Connect: " + e ) ; 102 } } p u b l i c v o i d C o n n e c t T o C l i e n t ( ) { t r y { i n t cbs = c l i e n t _ b i n d S e r v e r - 1025; i n t cbk = c l i e n t _ b i n d K e y - 1025; i n t sbc = s e r v e r _ b i n d C l i e n t - 1025; i n t kbc = k e y _ b i n d C l i e n t - 1025; B y t e A r r a y O u t p u t S t r e a m d a t a b u f l = new B y t e A r r a y O u t p u t S t r e a m ) ) ,-d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f l . w r i t e ( C l i e n t _ I D ) ; d a t a b u f 1 . w r i t e ( 0 ) ; d a t a b u f 1 . w r i t e ( c b s ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f 1 . w r i t e ( c b k ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f 1 . w r i t e ( s b c ) ; d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f 1 . w r i t e ( k b c ) ; b y t e [ ] b u f l = d a t a b u f 1 . t o B y t e A r r a y ( ) ; dgOutPkt = new D a t a g r a m P a c k e t ( b u f 1 , b u f l . l e n g t h , C l i e n t A d d r e s s , C l i e n t P o r t ) ; C l i e n t S o c k e t . s e n d ( d g O u t P k t ) ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From Connect: " + e ) ; } } -p u b l i c J s e r v e r ( i n t c_bs, i n t c_bk, i n t s_bc, i n t s_bk, i n t k_bc, i n t k_bs, i n t cp, I n e t A d d r e s s c a , i n t c i d ) { t r y { M D f i l e = (new S t r i n g ( c i d + " M D " ) ) . t r i m ( ) ; q f i l e = (new S t r i n g ( c i d + " q u e r y O u t " ) ) . t r i m ( ) ; p T e xt = (new S t r i n g ( c i d + " p l a i n " ) ) . t r i m ( ) ; s T e x t = (new S t r i n g ( c i d + " p l a i n _ s i g " ) ) . t r i m ( ) ; c T e x t = (new S t r i n g ( c i d + " c i p h e r " ) ) . t r i m ( ) ; c f i l e = (new S t r i n g ( c i d + " c i p h e r t e x t " ) ) . t r i m ( ) ; pkname = (new S t r i n g ( c i d + " p k " ) ) . t r i m ( ) ; skname = (new S t r i n g f c i d + " s k " ) ) . t r i m ( ) ; serv_pkname = (new S t r i n g f c i d + " s e r v _ p k " ) ) . t r i m ( ) ; signame = (new S t r i n g f c i d + " s i g " ) ) . t r i m ( ) ; C l i e n t P o r t = cp; C l i e n t A d d r e s s = c a ; C l i e n t _ I D = c i d ; c l i e n t _ b i n d S e r v e r = c_bs; c l i e n t _ b i n d K e y = c_bk; s e r v e r _ b i n d C l i e n t = s_bc; s e r v e r _ b i n d K e y = s_bk; k e y _ b i n d C l i e n t = k_bc; k e y _ b i n d S e r v e r = k_bs; C l i e n t S o c k e t = new D a t a g r a m S o c k e t ( s e r v e r _ b i n d C l i e n t ) K e ySocket = new D a t a g r a m S o c k e t ( s e r v e r _ b i n d K e y ) ; S y s t e m . o u t . p r i n t l n ( " C l i e n t _ I D = " + C l i e n t _ I D ) ; S y s t e m . o u t . p r i n t l n ( " c l i e n t _ b i n d S e r v e r = " + c l i e n t _ b i n d S e r v e r + " c l i e n t _ b i n d K e y = " + 103 c l i e n t _ b i n d K e y + " \ n s e r v e r _ b i n d C l i e n t = " + s e r v e r _ b i n d C l i e n t + " s e r v e r _ b i n d K e y = " + s e r v e r _ b i n d K e y + " \ n k e y _ b i n d C l i e n t = " + k e y _ b i n d C l i e n t + " k e y _ b i n d S e r v e r = " + k e y _ b i n d S e r v e r ) ; CONNECTION_READY = f a l s e ; ConnectToKey () ,-C o n n e c t T o C l i e n t ( ) ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r i s : " + e ) ; S y s t e m . e x i t ( 1 ) ; } } } 104 //Appendix 10: J E n c o d e r . J a v a i m p o r t j a v a . i o . * ; i m p o r t J a v a . s e c u r i t y . * ; i m p o r t J a v a . s e c u r i t y . s p e c . * ; i m p o r t j a v a . l a n g . * ; i m p o r t j a v a x . c r y p t o . C i p h e r ; i m p o r t j a v a x . c r y p t o . K e y G e n e r a t o r ; i m p o r t j a v a x . c r y p t o . C i p h e r O u t p u t S t r e a m ; i m p o r t j a v a x . c r y p t o . C i p h e r l n p u t S t r e a m ; c l a s s J E n c o d e r { p u b l i c P r o v i d e r sunJCE; p u b l i c i n t s; p u b l i c Key S e s s i o n K e y ; p u b l i c P r i v a t e K e y p r i v ; p u b l i c P u b l i c K e y pub; p u b l i c P u b l i c K e y servPK; p u b l i c b y t e [ ] pkey; p u b l i c b y t e [ ] skey; p u b l i c b y t e [ ] r e a l S i g ; p u b l i c b y t e [ ] hash; p u b l i c i n t hashLen; p u b l i c S i g n a t u r e s i g ; p u b l i c b y t e [ ] s i g T o V e r i f y ; p u b l i c i n t M D v e r i f y ; p u b l i c J E n c o d e r ( ) { t r y { sunJCE = new c o m . s u n . c r y p t o . p r o v i d e r . S u n J C E ( ) ; s = S e c u r i t y . a d d P r o v i d e r (sunJCE) ,-} c a t c h ( E x c e p t i o n g){ S y s t e m . e r r . p r i n t l n ( " P r o v i d e r E r r o r : " + g ) ; } } p u b l i c v o i d G e n e r a t e S e s s i o n K e y ( ) { t r y { K e y G e n e r a t o r S e s s i o n G e n = K e y G e n e r a t o r . g e t l n s t a n c e ( " D E S " ) ; SecureRandom random = new SecureRandom(); S e s s i o n G e n . i n i t ( r a n d o m ) ; S e s s i o n K e y = S e s s i o n G e n . g e n e r a t e K e y ( ) ; ) c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( "Caught e x c e p t i o n " + e . t o S t r i n g ( ) ) ; } } p u b l i c v o i d G e n e r a t e K e y P a i r ( l o n g u s e r s e e d ) { t r y { K e y P a i r G e n e r a t o r P a i r G e n = K e y P a i r G e n e r a t o r . g e t l n s t a n c e ( " D S A " , "SUN"); l o n g seed = u s e r s e e d ; SecureRandom random = SecureRandom.getlnstance("SHA1PRNG", "SUN") 105 r a n d o m . s e t S e e d ( s e e d ) ; P a i r G e n . i n i t i a l i z e ( 1 0 2 4 , random); K e y P a i r p a i r = P a i r G e n . g e n e r a t e K e y P a i r ( ) ; p r i v = p a i r . g e t P r i v a t e ( ) ; pub = p a i r . g e t P u b l i c ( ) ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( "Caught e x c e p t i o n " + e . t o S t r i n g ( ) ) ; } } p u b l i c v o i d G e n e r a t e S i g n a t u r e ( S t r i n g I n f i l e , S t r i n g O u t f i l e ) { t r y { S i g n a t u r e dsa = S i g n a t u r e . g e t l n s t a n c e ( " S H A l w i t h D S A "SUN"); d s a . i n i t S i g n ( p r i v ) ; F i l e l n p u t S t r e a m f i s l = new F i l e l n p u t S t r e a m ( I n f i l e ) b y t e [ ] b u f f e r l = new b y t e [ 1 0 0 ] ; i n t l e n ; w h i l e ( f i s l . a v a i l a b l e ( ) != 0){ l e n = f i s l . r e a d f b u f f e r l ) ; d s a . u p d a t e ( b u f f e r l , 0, l e n ) ; }; f i s l . c l o s e ( ) ; r e a l S i g = d s a . s i g n ( ) ; F i l e O u t p u t S t r e a m s i g f o s = new F i l e O u t p u t S t r e a m ( O u t f i l e ) ; s i g f o s . w r i t e ( r e a l S i g ) ; s i g f o s . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( "Caught e x c e p t i o n " + e . t o S t r i n g ( ) ) ; } } p u b l i c v o i d E n c o d e F i l e ( S t r i n g f i l e n a m e , S t r i n g O u t f i l e ) ( t r y { C i p h e r c i p h e r = C i p h e r . g e t l n s t a n c e ( " D E S " , "SunJCE") c i p h e r . i n i t ( C i p h e r . E N C R Y P T _ M O D E , S e s s i o n K e y ) ; I n p u t S t r e a m i n ; Ou t p u t S t r e a m o u t ; b y t e [ ] b u f f e r = new b y t e [ 1 0 0 ] ; i n t b y t e s _ r e a d ; i n = new F i l e l n p u t S t r e a m ( f i l e n a m e ) ; out = new C i p h e r O u t p u t S t r e a m f new F i l e O u t p u t S t r e a m ( O u t f i l e ) , c i p h e r ) w h i l e ( ( b y t e s _ r e a d = i n . r e a d ( b u f f e r ) ) != -1){ o u t . w r i t e ( b u f f e r , 0, b y t e s _ r e a d ) ; }; i n . c l o s e ( ) ; o u t . c l o s e () ,-} c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( "Caught e x c e p t i o n " + e . t o S t r i n g ( ) ) ; } } 106 p u b l i c v o i d E n c o d e P u b l i c K e y ( S t r i n g pkname){ t r y { p k e y = p u b . g e t E n c o d e d ( ) ; F i l e O u t p u t S t r e a m p k e y f o s = new F i l e O u t p u t S t r e a m ( p k n a m e ) ; p k e y f o s . w r i t e ( p k e y ) ; p k e y f o s . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( "Caught e x c e p t i o n " + e . t o S t r i n g ( ) ) ; } } p u b l i c v o i d E n c o d e S e s s i o n K e y ( S t r i n g skname){ t r y { s k e y = S e s s i o n K e y . g e t E n c o d e d ( ) ; F i l e O u t p u t S t r e a m s k e y f o s = new F i l e O u t p u t S t r e a m ( s k n a m e ) ; s k e y f o s . w r i t e ( s k e y ) ; s k e y f o s . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( "Caught e x c e p t i o n . " + e . t o S t r i n g ( ) ) ; } } p u b l i c b o o l e a n V e r i f y M e s s a g e D i g e s t ( S t r i n g pw, D a t a l n p u t S t r e a m d a t a l n ) { t r y { M e s s a g e D i g e s t sha = M e s s a g e D i g e s t . g e t l n s t a n c e ( " S H A " ) b y t e [ ] pbuf = new b y t e [ p w . l e n g t h ( ) + 1 ] ; i n t p l e n = p w . l e n g t h ( ) + 1; p w . g e t B y t e s ( 0 , p w . l e n g t h ( ) , pbuf, 0 ) ; s h a . u p d a t e ( p b u f ) ; b y t e [ ] phash = s h a . d i g e s t ( ) ; b y t e [ ] r h a s h = new b y t e [ p h a s h . l e n g t h ] ; - d a t a l n . r e a d ( r h a s h ) ; S t r i n g ps = (new S t r i n g ( p h a s h ) ) . t r i m ( ) ; S t r i n g r s = (new S t r i n g ( r h a s h ) ) . t r i m ( ) ; S y s t e m . o u t . p r i n t l n ( " p s = " + ps) ; S y s t e m . o u t . p r i n t l n ( " r s = " + r s ) ; M D v e r i f y = p s . c o m p a r e T o ( r s ) ; i f ( M D v e r i f y != 0){ S y s t e m . o u t . p r i n t l n ( " W r o n g Password ! ! ! " ) ; r e t u r n f a l s e ; } i f ( M D v e r i f y == 0){ S y s t e m . o u t . p r i n t l n ( " P a s s w o r d v e r i f i e d " ) ; r e t u r n t r u e ; } r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( "Caught e x c e p t i o n " + e . t o S t r i n g ( ) ) ; r e t u r n f a l s e ; 107 } p u b l i c v o i d M a k e M e s s a g e D i g e s t ( S t r i n g pw){ t r y { M e s s a g e D i g e s t sha = M e s s a g e D i g e s t . g e t l n s t a n c e ( " S H A " ) b y t e [ ] pbuf = new b y t e [ p w . l e n g t h ( ) + 1] ; p w . g e t B y t e s ( 0 , p w . l e n g t h ( ) , pbuf, 0) ; s h a . u p d a t e ( p b u f ) ; h a sh = s h a . d i g e s t () ,-hashLen = h a s h . l e n g t h ; } c a t c h ( E x c e p t i o n e) { S y s t e m . o u t . p r i n t l n ( "Caught E x c e p t i o n " + e . t o S t r i n g ( ) ) ; } } p u b l i c v o i d D e c o d e F i l e ( S t r i n g I n F i l e N a m e , S t r i n g O u t F i l e N a m e ) { t r y { C i p h e r d e c i p h e r = C i p h e r . g e t l n s t a n c e ( " D E S " , "SunJCE") b y t e [ ] b u f f e r = new b y t e [ 1 0 0 ] ; I n p u t S t r e a m i n ; O u t p u t S t r e a m o u t ; i n t b y t e s _ r e a d ; d e c i p h e r . i n i t ( C i p h e r . D E C R Y P T _ M O D E , S e s s i o n K e y ) ; i n = new C i p h e r I n p u t S t r e a m ( new F i l e l n p u t S t r e a m ( I n F i l e N a m e ) , d e c i p h e r ) ; out = new F i l e O u t p u t S t r e a m ( O u t F i l e N a m e ) ; w h i l e ( ( b y t e s _ r e a d = i n . r e a d ( b u f f e r ) ) != -1){ o u t . w r i t e ( b u f f e r , 0, b y t e s _ r e a d ) ; }; i n . c l o s e ( ) ; o u t . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e) { S y s t e m . o u t . p r i n t l n ( "Caught E x c e p t i o n " + e . t o S t r i n g ( ) + " \ n " ) ; } } p u b l i c v o i d D e c o d e P u b l i c K e y ( S t r i n g pname){ t r y { F i l e l n p u t S t r e a m f i s = new F i l e l n p u t S t r e a m ( p n a m e ) ; b y t e [ ] encKey = new b y t e [ f i s . a v a i l a b l e ( ) ] ; f i s . r e a d ( e n c K e y ) ; f i s . c l o s e ( ) ; X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); K e y F a c t o r y k e y F a c t o r y = K e y F a c t o r y . g e t l n s t a n c e ( " D S A " "SUN"); servPK = k e y F a c t o r y . g e n e r a t e P u b l i c ( p u b K e y S p e c ) ; } c a t c h ( E x c e p t i o n e) { S y s t e m . o u t . p r i n t l n ( "Caught E x c e p t i o n " + e . t o S t r i n g O + " \ n " ) ; } } p u b l i c v o i d D e c o d e S i g n a t u r e ( S t r i n g SigName){ t r y { 108 F i l e l n p u t S t r e a m s i g f i s = A new F i l e l n p u t S t r e a m ( S i g N a m e ) ; • s i g T o V e r i f y = new b y t e [ s i g f i s . a v a i l a b l e ( ) ] ; s i g f i s . r e a d ( s i g T o V e r i f y ) ; s i g f i s . c l o s e ( ) ; s i g = S i g n a t u r e . g e t l n s t a n c e ( " S H A l w i t h D S A " , "SUN" s i g . i n i t V e r i f y ( s e r v P K ) ; } c a t c h ( E x c e p t i o n e) { S y s t e m . o u t . p r i n t l n ( -"Caught E x c e p t i o n " + e . t o S t r i n g ( ) + "\n") } } p u b l i c b o o l e a n V e r i f y S i g n a t u r e ( S t r i n g PlainName){ t r y { F i l e l n p u t S t r e a m P l a i n f i s = new F i l e l n p u t S t r e a m ( P l a i n N a m e ) ; b y t e [ ] b u f f e r l = new b y t e [ 1 0 0 ] ; i n t l e n ; w h i l e ( P l a i n f i s . a v a i l a b l e ( ) != 0){ l e n = P l a i n f i s . r e a d ( b u f f e r l ) ; s i g . u p d a t e ( b u f f e r l , 0, l e n ) ; }; P l a i n f i s . c l o s e ( ) , -b o o l e a n v e r i f i e s = s i g . v e r i f y ( s i g T o V e r i f y ) ; S y s t e m . o u t . p r i n t l n ( " s i g n a t u r e v e r i f i e s : " + v e r i f i e s + " \ n " ) ; i f ( v e r i f i e s == t r u e ) r e t u r n t r u e ; e l s e r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e) { S y s t e m . o u t . p r i n t l n ( "Caught E x c e p t i o n " + e . t o S t r i n g ( ) + "\n") r e t u r n f a l s e ; } } 109 //Appendix 11: D B s e r v e r . j ava i m p o r t j a v a . i o . * ; i m p o r t j a v a . s q l . * ; i m p o r t j ava.math.*; i m p o r t J a v a . u t i l . * ; p u b l i c c l a s s D B s erver{ p r i v a t e S t r i n g s q , s q l , sq2, sq3, sq4; p r i v a t e S t r i n g sq5, sq6, sq7, sq8, sq9; p r i v a t e S t r i n g s q l O ; p r i v a t e S t r i n g Q u e r y _ S t a t ; p r i v a t e Statement s t m t ; p r i v a t e C o n n e c t i o n conn; p r i v a t e R e s u l t S e t r s e t ; p r i v a t e S t r i n g d b U r l = "jdbc:odbc:ORCL"; p r i v a t e S t r i n g u s e r = " s c o t t " ; p r i v a t e S t r i n g password = " t i g e r " ; p u b l i c S t r i n g UserName; p u b l i c S t r i n g U s e r L e v e l ; p u b l i c S t r i n g UserPW; p u b l i c D B s e r v e r ( ) { t r y { C l a s s . f o r N a m e ( " s u n . j d b c . o d b c . J d b c O d b c D r i v e r " ) ; conn = D r i v e r M a n a g e r . g e t C o n n e c t i o n ( d b U r l , u s e r , p a s s w o r d ) ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) } } p u b l i c v o i d e x e c u t e A u t h ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; UserName = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t PASS_WD, AUTH_LEVEL from a u t h where USER_ID '" + UserName +."'"); S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; • r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; w h i l e ( r s e t . n e x t ( ) ) { UserPW = new S t r i n g ( r s e t . g e t S t r i n g ( " P A S S _ W D " ) ) . t r i m ( ) ; U s e r L e v e l = new S t r i n g ( r s e t . g e t S t r i n g ( " A U T H _ L E V E L " ) ) . t r i m ( ) ; } S t r i n g L a s t L o g = new S t r i n g ( new J a v a . u t i l . D a t e ( ) . t o S t r i n g ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( "update a u t h s e t LAST_LOGIN = '" + L a s t L o g + "'" + " where USER_ID = '" + UserName + "' ") ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; s t m t . e x e c u t e U p d a t e ( Q u e r y _ S t a t ) ; s t m t . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) 110 } } p u b l i c v o i d U p d a t e H i t s ( S t r i n g i d ) { i n t n u m _ h i t s = 0; t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t NO_HITS from p r e c o r d s where ID = '" + i d + " '") ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; w h i l e ( r s e t . n e x t ( ) ) { n u m _ h i t s = r s e t . g e t l n t ( " N O _ H I T S " ) ; } num_hits += 1; Q u e r y _ S t a t = new S t r i n g ( "update p r e c o r d s s e t NO_HITS = " + num_hits + " where ID = '" + i d + "'") ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; s t m t . e x e c u t e U p d a t e ( Q u e r y _ S t a t ) ; s t m t . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e ) ; } } p u b l i c b o o l e a n e x e c u t e Q l ( D a t a l n p u t S t r e a m d a t a l n , i n t ULEVEL){ t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from p r e c o r d s where ID = '" + s q l + " ' " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e R e s u l t ( U L E V E L ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e D e l e t e ( D a t a l n p u t S t r e a m d a t a l n , S t r i n g tableName, S t r i n g k e y ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( " d e l e t e from " + tableName + " where " + key + » = '» + s q l + " ' ") ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; s t m t . e x e c u t e U p d a t e ( Q u e r y _ S t a t ) ; s t m t . c l o s e ( ) ; stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t c o u n t ( * ) from " + tableName); 111 S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( W r i t e C o u n t R e s u l t ( ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; ) c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 2 ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; sq2 = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from p r e c o r d s where LAST = '" + s q l + "'"+ "and FIRST ='" + sq2 + " ' " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e R e s u l t ( P a c k e t T y p e . V A L I D _ U S E R ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 3 ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from p r e c o r d s where CATEGORY = '" + s q l + " ' " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e R e s u l t ( P a c k e t T y p e . V A L I D _ U S E R ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 4 ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from p r e c o r d s where CITY = '" + s q l + "'" S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e R e s u l t ( P a c k e t T y p e . V A L I D _ U S E R ) == t r u e ) 112 r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 5 ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m () ,-Q u e r y _ S t a t = new S t r i n g ( " s e l e c t p r e c o r d s . I D , p r e c o r d s . F I R S T , " + "precords.LAST, " + "precords.CATEGORY from p r e c o r d s , c r e c o r d s " "where crecords.COMPANY = '" + s q l + "' and crecords.COMPANY = precords.COMPANY" ); S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e Q 5 R e s u l t s ( ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 6 ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; sq2 = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from p r e c o r d s where CATEGORY = '" + s q l + "'"+ "and YRS >='" + sq2 + " ' " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e R e s u l t ( P a c k e t T y p e . V A L I D _ U S E R ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 7 ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from a u t h where USER_ID = '" + s q l + "'") S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ,• i f ( w r i t e A u t h R e s u l t ( ) == t r u e ) 113 return true; return f a l s e ; }catch(Exception e){ System.err.println("Error From ExecuteQuery: " + e); return f a l s e ; } } p u b l i c boolean executeQ12(DatalnputStream dataln){ try{ stmt = conn.createStatement(); sql = new String(dataln.readLine()).trim(); sq2 = new String(dataln.readLine()).trim(); Query_Stat = new String( "update precords set CLASS = '" + sq2 + "'" + " where ID = " + sql) ; System.out.println(Query_Stat); stmt.executeUpdate(Query_Stat); stmt.close(); System.out.println("Update successful!!"); stmt = conn.createStatement(); Query_Stat = new String( "select * from precords where ID = '" + s q l + " ' " ) ; rset = stmt.executeQuery(Query_Stat); if(writeResult(PacketType.VALID_ADMIN) == true) return true; return f a l s e ; }catch(Exception e){ System.err.println("Error From ExecuteQuery: " + e); return f a l s e ; } } public'boolean executeQl3(DatalnputStream dataln){ try{ stmt = conn.createStatement(); s q l = new String(dataln.readLine()).trim(); Query_Stat = new String( "select ID, NO_HITS from precords where ID = '" + s q l + " ' " ) ; System.out.println(Query_Stat); rset = stmt.executeQuery(Query_Stat); if(writeOneResult() == true) return true; return f a l s e ; }catch(Exception e){ System.err.println("Error From ExecuteQuery: " + e); return f a l s e ; } } pub l i c boolean executeQ14(DatalnputStream dataln){ try{ stmt = conn.createStatement(); sql = new String(dataln.readLine()).trim(); sq2 = new String(dataln.readLine()).trim(); Query_Stat = new String( 114 "update p r e c o r d s s e t CATEGORY = '" + sq2 + "'"+ " where ID = '" + s q l + " ' " ) ; ' S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; s t m t . e x e c u t e U p d a t e ( Q u e r y _ S t a t ) ; s t m t . c l o s e () ; S y s t e m . o u t . p r i n t l n ( " U p d a t e s u c c e s s f u l ! ! " ) ; stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from p r e c o r d s where ID = '" + s q l + " ' " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e R e s u l t ( P a c k e t T y p e . V A L I D _ A D M I N ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From Query No. 14: " + e ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q l 5 ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q = = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; s q l = new S t r i n g ( d a t a l n r e a d L i n e ( ) ) . t r i m ( ) sq2 = new S t r i n g ( d a t a l n r e a d L i n e ( ) ) . t r i m ( ) sq3 = new S t r i n g ( d a t a l n r e a d L i n e ( ) ) . t r i m ( ) sq4 = new S t r i n g ( d a t a l n r e a d L i n e ( ) ) . t r i m ( ) sq5 = new S t r i n g ( d a t a l n r e a d L i n e ( ) ) . t r i m ( ) sq6 = new S t r i n g ( d a t a l n r e a d L i n e ( ) ) . t r i m ( ) sq7 = new S t r i n g ( d a t a l n r e a d L i n e ( ) ) . t r i m ( ) sq8 = new S t r i n g ( d a t a l n r e a d L i n e ( ) ) . t r i m ( ) sq9 = new S t r i n g ( d a t a l n r e a d L i n e ( ) ) . t r i m ( ) i n t n = 0; Q u e r y _ S t a t = new S t r i n g ( " i n s e r t i n t o p r e c o r d s v a l u e s (" + s q + ", '" + s q l + "' , '" + sq2 + "' , '" + sq3 + "' , '" + sq4 + "' , '" + sq5 + "' , '" + sq6 + "', " + sq7 + " , '" + sq8 + "' , " + n + ", '" + sq9 + "' )" ); S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; s t m t . e x e c u t e U p d a t e ( Q u e r y _ S t a t ) ; s t m t . c l o s e ( ) ; S y s t e m . o u t . p r i n t l n ( " U p d a t e s u c c e s s f u l ! ! " ) ; stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from p r e c o r d s where ID = '" + s q + " ' " ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e R e s u l t ( P a c k e t T y p e . V A L I D _ A D M I N ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) ; r e t u r n f a l s e ; } } 115 p u b l i c b o o l e a n e x e c u t e Q 1 6 ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q ='new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; sq2 = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; sq3 = "Not Y e t Logged i n " ; Q u e r y _ S t a t = new S t r i n g ( " i n s e r t i n t o a u t h v a l u e s ( '" + s q + "', '" + s q l + "' , '" + sq2 + "', '" + sq3 + " ' ) " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; s t m t . e x e c u t e U p d a t e ( Q u e r y _ S t a t ) ; s t m t . c l o s e ( ) ; S y s t e m . o u t . p r i n t l n ( " U p d a t e s u c c e s s f u l ! ! " ) ; stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from a u t h where USER_ID = ' " + s q + "'") ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ,• i f ( w r i t e A u t h R e s u l t ( ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 1 7 ( D a t a l n p u t S t r e a m d a t a l n ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; s q = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; s q l = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; sq2 = new S t r i n g ( d a t a l n . r e a d L i n e ( ) ) . t r i m ( ) ; Q u e r y _ S t a t = new S t r i n g ( " i n s e r t i n t o c r e c o r d s v a l u e s ('" + s q + "', '" + s q l + "' , '" + sq2 + " ' ) " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; s t m t . e x e c u t e U p d a t e ( Q u e r y _ S t a t ) ; s t m t . c l o s e ( ) ; S y s t e m . o u t . p r i n t l n ( " U p d a t e s u c c e s s f u l ! ! " ) ; stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from c r e c o r d s where COMPANY = '" + s q + " ' " ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e C o m p a n y R e s u l t ( ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 8 ( ) { t r y { 116 stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t c o u n t ( * ) from p r e c o r d s " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( W r i t e C o u n t R e s u l t ( ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 9 ( ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from a u t h " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e A u t h R e s u l t ( ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q I O ( ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from c r e c o r d s " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e C o m p a n y R e s u l t ( ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n e x e c u t e Q 1 8 ( ) { t r y { stmt = c o n n . c r e a t e S t a t e m e n t ( ) ; Q u e r y _ S t a t = new S t r i n g ( " s e l e c t * from p r e c o r d s " ) ; S y s t e m . o u t . p r i n t l n ( Q u e r y _ S t a t ) ; r s e t = s t m t . e x e c u t e Q u e r y ( Q u e r y _ S t a t ) ; i f ( w r i t e R e s u l t ( P a c k e t T y p e . S E N D _ Q 1 8 ) == t r u e ) r e t u r n t r u e ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; 117 } p u b l i c b o o l e a n w r i t e R e s u l t ( i n t q u e r y ) { t r y { B u f f e r e d W r i t e r bwout = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J s e r v e r . q f i l e ) ) ; w h i l e ( r s e t . n e x t ( ) ) { b w o u t . w r i t e ( " I D = " ) ; s q = new S t r i n g ( r s e t . g e t S t r i n g ( " I D " ) ) . t r i m ( ) ; b w o u t . w r i t e ( s q ) ; b w o u t . n e w L i n e ( ) ; b w o u t . w r i t e ( " L A S T NAME = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " L A S T " ) ) . t r i m ( ) ) ; bwo u t . n e w L i n e ( ) ; b w o u t . w r i t e ( " F I R S T NAME = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " F I R S T " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; b w o u t . w r i t e ( " C I T Y = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " C I T Y " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; bwout.write("COMPANY = ") ; bw o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " C O M P A N Y " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; i f ( q u e r y == PacketType.VALID_ADMIN || qu e r y == PacketType.SEND_Q11){ bw o u t . w r i t e ( " C L A S S = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " C L A S S " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; } bwout.write("CATEGORY = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " C A T E G O R Y " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; b w o u t . w r i t e ( " Y R S = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " Y R S " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; bwout.write("TEL.NO = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " T E L " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; bwout.write("NO_HITS = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " N O _ H I T S " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; i f ( q u e r y == PacketType.VALID_ADMIN || q u e r y == PacketType.SEND_Q11){ b w o u t . w r i t e ( " F I L E _ N O = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " F I L E _ N O " ) ) . t r i m ( ) ) b w o u t . n e w L i n e ( ) ; b w o u t . n e w L i n e ( ) ; 118 } } b w o u t . n e w L i n e ( ) ; b w o u t . c l o s e ( ) ; s t m t . c l o s e ( ) ; r e t u r n t r u e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From W r i t e R e s u l t s : " + e r e t u r n f a l s e ; } } p u b l i c b o o l e a n w r i t e O n e R e s u l t ( ) { t r y { B u f f e r e d W r i t e r bwout = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J s e r v e r . q f i l e ) ) ; w h i l e ( r s e t . n e x t ( ) ) { b w o u t . w r i t e ( " I D = " ) ; s q = new S t r i n g ( r s e t . g e t S t r i n g ( " I D " ) ) . t r i m ( ) ; b w o u t . w r i t e ( s q ) ; bw o u t . n e w L i n e ( ) ; bwout.write("NO_HITS = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " N O _ H I T S " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; b w o u t . n e w L i n e ( ) ; } b w o u t . n e w L i n e ( ) ; b w o u t . c l o s e ( ) ; s t m t . c l o s e ( ) ; U p d a t e H i t s ( s q ) ; r e t u r n t r u e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From P r i n t R e s u l t s : "• + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n W r i t e C o u n t R e s u l t ( ) { t r y { B u f f e r e d W r i t e r bwout = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J s e r v e r . q f i l e ) ) ; w h i l e ( r s e t . n e x t ( ) ) { bwout.write("TOTAL ITEMS REMAINING = " ) ; sq = new S t r i n g ( r s e t . g e t S t r i n g ( " C O U N T ( * ) " ) ) . t r i m ( ) ; b w o u t . w r i t e ( s q ) ; b w o u t . n e w L i n e ( ) ; } b w o u t . c l o s e ( ) ; s t m t . c l o s e ( ) ; r e t u r n t r u e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From P r i n t R e s u l t s : " + e) r e t u r n f a l s e ; } } 119 p u b l i c b o o l e a n w r i t e A u t h R e s u l t ( ) { t r y { B u f f e r e d W r i t e r bwout = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J s e r v e r . q f i l e ) ) ; S t r i n g r e s ; w h i l e ( r s e t . n e x t ( ) ) { b wout.write("USER_ID = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " U S E R _ I D " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; bwout.write("AUTH_LEVEL = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " A U T H _ L E V E L " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; bwout.write("LAST_LOGIN = ") ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " L A S T _ L O G I N " ) ) . t r i m ( ) ) ; b w o u t ; n e w L i n e ( ) ; b w o u t . n e w L i n e ( ) ; } b w o u t . n e w L i n e ( ) ; b w o u t . c l o s e ( ) ; s t m t . c l o s e ( ) ; r e t u r n t r u e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From P r i n t R e s u l t s : " + e) r e t u r n f a l s e ; } } p u b l i c b o o l e a n w r i t e C o m p a n y R e s u l t ( ) { t r y { B u f f e r e d W r i t e r bwout = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J s e r v e r . q f i l e ) ) ; w h i l e ( r s e t . n e x t ( ) ) { bwout.write("COMPANY = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " C O M P A N Y " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; bwout.write("MANAGER NAME = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " M A N A G E R " ) ) . t r i m I)); b w o u t . n e w L i n e ( ) ; bwout.write("CONTACT ADDRESS = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " C O N T A C T " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; b w o u t . n e w L i n e ( ) ; } b w o u t . n e w L i n e ( ) ; b w o u t . c l o s e ( ) ; s t m t . c l o s e ( ) ; r e t u r n t r u e ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r From P r i n t R e s u l t s : " + e) r e t u r n f a l s e ; 120 } } p u b l i c b o o l e a n w r i t e Q 5 R e s u l t s ( ) { t r y { B u f f e r e d W r i t e r bwout = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( J s e r v e r . q f i l e ) ) ; w h i l e ( r s e t . n e x t ( ) ) { b w o u t . w r i t e C ' I D = " ) ; s q = new S t r i n g ( r s e t . g e t S t r i n g ( " I D " ) ) . t r i m ( ) ; b w o u t . w r i t e ( s q ) ; b w o u t . n e w L i n e ( ) ; b w o u t . w r i t e ( " F I R S T NAME = "),-b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " F I R S T " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; b w o u t . w r i t e ( " L A S T NAME = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " L A S T " ) ) . t r i m ( ) ) ; bwo u t . n e w L i n e ( ) ; bwout.write("CATEGORY = " ) ; b w o u t . w r i t e ( n e w S t r i n g ( r s e t . g e t S t r i n g ( " C A T E G O R Y " ) ) . t r i m ( ) ) ; b w o u t . n e w L i n e ( ) ; b w o u t . n e w L i n e ( ) ; } b w o u t . n e w L i n e ( ) ; b w o u t . c l o s e ( ) ; s t m t . c l o s e ( ) ; r e t u r n t r u e ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r From P r i n t R e s u l t s : " + e ) ; r e t u r n f a l s e ; } } p u b l i c b o o l e a n E x e c u t e Q u e r y ( D a t a l n p u t S t r e a m d a t a l n , i n t opcode, b o o l e a n ACK_RECIEVED){ b o o l e a n r e s u l t = f a l s e ; t r y { i f ( o p c o d e == PacketType.SEND_Q1 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q l ( d a t a l n , PacketType.SEND_Q1) } e l s e i f ( o p c o d e == PacketType.SEND_Q2 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 2 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q3 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 3 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q4 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 4 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q5 && 121 ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 5 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q6 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 6 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q7 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 7 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q11 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q l ( d a t a l n , PacketType.SEND_Q11); } e l s e i f ( o p c o d e == PacketType.SEND_Q12 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 1 2 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q13 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 1 3 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q14 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 1 4 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q15 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q l 5 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q16 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q l 6 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q17 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 1 7 ( d a t a l n ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q19 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e D e l e t e ( d a t a l n , " p r e c o r d s " , "ID"),-} e l s e i f ( o p c o d e == PacketType.SEND_Q20 && AGK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e D e l e t e ( d a t a l n , " a u t h " , "USER_ID" )' ; } e l s e i f ( o p c o d e == PacketType.SEND_Q21 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e D e l e t e ( d a t a l n , " c r e c o r d s " , "COMPANY"); } e l s e i f ( o p c o d e == PacketType.SEND_Q8 && AC K_RECIEVED == t r u e ) { 122 r e s u l t = e x e c u t e Q 8 ( ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q9 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 9 ( ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q10 && ACK_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q i O ( ) ; } e l s e i f ( o p c o d e == PacketType.SEND_Q18 && AC K_RECIEVED == t r u e ) { r e s u l t = e x e c u t e Q 1 8 ( ) ; } e l s e r e t u r n f a l s e ; i f ( r e s u l t == t r u e ) r e t u r n t r u e ; e l s e r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r From ExecuteQuery: " + e) r e t u r n f a l s e ; } } } 123 //Appendix 12: m a i n k e y . J a v a i m p o r t j a v a . n e t . * ; i m p o r t j a v a . i o . * ; i m p o r t J a v a . u t i l . * ; i m p o r t j ava.awt.*; i m p o r t J a v a . l a n g . T h r e a d ; p u b l i c c l a s s mainkey e x t e n d s Thread{ p r i v a t e DatagramPacket d g l n P k t ; p r i v a t e DatagramSocket main_soc; p r i v a t e b y t e [ ] - buf = new b y t e [ 1 0 0 0 ] ; p u b l i c m a i n k e y ( ) { t r y { w h i l e ( t r u e ) { main_soc = new D a t a g r a m S o c k e t ( 2 0 0 5 ) ; S y s t e m . o u t . p r i n t l n ( " J a v a Key S e r v e r S t a r t e d " ) d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) m a i n _ s o c . r e c e i v e ( d g l n P k t ) ; new J K e y ( d g l n P k t ) . s t a r t ( ) ; m a i n _ s o c . c l o s e ( ) ; S y s t e m . o u t . p r i n t l n ( "Java Key S e r v e r r e a d y f o r n e x t r e q u e s t . . " ) ; } } c a t c h ( S o c k e t E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " c a n ' t open s o c k e t " ) ; S y s t e m . e x i t ( 1 ) ; } c a t c h ( I O E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " C o m m u n i c a t i o n e r r o r " ) ; e . p r i n t S t a c k T r a c e ( ) ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( " E r r o r i s : " + e ) ; S y s t e m . e x i t ( 1 ) ; } } p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { new m a i n k e y ( ) ; } 124 I/Appendix 13: J K e y . j ava i m p o r t java.net.*,-i m p o r t j a v a . i o . * ; i m p o r t j a v a . u t i l . * ; i m p o r t J a v a . l a n g . T h r e a d ; p u b l i c c l a s s J K e y e x t e n d s Thread{ p r i v a t e DatagramPacket d g l n P k t ; p r i v a t e DatagramPacket dgOutPkt; p r i v a t e DatagramSocket C l i e n t S o c k e t ; p r i v a t e i n t C l i e n t P o r t ; p r i v a t e i n t C l i e n t _ I D ; p r i v a t e DatagramSocket S e r v e r S o c k e t ; p r i v a t e i n t S e r v e r P o r t ; p r i v a t e i n t k e y _ b i n d C l i e n t ; p r i v a t e i n t k e y _ b i n d S e r v e r ; p r i v a t e i n t opcode; p r i v a t e i n t d a t a L e n ; p r i v a t e I n e t A d d r e s s C l i e n t A d d r e s s ; p r i v a t e I n e t A d d r e s s S e r v e r A d d r e s s ; p r i v a t e i n t t r a n s f e r C o m p l e t e ; p r i v a t e i n t OutCode;' p r i v a t e D a t a l n p u t S t r e a m dataln,-p r i v a t e S t r i n g pkname; p r i v a t e S t r i n g serv_pkname; p r i v a t e S t r i n g skname; p r i v a t e S t r i n g s t i c k e r -p r i v a t e S t r i n g c t i c k e t ; p r i v a t e J T i c k e t j t ; p r i v a t e b y t e [ ] buf = new b y t e [ 1 0 0 0 ] ; p r i v a t e b y t e [ ] k b u f ; p r i v a t e b y t e [ ] pbuf; p r i v a t e b o o l e a n ACK_RECIEVED=true; p r i v a t e b o o l e a n GO_AHEAD = f a l s e ; p r i v a t e b o o l e a n CLIENT_AUTH = f a l s e ; p u b l i c v o i d m a k e A C K P a c k e t ( i n t ocode, I n e t A d d r e s s d e s t , i n t d e s t P o r t ) { t r a n s f e r C o m p l e t e = 1; t r y { B y t e A r r a y O u t p u t S t r e a m d a t a b u f l = new B y t e A r r a y O u t p u t S t r e a m () ,-d a t a b u f l . w r i t e ( 0 ) ; d a t a b u f 1 . w r i t e (ocode) ,-d a t a b u f 1 . w r i t e ( 0 ) ; d a t a b u f l . w r i t e ( t r a n s f e r C o m p l e t e ) ; b y t e [ ] b u f l = d a t a b u f 1 . t o B y t e A r r a y ( ) ; dgOutPkt = new D a t a g r a m P a c k e t ( b u f 1 , b u f l . l e n g t h , d e s t , d e s t P o r t ) ,-} c a t c h ( E x c e p t i o n a){ System, e r r . p r i n t l n ( " E r r o r : " + a) ,-} } p u b l i c v o i d m a k e O u t P a c k e t ( b y t e [ ] b, i n t o_code, i n t f l a g , I n e t A d d r e s s d e s t , i n t d e s t P o r t ) { 125 t r y { B y t e A r r a y O u t p u t S t r e a m d a t a b u f = new B y t e A r r a y O u t p u t S t r e a m ( ) ; d a t a b u f . w r i t e ( 0 ) ; d a t a b u f . w r i t e ( o _ c o d e ) ; d a t a b u f . w r i t e ( 0 ) ; d a t a b u f . w r i t e ( f l a g ) ; d a t a b u f . w r i t e ( b ) ; b y t e [ ] buf 2 = d a t a b u f . t o B y t e A r r a y ( ) ; dgOutPkt = new D a t a g r a m P a c k e t ( b u f 2 , b u f 2 . l e n g t h , d e s t , d e s t P o r t ) ; } c a t c h ( E x c e p t i o n e ){ S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e ) ; } } p u b l i c v o i d R e t r i e v e C l i e n t P a c k e t ( ) { t r y { d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; C l i e n t S o c k e t . r e c e i v e ( d g l n P k t ) ; C l i e n t P o r t = d g l n P k t . g e t P o r t ( ) ; C l i e n t A d d r e s s = d g l n P k t . g e t A d d r e s s ( ) ; d a t a l n = new D a t a l n p u t S t r e a m ( new B y t e A r r a y l n p u t S t r e a m ( d g l n P k t . g e t D a t a ( ) ) ) opcode = d a t a l n . r e a d S h o r t ( ) ; t r a n s f e r C o m p l e t e = d a t a l n . r e a d S h o r t ( ) ; d a t a L e n = d g l n P k t . g e t L e n g t h ( ) ; pbuf = new b y t e [ d a t a L e n - PacketType.EMPTY_PKT]; d a t a l n . r e a d F u l l y ( p b u f , 0, d a t a L e n - PacketType.EMPTY_PKT); ) c a t c h ( E x c e p t i o n c ) { S y s t e m . e r r . p r i n t l n ( " E r r o r From G e t C l i e n t F i l e R e q u e s t : " + c ) ; } } p u b l i c v o i d R e t r i e v e D a t a S e r v e r P a c k e t ( ) { t r y { d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; S e r v e r S o c k e t . r e c e i v e ( d g l n P k t ) ; S e r v e r P o r t = d g l n P k t . g e t P o r t ( ) ; S e r v e r A d d r e s s = d g l n P k t . g e t A d d r e s s ( ) ; d a t a L e n = d g l n P k t . g e t L e n g t h ( ) ; d a t a l n = new D a t a l n p u t S t r e a m ( new B y t e A r r a y l n p u t S t r e a m ( d g l n P k t . g e t D a t a ( ) ) ) opcode = d a t a l n . r e a d S h o r t ( ) ; t r a n s f e r C o m p l e t e = d a t a l n . r e a d S h o r t ( ) ; k b uf = new b y t e [ d a t a L e n - PacketType.EMPTY_PKT]; d a t a l n . r e a d F u l l y ( k b u f , 0, d a t a L e n - PacketType.EMPTY_PKT); } c a t c h ( E x c e p t i o n d){ S y s t e m . e r r . p r i n t l n ( " E r r o r From G e t C l i e n t F i l e R e q u e s t : " + d ) ; } } p u b l i c v o i d G e t P K W r i t e R e q u e s t ( ) { 126 t r y { F i l e O u t p u t S t r e a m p k e y f o s = new F i l e O u t p u t S t r e a m ( p k n a m e ) ; p k e y f o s . w r i t e ( k b u f ) ; p k e y f o s . c l o s e ( ) ; makeACKPacket(PacketType.ACK_PUBKEY, S e r v e r A d d r e s s S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e) ; } } p u b l i c v o i d G e t S K W r i t e R e q u e s t ( ) { t r y { F i l e O u t p u t S t r e a m s k e y f o s = new F i l e O u t p u t S t r e a m ( s k n a m e ) ; s k e y f o s . w r i t e ( k b u f ) ; s k e y f o s . c l o s e ( ) ; makeACKPacket(PacketType.ACK_SESKEY, S e r v e r A d d r e s s S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; } c a t c h ( E x c e p t i o n f ) { S y s t e m . e r r . p r i n t l n ( " E r r o r : " + f ) ; } } p u b l i c v o i d W r i t e T i c k e t ( ) { t r y { F i l e O u t p u t S t r e a m T f o s = new F i l e O u t p u t S t r e a m ( s t i c k e t ) ; T f o s . w r i t e ( k b u f ) ; T f o s . c l o s e ( ) ; makeACKPacket(PacketType.ACK_TICKET, S e r v e r A d d r e s s S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; } c a t c h ( E x c e p t i o n f ) { S y s t e m . e r r . p r i n t l n ( " E r r o r : " + f ) ; } } p u b l i c b o o l e a n V e r i f y T i c k e t ( ) { t r y { i n t code; CLIENT_AUTH = f a l s e ; F i l e O u t p u t S t r e a m T f o s = new F i l e O u t p u t S t r e a m ( c t i c k e t ) ; T f o s . w r i t e ( p b u f ) ; T f o s . c l o s e ( ) ; i f ( j t . V e r i f y T i c k e t ( s t i c k e t , c t i c k e t ) == t r u e ) { C LIENT_AUTH = t r u e ; r e t u r n t r u e ; } r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n g){ S y s t e m . e r r . p r i n t l n ( " E r r o r : " + g ) ; r e t u r n f a l s e ; 127 } } p u b l i c void GetPKReadRequest(){ try{ FilelnputStream k e y f i s = new FilelnputStream(pkname) byte[] encPKey = new b y t e [ k e y f i s . a v a i l a b l e ( ) ] ; keyfis.read(encPKey); k e y f i s . c l o s e ( ) ; transferComplete=l; makeOutPacket(encPKey,PacketType.ACK_PUBKEY, transferComplete, ClientAddress, C l i e n t P o r t ) ; ClientSocket.send(dgOutPkt); }catch(Exception g){ System.err.println("Error: " + g); } } p u b l i c v o i d GetSKReadRequest(){ try{ FilelnputStream skeyfis = new FilelnputStream(skname); byte[] encSkey = new byt e [ s k e y f i s . a v a i l a b l e ( ) ] ; skeyfis.read(encSkey); s k e y f i s . c l o s e ( ) ; transferComplete=l; makeOutPacket(encSkey,PacketType.ACK_SESKEY, transferComplete, ClientAddress, C l i e n t P o r t ) ; ClientSocket.send(dgOutPkt); }catch(Exception h){ System.err.println("Error: " + h); } } public void WritePubKey(){ try{ FileOutputStream pkeyfos = new FileOutputStream(serv_pkname); pkeyfos.write(pbuf); pkeyfos.close(); makeACKPacket(PacketType.ACK, ClientAddress, C l i e n t P o r t ) ; ClientSocket.send(dgOutPkt); }catch(Exception e){ System.err.println("Error: " + e); } } pub l i c void ReadPubKey(){ try{ FilelnputStream k e y f i s = new FilelnputStream(serv_pkname); byte[] encPKey = new b y t e [ k e y f i s . a v a i l a b l e ( ) ] ; keyfis.read(encPKey); k e y f i s . c l o s e ( ) ; transferComplete=l; makeOutPacket(encPKey,PacketType.ACK, 128 t r a n s f e r C o m p l e t e , S e r v e r A d d r e s s , S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; } c a t c h ( E x c e p t i o n g){ S y s t e m . e r r . p r i n t l n ( " E r r o r : " + g ) ; } } p u b l i c b o o l e a n G e t S e r v e r R e q u e s t ( ) { t r y { R e t r i e v e D a t a S e r v e r P a c k e t ( ) ; i f ( o p c o d e == PacketType.WRITE_PUBKEY){ S y s t e m . o u t . p r i n t l n ( "Data S e r v e r ' s P u b l i c Key W r i t e R e q u e s t " ) ; G e t P K W r i t e R e q u e s t ( ) ; r e t u r n t r u e ; } e l s e i f ( o p c o d e == PacketType.WRITE_TICKET){ S y s t e m . o u t . p r i n t l n ( "Data S e r v e r ' s T i c k e t W r i t e R e q u e s t " ) ; W r i t e T i c k e t ( ) ; r e t u r n t r u e ; } e l s e i f ( o p c o d e == PacketType.WRITE_SESKEY){ S y s t e m . o u t . p r i n t l n ( "Data S e r v e r ' s S e s s i o n Key W r i t e R e q u e s t " ) ; G e t S K W r i t e R e q u e s t ( ) ; r e t u r n t r u e ; } e l s e i f ( o p c o d e == PacketType.READ_PUBKEY){ S y s t e m . o u t . p r i n t l n ( "Data S e r v e r ' s P u b l i c Key Read R e q u e s t " ) ; ReadPubKey(); r e t u r n t r u e ; } e l s e makeACKPacket(PacketType.ERROR, S e r v e r A d d r e s s S e r v e r P o r t ) ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e l ) { S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e l ) ; r e t u r n t r u e ; } } p u b l i c b o o l e a n G e t C l i e n t R e q u e s t ( ) { t r y { R e t r i e v e C l i e n t P a c k e t ( ) ; i f ( o p c o d e == PacketType.READ_PUBKEY){ S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s P u b l i c Key Read R e q u e s t " ) ; S y s t e m . o u t . p r i n t l n ( " C l i e n t V e r i f y i n g T i c k e t " ) i f ( V e r i f y T i c k e t ( ) ==-true){ Ge t P K R e a d R e q u e s t ( ) ; r e t u r n t r u e ; } e l s e 129 r e t u r n f a l s e ; } e l s e i f ( o p c o d e == PacketType.READ_SESKEY){ S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s S e s s i o n Key Read R e q u e s t " ) ; S y s t e m . o u t . p r i n t l n ( " C l i e n t V e r i f y i n g T i c k e t " ) i f ( V e r i f y T i c k e t ( ) == t r u e ) { G e t S K R e a d R e q u e s t ( ) ; r e t u r n t r u e ; ) e l s e r e t u r n f a l s e ; } e l s e i f ( o p c o d e == PacketType.WRITE_PUBKEY){ S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s P u b l i c Key W r i t e R e q u e s t " ) ; W r i t e P u b K e y ( ) ; r e t u r n t r u e ; } e l s e makeACKPacket(PacketType.ERROR, C l i e n t A d d r e s s C l i e n t P o r t ) ; r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e2){ S y s t e m . e r r . p r i n t l n ( " E r r o r : " + e 2 ) ; r e t u r n f a l s e ; } } p u b l i c v o i d r u n ( ) { t r y { j t = new J T i c k e t f ) ; w h i l e ( t r u e ) { GO_AHEAD = G e t S e r v e r R e q u e s t ( ) ; S y s t e m . o u t . p r i n t l n ( " S e r v e r ' s Request: " + GO_AHEAD); GO_AHEAD = G e t S e r v e r R e q u e s t ( ) ; S y s t e m . o u t . p r i n t l n ( " S e r v e r ' s Request: " + GO_AHEAD); GO_AHEAD = G e t C l i e n t R e q u e s t ( ) ; S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s Request: " + GO_AHEAD); GO_AHEAD = G e t S e r v e r R e q u e s t ( ) ; S y s t e m . o u t . p r i n t l n ( " S e r v e r ' s Request: " + GO_AHEAD); GO_AHEAD = G e t C l i e n t R e q u e s t ( ) ; S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s Request: " + GO_AHEAD); GO_AHEAD = G e t C l i e n t R e q u e s t ( ) ; S y s t e m . o u t . p r i n t l n ( " C l i e n t ' s Request: " + GO_AHEAD); GO_AHEAD = G e t S e r v e r R e q u e s t ( ) ; S y s t e m . o u t . p r i n t l n ( " S e r v e r ' s Request: " + GO_AHEAD); S y s t e m . o u t . p r i n t l n ( "Key S e r v e r r e a d y f o r n e x t r e q u e s t .... " ) ; } 130 } c a t c h ( E x c e p t i o n e3){ S y s t e m . e r r . p r i n t l n ( " E r r o r i n r u n : " + e 3 ) ; S y s t e m . e x i t ( 1 ) ; } } p u b l i c J K e y ( D a t a g r a m P a c k e t m a i n P k t ) { t r y { d a t a l n = new D a t a l n p u t S t r e a m ( new B y t e A r r a y l n p u t S t r e a m ( m a i n P k t . g e t D a t a ( ) ) ) ; C l i e n t _ I D = d a t a l n . r e a d S h o r t ( ) ; k e y _ b i n d S e r v e r = d a t a l n . r e a d S h o r t ( ) ; k e y _ b i n d C l i e n t = d a t a l n . r e a d S h o r t ( ) ; S e r v e r P o r t = d a t a l n . r e a d S h o r t ( ) ; C l i e n t P o r t = d a t a l n . r e a d S h o r t ( ) ; k e y _ b i n d S e r v e r += 1025; k e y _ b i n d C l i e n t += 1025; S e r v e r P o r t += 1025; C l i e n t P o r t += 1025; S e r v e r A d d r e s s = m a i n P k t . g e t A d d r e s s ( ) ; C l i e n t A d d r e s s = InetAddress.getByName("142.103.10.55"); pkname = C l i e n t _ I D + "PK"; serv_pkname = C l i e n t _ I D + "serv_PK"; skname = C l i e n t _ I D + "SK"; s t i c k e t = C l i e n t _ I D + "ST"; c t i c k e t = C l i e n t _ I D + "CT"; S e r v e r S o c k e t = new D a t a g r a m S o c k e t ( k e y _ b i n d S e r v e r ) ; C l i e n t S o c k e t = new D a t a g r a m S o c k e t ( k e y _ b i n d C l i e n t ) ; makeACKPacket(PacketType.READY, S e r v e r A d d r e s s , S e r v e r P o r t ) ; S e r v e r S o c k e t . s e n d ( d g O u t P k t ) ; S y s t e m . o u t . p r i n t l n ( " C l i e n t _ I D = " + C l i e n t _ I D ) ; S y s t e m . o u t . p r i n t l n ( " S e r v e r P o r t = " + S e r v e r P o r t + " C l i e n t P o r t = " + C l i e n t P o r t ) ; S y s t e m . o u t . p r i n t l n ( " S e r v e r B i n d e d @ " + k e y _ b i n d S e r v e r + " C l i e n t B i n d e d @ " + k e y _ b i n d C l i e n t ) ; d g l n P k t = new D a t a g r a m P a c k e t ( b u f , b u f . l e n g t h ) ; S e r v e r S o c k e t . r e c e i v e ( d g l n P k t ) ; d a t a l n = new D a t a l n p u t S t r e a m ( new B y t e A r r a y l n p u t S t r e a m ( d g l n P k t . g e t D a t a ( ) ) ) ; opcode = d a t a l n . r e a d S h o r t ( ) ; i f ( o p c o d e == PacketType.READY){ System.out.println("CONNECTION READY !!!!!!") makeACKPacket(PacketType.READY, C l i e n t A d d r e s s C l i e n t P o r t ) ; C l i e n t S o c k e t . s e n d ( d g O u t P k t ) ; } } c a t c h ( S o c k e t E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " c a n ' t open s o c k e t " ) ; S y s t e m . e x i t ( 1 ) ; } c a t c h ( I O E x c e p t i o n e ) { S y s t e m . e r r . p r i n t l n ( " C o m m u n i c a t i o n e r r o r " ) ; e . p r i n t S t a c k T r a c e ( ) ; } } 131 } 132 //Appendix 14: J T i c k e t . J a v a i m p o r t j a v a . i o . * ; i m p o r t j a v a . s e c u r i t y . * ; i m p o r t j a v a . s e c u r i t y . s p e c . * ; i m p o r t j a v a . l a n g . * ; c l a s s J T i c k e t { p u b l i c P r o v i d e r sunJCE; p u b l i c i n t M D v e r i f y ; p u b l i c i n t s; p u b l i c J T i c k e t ( ) { t r y { ' sunJCE = new c o m . s u n . c r y p t o . p r o v i d e r . S u n J C E ( ) ; s = S e c u r i t y . a d d P r o v i d e r ( s u n J C E ) ; } c a t c h ( E x c e p t i o n g){ S y s t e m . o u t . p r i n t l n ( " P r o v i d e r E r r o r : " + g + " \ n " ) ; } } p u b l i c b o o l e a n V e r i f y T i c k e t ( S t r i n g S e r v e r T i c k e t , S t r i n g C l i e n t T i c k e t ) { t r y { F i l e l n p u t S t r e a m s f i s = new F i l e l n p u t S t r e a m ( S e r v e r T i c k e t ) ; b y t e [ ] s h a s h = new b y t e [ s f i s . a v a i l a b l e ( ) ] ; s f i s . r e a d ( s h a s h ) ; F i l e l n p u t S t r e a m c f i s = new F i l e l n p u t S t r e a m ( C l i e n t T i c k e t ) ; b y t e [ ] c h a s h = new b y t e [ c f i s . a v a i l a b l e ( ) ] ; c f i s . r e a d ( c h a s h ) ; S t r i n g s s = (new S t r i n g ( s h a s h ) ) . t r i m ( ) ; S t r i n g c s = (new S t r i n g ( c h a s h ) ) . t r i m ( ) ; S y s t e m . o u t . p r i n t l n ( " s s = " + s s ) ; S y s t e m . o u t . p r i n t l n ( " c s = " + c s ) ; M D v e r i f y = s s . c o m p a r e T o ( c s ) ; i f ( M D v e r i f y != 0){ S y s t e m . o u t . p r i n t l n ( " T i c k e t Not V e r i f i e d ! ! ! " ) ; r e t u r n f a l s e ; } i f ( M D v e r i f y == 0){ S y s t e m . o u t . p r i n t l n ( " T i c k e t V e r i f i e d " ) ; r e t u r n t r u e ; } r e t u r n f a l s e ; } c a t c h ( E x c e p t i o n e){ S y s t e m . e r r . p r i n t l n ( "Caught e x c e p t i o n " + e . t o S t r i n g ( ) ) ; r e t u r n f a l s e ; } } } 133 

Cite

Citation Scheme:

        

Citations by CSL (citeproc-js)

Usage Statistics

Share

Embed

Customize your widget with the following options, then copy and paste the code below into the HTML of your page to embed this item in your website.
                        
                            <div id="ubcOpenCollectionsWidgetDisplay">
                            <script id="ubcOpenCollectionsWidget"
                            src="{[{embed.src}]}"
                            data-item="{[{embed.item}]}"
                            data-collection="{[{embed.collection}]}"
                            data-metadata="{[{embed.showMetadata}]}"
                            data-width="{[{embed.width}]}"
                            async >
                            </script>
                            </div>
                        
                    
IIIF logo Our image viewer uses the IIIF 2.0 standard. To load this item in other compatible viewers, use this url:
http://iiif.library.ubc.ca/presentation/dsp.831.1-0051613/manifest

Comment

Related Items