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