Java Programming Lecture 10

Similar documents
JAVA.LANG.INTEGER CLASS

CSC Java Programming, Fall Java Data Types and Control Constructs

Basic data types. Building blocks of computation

Class Library java.lang Package. Bok, Jong Soon

An Interesting Article. How Revolutionary Tools Cracked a 1700s Code.

JAVA WRAPPER CLASSES

PIC 20A Number, Autoboxing, and Unboxing

Language Fundamentals Summary

Exercise: Using Numbers

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Java characters Lecture 8

CS2141 Software Development using C/C++ C++ Basics

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

The type of all data used in a C++ program must be specified

The type of all data used in a C (or C++) program must be specified

Chapter 2 Elementary Programming

Zheng-Liang Lu Java Programming 45 / 79

COMP2611: Computer Organization. Data Representation

JAVA.LANG.CHARACTER CLASS

Systems Programming/ C and UNIX

COMP6700/2140 Data and Types

Java Programming Lecture 9

Number Representation & Conversion

Cryptography (DES+RSA) by Amit Konar Dept. of Math and CS, UMSL

World Inside a Computer is Binary

More Programming Constructs -- Introduction

M1 Computers and Data

ITI Introduction to Computing II

Data Types and Variables

ITI Introduction to Computing II

VARIABLES AND TYPES CITS1001

UNIT 7A Data Representation: Numbers and Text. Digital Data

Chapter 3. Information Representation

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Data Representation 1

Introduction to Programming Using Java (98-388)

Administrivia. CMSC 216 Introduction to Computer Systems Lecture 24 Data Representation and Libraries. Representing characters DATA REPRESENTATION

A New Symmetric Key Algorithm for Modern Cryptography Rupesh Kumar 1 Sanjay Patel 2 Purushottam Patel 3 Rakesh Patel 4

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Cryptography Basics. IT443 Network Security Administration Slides courtesy of Bo Sheng

ECE 122. Engineering Problem Solving with Java


Cryptographic Concepts

Princess Nora Bint Abdulrahman University College of computer and information sciences Networks department Networks Security (NET 536)

Java Classes: Math, Integer A C S L E C T U R E 8

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

Object-Oriented Programming

Digital Computers and Machine Representation of Data

Encryption. INST 346, Section 0201 April 3, 2018

Chapter 2. Data Representation in Computer Systems

Inf2C - Computer Systems Lecture 2 Data Representation

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CS101 Lecture 04: Binary Arithmetic

9/30/2016. Cryptography Basics. Outline. Encryption/Decryption. Cryptanalysis. Caesar Cipher. Mono-Alphabetic Ciphers

Security: Cryptography

Cryptography Functions

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Computer System and programming in C

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Final Labs and Tutors

MACHINE LEVEL REPRESENTATION OF DATA

Security+ Guide to Network Security Fundamentals, Third Edition. Chapter 11 Basic Cryptography

IEEE Std and IEEE Std 1363a Ashley Butterworth Apple Inc.

