1 / 20

A Short Tutorial for Implementing CS 421 AS1 in Java

A Short Tutorial for Implementing CS 421 AS1 in Java. Outline. Hypertext Transfer Protocol Request Messages Response Messages CS 421 - Programming Assignment 1 Java Socket Programming Socket class Sending data Receiving data Final Words. 2. Hypertext Transfer Protocol (HTTP).

nita-coffey
Télécharger la présentation

A Short Tutorial for Implementing CS 421 AS1 in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Short Tutorial for Implementing CS 421 AS1in Java

  2. Outline • Hypertext Transfer Protocol • Request Messages • Response Messages • CS 421 - Programming Assignment 1 • Java Socket Programming • Socket class • Sending data • Receiving data • Final Words 2

  3. Hypertext Transfer Protocol (HTTP) • Client/Server Model • Two types of HTTP messages • Request messages hasmethods GET, HEAD, POST, PUT, DELETE etc. • Response messages hasstatus codes 200 OK, 206 Partial Content, 301 Moved Permanently, 302 Found, 404 Not Found etc. • See RFC 2616 for details on HTTP/1.1 protocol. 3

  4. HTTP Request Messages • GET: Retrieve whatever information is identified by the requested file • GET /~cs421/file.txt HTTP/1.1\r\n Host: www.foo.com\r\n \r\n • Partial GET: Request only part of the entity within a range • GET /~cs421/file.txt HTTP/1.1\r\n Host: www.foo.com\r\n Range: bytes=100-199\r\n \r\n 4

  5. HTTP Request Messages (cont'd) • HEAD: Identical to GET request message except that the server MUST NOT return a message body • HEAD /~cs421/file.txt HTTP/1.1\r\n Host: www.foo.com\r\n \r\n • The length of the file (in bytes) is returned in Content-Length field in the response message of HEAD request message. 5

  6. HTTP Response Messages • Some of the status codes of response messages: • 200 OK • 206 Partial Content • 301 Moved Permanently • 404 Not Found • The first line of the response message includes the status of the file. 6

  7. HTTP Response Messages (cont'd) • 200 OK: The file is found • HTTP/1.1 200 OK\r\n Content-Length: 1000\r\n Content-Type: text/html\r\n \r\n <content-of-the-requested-file> • If the request method is HEAD, <content-of-the-requested-file> is not returned in the response message. 7

  8. HTTP Response Messages (cont'd) • 206 Partial Content: The part of the requested file is found • HTTP/1.1 206 Partial Content\r\n Date: Mon, 11 Mar 2013 10:23:10 GMT\r\n Content-Length: 51\r\n Content-Range: bytes 100-150/972\r\n Content-Type: text/html\r\n ETag: "2abd17-3cc-4d73d881691c0"\r\n \r\n <content-of-the-requested-part-of-file> 8

  9. HTTP Response Messages (cont'd) • 301 Moved Permanently: A new permanent URL is assigned to the file • HTTP/1.1 301 Moved Permanently\r\n Location: http://www.foo.com/~cs421/file.txt\r\n \r\n • File can be found in the URL returned in the Location header. • 404 Not Found: File is not found • HTTP/1.1 404 Not Found\r\n \r\n 9

  10. Programming Assignment • You are asked to implement a program that • downloads a text file, • decrypt the encrypted lines • save the decrypted content to the local directory. • Client program will be a console application and should be run with the following command in the shell: • java FileDecrypter <file-url> 10

  11. Programming Assignment (cont'd) • Command-line arguments are obtained from the String array parameter of the main method. • The file is requested from the server with an HTTP GET request message. • If the file is found, the response message is 200 OK. • If the file is not found, the response message is not 200 OK 11

  12. AES Cryptography Key Key Text Encrypted Text Text Symmetric

  13. Programming Assignment (cont'd) Encrypted File 0^Bfivgg]qP?uFeJ KEY sPref2IjC4ZjcSEQ/BqqRJMBFi5azd8aXSATC2hpm7nTfvBSF4CHE5VrzuCBs2E6 kCWAOM2cJgar/jsUVbYdu6DjVgue02rzXHKHnRvSCHy1X/AJIaKgGLXfY2/VYKXT ………. Encoded amount of encrypted data with AES 13

  14. Programming Assignment (cont'd) Decrypted File Cleopatra Abdou 1124440 53 1711 Jesse Acevedo 1447506 20 3712 … In AS1, you can check decrypted data with decrypted_file_1 and decrypted_file_2 14

  15. Java Sockets • The socket API in Java is available through the classes in the java.net package. • URL, URLConnection, Socket, ServerSocket use TCP. • DatagramPacket, DatagramSocket, MulticastSocket use UDP. 15

  16. Java Sockets – Socket Class • Socket implements client sockets, an endpoint communication between two machines. • Socket(String host, int portNum) is a constructor of Socket class. • Instantiates a new socket and tries to connect it to the specified port number portNum at the specified named host. • UnknownHostException may be thrown. • Note that, HTTP port number is 80. 16

  17. Java Sockets – Sending Data • Here is a code snippet to send data: • If you send data through a buffered stream, don't forget to flush it. • If you don't, your data may stay in a local buffer, waiting for more data to be transmitted. 17

  18. Java Sockets – Receiving Data • Here is a code snippet to receive data: • For the above snippet to work as expected: • either the remote end-point should send a string terminated by a newline, or • the remote end-point should close its associated output stream (or socket) making sure all output data is flushed. 18

  19. Decryption using AES Using decode method of Base64 class to have encrypted lines in array of byte form Using Cipher class and calls its getInstance method Call init method of Cipher with given secret key and in DECRYPT_MODE In the end, dofinal method finishe decrypted process, output of this method is array of bytes

  20. Final Words • You should read the assignment carefully. • You should not use the classes that are not allowed (in the assigment). • Your program should be run as described in the assignment. • You should follow all the submission rules. • Please, do not hesitate to ask questions to your assistant. 20

More Related