Pretty Good Privacy (PGP

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

Slides by Kent Seamons and Tim van der Horst Last Updated: Oct 7, 2013

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Introduction to Network Security Missouri S&T University CPE 5420 Data Integrity Algorithms

Computer Networks. Network Security and Ethics. Week 14. College of Information Science and Engineering Ritsumeikan University

Chapter 4: Computer Codes. In this chapter you will learn about:

Implementing Cryptography: Good Theory vs. Bad Practice

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Type Checking. Chapter 6, Section 6.3, 6.5

COMP Primitive and Class Types. Yi Hong May 14, 2015

Chapter 6 Primitive types

Variables and Data Representation

Programming with Java

Security. Communication security. System Security

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Diskrečioji matematika

Understand Computer Storage and Data Types

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

S. Erfani, ECE Dept., University of Windsor Network Security

Characters, Strings, and Floats

Thus needs to be a consistent method of representing negative numbers in binary computer arithmetic operations.

Lecture 5. Cryptographic Hash Functions. Read: Chapter 5 in KPS

Network and System Security

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Message Authentication and Hash function 2

Information System Security

Operators and Expressions

CS-201 Introduction to Programming with Java

ESc101 : Fundamental of Computing

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Transcription:

Java Programming Lecture 10 Alice E. Fischer February 21 2012 Alice E. Fischer () Java Programming - L10... 1/19 February 21 2012 1 / 19

Outline 1 Encryption 2 Wrapper Classes 3 Unicode and the Character Class Alice E. Fischer () Java Programming - L10... 2/19 February 21 2012 2 / 19

Encryption Encryption Cryptographic Hashes Using SHA-1 Example: Password.java Alice E. Fischer () Java Programming - L10... 3/19 February 21 2012 3 / 19

Encryption Cryptography in Computing Cryptography has found important applications in today s business world: Confidential data storage and communication. Digital signatures that cannot be repudiated. Authentication: prove your identity by supplying a secret password. Digital fingerprints: show that a file has not been modified. The first two applications require a crypto system that can be decoded. The last two applications can be done with one-way encryption. Alice E. Fischer () Java Programming - L10... 4/19 February 21 2012 4 / 19

Encryption Cryptographic Hashes A Cryptographic hash function performs one-way cryptography: the hashed value cannot be decrypted. It is used to extract a random-looking fingerprint from a body of data (small or large). The fingerprint is now called a digest. The encryption is repeatable but not reversible. It does not necessarily retain all of the information in the original text. Two different texts could hash to the same digest. However, given a digest., it is extremely difficult, probably impossible, to find a text that hashes to that digest. Used for encrypting passwords. Used for identifying whether a file has been modified surreptitiously. Alice E. Fischer () Java Programming - L10... 5/19 February 21 2012 5 / 19

Encryption SHA-1 is a Cryptographic Hash Function SHA-1 calculates a 20-byte (160 bit) message digest. Published in 1995 by the National Security Agency. SHA-1 is widely used and is employed in several common security applications and protocols. In 2008, one researcher found a way to produce a hash collision with only 2 51 operations. This is not the same as breaking the code or finding a message that hashes to a particular digest. Although there is no known way to crack the code in practice, more secure (longer) versions have been / are being invented. All SHA algorithms work by iterating a process that interchanges blocks of code, rotates the bits within a block, and applies (to some blocks) a nonlinear function that changes on every iteration. Alice E. Fischer () Java Programming - L10... 6/19 February 21 2012 6 / 19

Encryption Using SHA-1 in Java Example: Password.java This is the code from the computehash( String st) function: import java.security.* Create an object that does the encryption: MessageDigest d = MessageDigest.getInstance("SHA-1"); catch (NoSuchAlgorithmException e) {...} Convert your text into a byte[] using function in String class: st.getbytes() Store your byte array into the encryption-object: d.update(st.getbytes()); Encrypt it and return the result as a byte array: return d.digest(); Alice E. Fischer () Java Programming - L10... 7/19 February 21 2012 7 / 19

Encryption Calling computehash() System.out.print("Enter a password : " ); sc.next() computehash( sc.next() ) String( computehash( sc.next() ) ) inputhash = new String(computeHash( sc.next() )); Alice E. Fischer () Java Programming - L10... 8/19 February 21 2012 8 / 19

Wrapper Classes Wrapper Classes Purpose in Java Integer and Double Character Alice E. Fischer () Java Programming - L10... 9/19 February 21 2012 9 / 19

Wrapper Classes A Java Design Problem These basic Java facts are almost contradictory: 1 All Java code is and must be inside some class. 2 A modern language must provide the functions in the standard C libraries (math, ctype). 3 A modern language must support pre-programmed collections of both primitive values and objects. 4 Java strictly divides the world of primitive objects from the world of dynamic objects. You cannot create a primitive variable in dynamic memory. All class objects are in dynamic memory. #1 and #2 conflict, as do #3 and #4. Alice E. Fischer () Java Programming - L10... 10/19 February 21 2012 10 / 19

Wrapper Classes Purpose of Wrappers in Java The wrapper classes in Java solve the problem. A wrapper class:... gives us an easy way to turn a primitive into an object with the same meaning that can be dynamically allocated and put into a collection.... gives us a place to put the related standard library methods and methods that convert values to and from the wrapper type.... gives us a place to put information about the implementation of the primitive type on the local system. In C, this information is in a header file such as limits.h. Every primitive type has a matching wrapper class: Integer, Long, Short, Byte, Character, Boolean, Float, Double. Alice E. Fischer () Java Programming - L10... 11/19 February 21 2012 11 / 19

Wrapper Classes Information about Integers The Integer class contains static data members that describe the type int: static int MAX_VALUE an int can have, 2 31 1. static int MIN_VALUE an int can have, 2 31. A constant holding the maximum value A constant holding the minimum value static int SIZE The number of bits used to represent an int value in two s complement binary form (32). Every other wrapper class contains the same static members, with values appropriate for each different type. Alice E. Fischer () Java Programming - L10... 12/19 February 21 2012 12 / 19

Integer Wrapper Classes An Integer object has exactly one non-static data member, which is an int. Significant methods include: static int signum(int k) : Return -1, 0, or +1, according to the sign of k. Returns the signum function of the specified int value. static int parseint(string s) signed decimal integer. String tostring() Convert the String as into a Convert the value of an Integer to a String. int intvalue() Return the value of this Integer as an int. Similarly, bytevalue(), doublevalue(), floatvalue(), longvalue() etc. Alice E. Fischer () Java Programming - L10... 13/19 February 21 2012 13 / 19

Integer Methods Wrapper Classes An Integer object has exactly one data member, which is an int. Significant methods include: static int signum(int k) : Return -1, 0, or +1, according to the sign of k. Returns the signum function of the specified int value. static int parseint(string s) signed decimal integer. String tostring() Convert the String as into a Convert the value of an Integer to a String. int intvalue() Return the value of this Integer as an int. Similarly, bytevalue(), doublevalue(), floatvalue(), longvalue() etc. Alice E. Fischer () Java Programming - L10... 14/19 February 21 2012 14 / 19

Wrapper Classes Information about Doubles static double MAX_VALUE A constant holding the largest positive finite value of type double, (2 2 52 )2 1023. static double MIN_VALUE A constant holding the smallest positive nonzero value of type double, 2 1074. static double NaN A bit pattern representing an error resulting from floating-point underflow. static double NEGATIVE_INFINITY A negative value representing an error from floating-point overflow. static double POSITIVE_INFINITY A posiive value representing an error from floating-point overflow. static int SIZE The number of bits used to represent a double value. Alice E. Fischer () Java Programming - L10... 15/19 February 21 2012 15 / 19

Double Methods Wrapper Classes In addition to conversion methods like those in the Integer class, Double supports these methods: static boolean isnan(double v) (NaN) value, false otherwise. static boolean isinfinite(v) overflowed the limits of type double. True if v is a Not-a-Number True if the magnitude of v has boolean equals(object obj) Compares this object against obj. Every wrapper class implements an equals() method. int compareto(double anotherdouble) Compares two Double objects numerically; returns negative, zero, or positive. Every wrapper class implements an compaareto() method. Alice E. Fischer () Java Programming - L10... 16/19 February 21 2012 16 / 19

Unicode and the Character Class Unicode Java character displays are based on Unicode. http://www.pccl.demon.co.uk/java/unicode.html American Java compilers typically use UTF-8 character code UTF-8 (8-bit UCS/Unicode Transformation Format) is a variable-length character encoding for Unicode. It is able to represent any character in the Unicode standard, yet is backwards compatible with ASCII. For these reasons, it is steadily becoming the preferred encoding for e-mail, web pages, and other places where characters are stored or streamed. UTF-8 encodes each character (code point) in 1 to 4 octets (8-bit bytes), with the single octet encoding used only for the 128 US-ASCII characters. Alice E. Fischer () Java Programming - L10... 17/19 February 21 2012 17 / 19

Character Class Unicode and the Character Class Implements Interfaces: Comparable, Serializable. Replaces the ctype library in C. Constants: static char MAX_VALUE The constant value of this field is the largest value of type char, \uffff. static char MIN_VALUE The constant value of this field is the smallest value of type char, \u0000. Constructor: Character(char value) char charvalue() Returns the value of this Character object. getnumericvalue(char ch) Returns the int value that the specified Unicode character represents. int compareto(character anothercharacter) Compares two Character objects numerically. Alice E. Fischer () Java Programming - L10... 18/19 February 21 2012 18 / 19

Unicode and the Character Class Character Methods Character has a large number of constants related to international character codes plus the functions from C s ctype library. static boolean islowercase(char ch) static boolean isuppercase(char ch) True if ch is an upper case letter, false otherwise. static boolean tolowercase(char ch) static boolean touppercase(char ch) Returns the upper-case equivalent of ch; returns ch if it is not a lower case letter. static boolean iswhitespace(char ch) static boolean isletterordigit(char ch) static boolean isletter(char ch) static boolean isdigit(char ch) Alice E. Fischer () Java Programming - L10... 19/19 February 21 2012 19 / 19