Download presentation
Presentation is loading. Please wait.
Published byEzra Quinn Modified over 9 years ago
1
Li Tak Sing COMPS311F
2
XML Schemas XML Schema is a more powerful alternative to DTD to describe XML document structures. The XML Schema language is also referred to as XML Schema Definition (XSD). You have seen the DTD for the employee list.
3
XML schemas for employee-list
4
XML schemas for employee-list
5
The root element of an XML Schema is just schema. It must be specified with the namespace of "http://www.w3.org/2001/XMLSchema". By convention, we use the prefix of xs or xsd though technically we could choose other prefixes, for example a meaningless prefix abc. The XML Schema will still be correct if we change all occurrences of xs to abc.
6
Referring to an XSD file You have learned that the inclusion of the following DOCTYPE declaration in an XML document will validate it against employee- list.dtd. For validation of an XML document against an XSD, you will remove the DOCTYPE declaration and modify the start tag of the root element with two added attributes as follows. The xmlns:xsi attribute indicates that this XML document should be validated against an XML Schema. The xsi:noNamespaceSchemaLocation attribute specifies the file name of the schema and its location. Since no path is specified, the schema file is assumed to be in the same directory as the XML file being validated. As can be seen below: The namespace http://www.w3.org/2001/XMLSchema-instance is now bound to xsi which is the standard prefix for XML Schema Instance.
7
Simple elements in XSD A simple XML element is one that does not contain any other elements. Its content can be one of the few dozens of built-in data types. The more popular ones are: string, decimal, integer, boolean, date and time. The name element is a simple element. For example: This makes use of the optional attributes minOccurs and maxOccurs to specify the minimum and maximum number of occurrences in its parent (enclosing) element. Since both attributes are set to one, the name element must occur exactly once in the enclosing employee element.
8
Value restrictions by range The hours element is defined with the restriction element available from XML Schema. The base type decimal is restricted to the minimum value of 0 and maximum value of 60 inclusively. This lower bound of zero is obvious because it is impossible to work negative number of hours. The upper bound of sixty could be due to the company policy or labour regulations.
9
Value restrictions by range The XSD specification is vastly better that the earlier DTD specification which does not consider non-numeric values in hours as errors.
10
Value restrictions by enumeration There are other forms of value restrictions. Enumeration allows you to restrict an element to a list of possible values. The fruit element is defined below to have one of three possible values: Apple, Banana and Orange.
11
Value restrictions by pattern More advanced restrictions are specified with the pattern element. The direction element is restricted to one of the directions: north, south, east and west respectively denoted by their first characters n, s, e and w. Even the base type is specified as a string, the direction element can only have one character because there is only one pair of square brackets in the pattern. The square brackets specify the four allowed values: n, s, e and w.
12
Value restrictions by pattern
13
Value restrictions by pattern The following definition allows 2-character strings. The first character is a lower case or upper case character while the second character is a decimal digit. Values allowed include A1, a1, B3 but not BB.
14
Suppose we want to allow strings like r2d2, H5N1, H1N1 and c9L3k4. The strings can hold non-zero repetitions of the 2-character string pattern that we defined above. We could put the original pattern in brackets and add a trailing + for non-zero repetitions as follows. If we also allow empty strings, we will replace + with *.
15
Whitespace processing restrictions A whitespace character is one of line feed, tab, space and carriage return. Inside a restriction element, you can have a whiteSpace element. The attribute value of preserve is for not changing any whitespace characters, replace for replacing whitespace characters with space characters, and collapse for replacing consecutive whitespace characters with a single space character. The following is an example.
16
Restrictions on string length You can restrict the length of a string to a fixed number. In this example, a password must have 8 characters.
17
Restrictions on string length You can also restrict the length of a string to a range. In the following example, a password must have at least 6 characters and a maximum of 10 characters.
18
Complex elements in XSD An XML element is complex if it contains attributes or other elements.
19
Sequence We may have a student element that contains the elements of firstname and lastname as follows: Peter Wong
20
It could be defined as a sequence in a complex type.
21
Note that if the two child elements appear in the XML document in a different order, for example lastname before firstname as follows, the validation will fail. In other words, ordering is crucial in sequences. Wong Peter
22
Attributes If an element can have attributes, there must be attribute elements defined within its complexType attribute. Suppose we have a student element. Its XML Schema definition would look like this.
23
Text with attributes In XML Schema, we can use the extension element to define attributes for any simpleType or complexType element. Consider the following element to express shoe sizes. 9 Its XSD definition would look like this.
24
However shoe sizes are not standardized across countries. For example, the UK shoe size 8 is slightly larger than the US shoe size 8. In a shoe catalogue, it may be necessary to indicate the country for which the size number applies. 9 We can define the XML Schema with the extension element to hold the integer representing the shoe size. Within the extension element, we have the attribute element for the country. To keep our schema simple, we chose not to represent half size.
26
Texts mixed with other elements In its simplest form, an element may contain just texts as follows. Your buy order of 400 shares of HSBC has been executed on 2009-05-04. Perhaps certain parts of the texts have special meaning. You can turn special parts into elements to facilitate processing. The following element sms has been enhanced by making the quantity of shares, stock name and date into elements. Your buy order of 400 shares of HSBC has been executed on 2009-05-04.
27
You can define the corresponding complexType element by setting its mixed attribute to true.
28
Unspecific order in a complex type Suppose we don’t care if the first name or the last name appears first in a student element. Peter Wong Wong Peter
29
In that case, in place of the sequence element, we can use the all element to accept any order for firstname and lastname.
30
Choice in a complex type Suppose you only want to contact customers by their email addresses or phone numbers but not both. The following are the contact information for two different customers. johndoe@coolmail.com 709394
31
The two alternative customer elements are expressed with the choice element as follows.
32
Named types for reuse Suppose you want to have an element to capture someone’s favourite fruit, as follows: Apple This can also be enforced as follows which also allows you to specific which fruit, here it would be banana and orange.
33
If we have elements other than myfruit that make use of this enumeration of Apple, Banana and Orange, we can define a type called fruitType. This will result in a more readable XML Schema without the risk of different usages of the enumeration to get out of step when for example Coconut is added to the existing fruits. Changing the original element definition to use a named type is trivial. First, you add the type attribute to the element with the new type name.
34
Second, you add the name attribute to the simpleType or complexType.
35
However, where can you place this named type definition in the XSD file? You can put it right below its first use. Alternatively, you can group all the named type definitions together and place them right after xs:schema’s start tag or just before its end tag. Good use of named types can make an XSD file more readable and maintainable.
36
elementFormDefault and attributeFormDefault In the sample XML Schemas we have presented so far, some names have prefixes and some don’t. You are probably confused when you should use a prefix. In the schema element, you can use two attributes to control this. Attribute elementFormDefault controls whether prefixes are required for element names. Likewise attributeFormDefault controls whether prefixes are required for attribute names. We use the value qualified for prefixes required and the value unqualified for prefixes not required. The sample code that we have been using has prefixes for elements but not for attributes as indicated below.
38
The default values of both are unqualified. Therefore removing attributeFormDefault as follows will have the same meaning.
39
In case the two attributes are set to qualified, we can still avoid the use of prefixes with a default namespace. Consider the following XML schema saved in the file employee.xsd.
41
The following XML document can be successfully validated against it. Oliver Au oau@ouhk.edu.hk 2009-09-01
42
The xsi:schemaLocation attribute specifies two URI references separated by white space. The first value http://www.ouhk.edu.hk/employeeNS here is a namespace. The second value employee.xsd gives a hint to the location of the schema document. If we had specified elementFormDefault attribute as qualified.
43
The validation will fail. The XML document must be modified as follows. Note the required prefixes added to the name, email and hireDate elements. Oliver Au oau@ouhk.edu.hk 2009-09-01
44
XPath XPath is a simple language that allows you to write expressions to refer to different parts of an XML document. We will learn XSLT shortly which enables us to transform XML documents to other XML documents, HTML documents or text. But XSLT uses XPath expressions which are what we need to learn first. Our XPath examples will be based on the XML document below.
45
16.00 Coffee mug 25.00 Beer glass
46
Nodes Everything in an XML document is an XPath node. The most used nodes are element nodes, for example the catalogue, product, price and description above. XPath borrows its terminology from family trees. The catalogue node is the parent of the product node. The price node and description node are siblings which share the same ancestors of product and catalogue. The terms children and descendants have the obvious meaning. The table below summarizes the different XPath expressions we refer to above.
47
XPath expressions XPath expressionMeaning /The beginning slash stands for the root node which is the document element. /catalogIt stands for the catalog element. /catalog/productIt stands for the product element which is the child node of the catalog element. productNote that there is no beginning slash in this expression. That means we are not starting from the root node but from the current node. This expression is only meaningful if the current node has a child node called product. The expression is meaningful when catalog is our current node. This expression refers to its child product element..A single dot stands for the current node whatever that may be...The double dot stands for the parent node of the current node. /catalog/product/@idThis expression uses the @ symbol to refer to the id attribute.
48
XPath If you have used command prompts on Windows or Unix, path expressions are not new to you. In an XSLT specification, you use XPath expressions to refer to specific parts of the XML document. This allows you to perform specific transformations selectively.
49
Predicates Predicates are used to filter the nodes that match an XPath expression. A predicate is enclosed in a pair of square brackets placed after an XPath expression. Numeric predicates are predicates that evaluate to integers. For example, we use the following expression to refer to the first product child of catalog which is the product with id mug. /catalog/product[1] Likewise, we use the next expression to refer to the second product element with id glass. /catalog/product[2]
50
Predicates A predicate can also evaluate to a Boolean value. The following expression matches all the product elements priced less than fifteen dollars. /catalog/product [price < 15] There are many operators and functions to help you build XPath predicates. For example, the following predicate makes use of the last( ) function. The expression refers to the second last product element. /catalog/product[last( ) -1].
51
Axes XPath provides 13 axes to help you select a set of nodes in relation to the current node, for example, descendant, following, ancestor, preceding, parent, self, attribute, etc. Axes are used with double colon before node names. Many uses of axes can be substituted with other mechanisms that you have learned. For example, the following two expressions are equivalent. The first expression makes use of the attribute axis. /catalog/product/attribute::id /catalog/product/@id
52
Extensible Style Language Transformation (XSLT) XSLT is itself defined in XML syntax. An XSLT file takes the input from an XML file and transforms it into another file which could be XML, HTML or text. XSLT is more like a declarative language such as SQL than a conventional procedural language such as C. A good thing about XSLT is that all you need to run it is a Web browser. It is supported by all major Web browsers in the market including Mozilla Firefox, Microsoft Internet Explorer, Google Chrome, Opera, and Safari.
53
XSLT Namespace Keep in mind that XSLT files need to have the following root element. This must be closed by a matching close tag at the end of the file as follows.
54
Referring to an XSLT file The following is an XML document named cdcatalog.xml adapted from http://www.w3schools.com/xsl/. Its second processing instruction refers to an XSLT file called cdcatalogue.xslt stored in the same directory. When you open cdcatalog.xml from a Web browser, the transformations in cdcatalog.xslt will be invoked automatically. Both xsl and xslt are legitimate extensions for XSLT files. By default, Liquid XML Studio uses xslt.
55
Referring to an XSLT file The Freewheelin' Bob Dylan USA Columbia 10.90 1963
56
One night only Bee Gees UK Polydor 10.90 1998
57
Maggie May Rod Stewart UK Pickwick 8.50 1990
58
Romanza Andrea Bocelli EU Polydor 10.80 1996
59
Here is our first version of the cdcatalog.xslt which contains a template. Templates are the basic building blocks of XSLT. We will build this file incrementally until it has the desired functionalities. Hello
60
The XSLT file has one xsl:template. From now on, we may simply call it template. It has the match attribute with the XPath expression value cd. By default, the current node is the root element catalog. It has four child elements of cd. There are four CDs in the XML file to match this template four times. Therefore Hello appears four times in the output as you can see in the screen shot below.
61
Even if you have multiple templates defined in an XSLT file, only the top-level template with the match attribute closest to the root will be executed automatically. My CD Collection Title
62
Opening the XML document referring to this XSLT gives the following result.
63
If a template is applied, the string inside the template will appear in the output. The two templates try to produce My CD Collection and Title. But we can only see the first string in the output. Clearly, the second template with match="/catalog/cd/title" is never executed. If we want to execute templates for nodes at various levels, we should start from the root and call xsl:apply-templates explicitly to apply the templates for the lower-level nodes. Consider our next XSLT stylesheet. Keep in mind that it does not matter what order you write the template rules.
64
My CD Collection
65
Title:
66
Artist:
67
The XSLT stylesheet has four templates. Each template has a match attribute which decides the kind of nodes that it will handle. The template with match value / handles the root element. This template produces a few HTML tags including a level-2 heading My CD Collection. The root template is the only one that will be applied automatically. Other templates must be manually applied with xsl:apply- template tags. For example, our root template invokes xsl:apply-templates for catalog/cd. The current node is / and the select value is "catalog/cd". Concatenating the two, we have an XPath expression "/catalog/cd". Do we have nodes matching this XPath expression in the document? Yes. Do we have a template defined that can match this XPath expression? Yes, again. The invocation succeeds. If at least one of the answers to the two questions is negative, the invocation will not happen.
68
The second template matches "catalog/cd". It invokes xsl:apply-templates for title and artist. The result of the two calls are enclosed in a pair of paragraph tags and. The current node is "/catalog/cd". The first apply-templates has select value "title". Appending the value to the current node, we have an XPath expression "/catalog/cd/title". The nodes in the XML document that match this XPath expression will be used for the execution of the third template in the XSLT file.
69
The apply-templates for "artist" yields the XPath expression "/catalog/cd/artist". This XPath expression will also match the template with select value "artist". Note that the select value of just "artist" works as well as a more detailed XPath expression "/catalog/cd/artist". The minor difference between the short and the long expressions is that the later requires node artist to be a child of /catalog/cd. Since we do not have other nodes with child node artist, the two expressions make no difference to us.
70
The last two templates in our XSLT stylesheet contain the following. In case you don’t remember is the line break tag in HTML. Title: Artist: The HTML code can be seen on a Web browser as follows in the screen shot below. The two occurrences of the tag return the title and the singer of the CD depending on the current node in the template.
72
In our construction of the XSLT stylesheet, we define an automatically applied template for the root node. In that template, we call apply-templates to navigate down the child nodes and output HTML or XML code. The select attribute in apply-templates, the match attribute in templates and the nodes in the XML document being processed must agree.
73
without select attribute In the previous example, we selectively applied templates to title and artist. However, if we omit the select attribute, the Web browser will try to match all child nodes of the current node to a template as can be seen below.
74
My CD Collection
75
without select attribute Title:
76
Artist: Country: Company:
77
Year: Price:
78
Pay attention to the second xsl:template with match attribute value "catalog/cd". It contains an xsl:apply-templates element without the select attribute. With no select attributes, the xsl:apply-templates attempts to find an appropriate template for its nodes. This XSLT stylesheet gives the following output as can be seen in the screen shot below.
80
However, suppose we are only interested in displaying CDs that cost over 10 dollars. In that case we can modify the second template for "catalog/cd" as follows.
82
Security For Java applications, there are two main areas of security issues: 1 system security 2 information security.
83
System security System security refers to the safety and stability of the computing environment. The safety and stability can be breached in a number of ways. When a malicious application (such as a virus) executes itself, it can cause damage to the system — for example, by deleting some critical files and rendering the computer inoperable.
84
Information security Or, the malicious application can intentionally or unintentionally consume too many resources, such as computing time, disk space, or network bandwidth, thereby causing the system to perform improperly.
85
Information security Information security, however, refers to the secrecy and integrity of data. For example, when you send an email, how do you ensure that only the targeted recipients can read the message? When you receive an email, how do you ensure that the message has not been tampered with and that it is from the supposed sender?
86
System security In the security policy model, resources can be granted or denied different types of access independently. For example, a file can be a resource, and the read action can be differentiated from the write action. So, you can easily grant read-only access to a particular file. You can do the same with objects, allowing you to create security policies for runtime objects as well.
87
System security Java makes things even more interesting by allowing different policies to apply to different applications, or to different invocations of the same application.
88
Security policy As you know by now, applets are small applications that can be embedded in webpages. When Java was first introduced, applets were sensational because they provided a cross- platform solution for making a webpage more interesting. To safeguard users from malicious applets, applets are run in a sandbox, which imposes rather stringent restrictions on what the applets can do.
89
Applet security If you run an applet through a browser and then the applet tries to read a local file, an error message would appear. You can try the following link: http://plbpc001.ouhk.edu.hk/~mt311f/2005- mt311f/lecture/test/build/classes/test.html
90
Applet security The applet in the file tries to read a local file "c:/test.dat" and then display it.
91
Code of the applet public class ReadFile extends javax.swing.JApplet { private String st=""; public void init() { try { java.io.FileReader reader=new java.io.FileReader("c:/test.dat"); char c[]=new char[1000];
92
Code of the applet while (true) { int no=reader.read(c); if (no<0) { break; } for (int i=0;i<no;i++) {
93
Code of the applet st+=c[i]; } catch (Exception e) { st=e.toString();
94
Code of the applet } javax.swing.JTextArea area=new javax.swing.JTextArea(4,40); this.getContentPane().add(area); area.setText(st); }
95
Allowing an applet to access a local file To remove the restriction, we need to specify a different policy. The format of the policy file is quite simple, and you can create one using a text editor. For example, below is a simple file that grants rights for applets from plbpc001.ouhk.edu.hk ‘.java.policy’:
96
Changing the policy grant codeBase "http://plbpc001.ouhk.edu.hk/-" { permission java.security.AllPermission; }; You should put this file in the home directory. In MS Windows, this should be the parent directory of "My Documents" As this would grant permission to applets from the host to do everything, you should remove this file after testing it.
97
Cryptography Information security is save guarded by cryptography.
98
Cryptography Cryptography has four main objectives: Confidentiality — the information cannot be understood by anyone for whom it was not intended. Integrity — the information cannot be altered in storage or transit between sender and intended receiver without the alteration being detected.
99
Cryptography Non-repudiation — the creator/sender of the information cannot deny at a later stage his or her intentions in the creation or transmission of the information. Authentication — the sender and receiver can confirm each other’s identity and the origin/destination of the information.
100
Secret Key method In the secret key method, the sender and the receiver share the same secret key. Then, the sender first encrypts the message with the key and sends the encrypted message to the receiver who decrypts the message with the same key.
101
Secret key message encrypted message encrypted message key send over an unsafe channel
102
Java secret key APIs Java’s cryptographic APIs are defined in the java.security and javax.crypto packages. For a basic encryption, we need a secret key and a cryptographic algorithm. The Java classes for those are: SecretKey — this class encapsulates the secret key for use in encryption and decryption Cipher — this class provides cryptographic APIs for encryption and decryption.
103
Creating a secret key The following code would create a secret key: // the key itself as a byte array byte[] key = new byte[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; // create a KeySpec specifically for DES for our key DESKeySpec spec = new DESKeySpec(key); // retrieve a DES SecretKeyFactory SecretKeyFactory factory = SecretKeyFactory.getInstance("DES"); // generate the actual SecretKey object SecretKey secret = factory.generateSecret(spec);
104
Creating a cipher To create a DES cipher, you first need to find a DES algorithm provider using Cipher.getInstance: Cipher c = Cipher.getInstance("DES"); The same Cipher object can be used for either encryption or decryption, depending on how you initialize the object: Encryption: c.init(Cipher.ENCRYPT_MODE, secret); Decryption c.init(Cipher.DECRYPT_MODE, secret);
105
To encrypt or decrypt a message Now, with Cipher object, you can encrypt or decrypt any bytes easily with: byte[] c.update(byte[] buf); You can invoke update as many times as necessary to encrypt or decrypt the entire message. To retrieve the encrypted or decrypted result, you invoke the doFinal method: byte[] inEncrypted = c.doFinal(); or byte[] inEncrypted=.cdoFinal(byte[] buf); The result is the combination of all the output of update() and doFinal().
106
Overall algorithm To create a secret key To create a cipher Invoke the update method continuously until the end of data Invoke the doFinal method to get the final result.
107
Conversion to or from byte[] You should notice that all the cryptography APIs work on byte[]. So no method what is the format of your original message, you must convert it to byte[].
108
Conversion to or from byte[] To convert a String to byte[], you can use the following method of String: public byte[] getBytes() To convert a byte[] to a String, you can use the following constructor of String: public String(byte[] bytes)
109
Encryption The following method would encrypt a message: static byte[] encrypt(String st, Cipher c) { return c.doFinal(st.getBytes()); }
110
Decryption The following method would decrypt a message: public static String decrypt(byte[] message, Cipher c) { byte[] result=c.doFinal(message); return new String(result); }
111
Message digest The above method would only be able to protect the confidentiality of the message. It cannot protect the integrity of the message because the receiver would not know whether the message has been altered in any way. To protect the integrity, we need the message digest.
112
Hash function A message digest, typically of fixed length, is generated using a special mathematical transformation called a hash function. A hash function is basically a transformation that takes any arbitrary input and produces an output in a finite space.
113
Hash function For message digests, we need a hash function that has two properties: It must be extremely difficult to produce the same message digest from two different messages, i.e. the hash must be one-to- one. It must be extremely difficult to produce the original message from a given message digest, i.e. the hash must be irreversible.
114
Message digest Message digest alone cannot be used to protect the integrity of message. This is because anyone can use the same hash function to protect a message digest of an altered message. So the secret key method must be used together with the message digest.
115
Secret key method with message digest message message digest encrypted message sent over unsafe channel compare produce key produce message digest
116
Message digest The creation of a MessageDigest is similar to that of Cipher, using getInstance: MessageDigest md = MessageDigest.getInstance("MD5"); Once you have a MessageDigest object, you can feed data to it using the update method: md.update(inbuf); Finally, you can retrieve the final hash using the digest method: md.digest();
117
Message digest The following method generate the message digest of a String: public static byte[] md(String st) { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(st.getBytes()); return md.digest(); }
118
Compare two byte[] Since message digests are in the format of byte[], we need to compare two byte[]'s. you should use a for loop to compare two byte[]'s.
119
Message Authentication Code (MAC) As we have mentioned earlier, we cannot use the message digest alone to guarantee the integrity of a message. We need to add a secret key protection so that the resulting string would also depends on the secret key. Such an encrypted message digest is called a message authentication code(MAC). MAC protects the authentication and integrity of a message.
120
MAC When a user want to send a message, it would use the secret key to produce a MAC of the message. Then he/she sends the message and the MAC to the receiver. The receiver would then use the same secret key and the message to generate another MAC. The two MACs would then be compared. If they are the same, then the message is really from the supposed sender and the message has not been messed with by others.
121
message MAC keyed hash message MAC send to the recipient keyed hash compare
122
MAC The followings are the steps to create an MAC of a message: create a secret key create an Mac object initialize the Mac object with the key update the Mac object with the contents of the message get the MAC from the Mac object.
123
Create a secret key KeyGenerator kg = KeyGenerator.getInstance("HmacMD5"); SecretKey sk = kg.generateKey(); Note that the key is randomly generated. So different key would be generated if the generateKey is invoked for many times. So if you want to share this key, you need to save the key to a file and then share the file.
124
Create an Mac object Mac mac = Mac.getInstance("HmacMD5");
125
Initialize the Mac object with the key mac.init(sk);
126
update the Mac object with the contents of the message mac.update(buf); where buf is an array of bytes. You can call this method as many times as you wish.
127
get the MAC from the Mac object. byte[] result=mac.doFinal();
128
The MD5 program KeyGenerator kg = KeyGenerator.getInstance("HmacMD5"); SecretKey sk = kg.generateKey(); Mac mac = Mac.getInstance("HmacMD5"); mac.init(sk); mac.update(buf); byte[] result=mac.doFinal();
129
Public key method In the secret key method, there is a problem in distributing the key because both the sender and recipient need to have the same key. It is possible that the key is intercepted when it is transmitted from one person to another.
130
Public key method The public key method, you need a pair of keys to perform the encryption and decryption process. The two keys are called public key and private key. You cannot deduce the private key from the public key. When a message is encrypted with the public key, it must be decrypted with the private key. When a message is decrypted with the private key, it must be decrypted with the public key.
131
Public key methods A user first generates a key pair that consists of a public key and private key. He/she now informs others about the public key. Then anybody can now encrypt a message with the public key and send the encrypted message to the user. Now, the user can decrypt the message with the private key.
132
Public key methods Note that the public key does not have to be send over a secured channel because any one who knows the public key would still not be able to decrypt any message that has been encrypted with the public key. The method protects the confidentiality of the message.
133
Java classes for the public key methods Most java classes for the public key methods are in the package java.security. KeyPairGenerator the static method static public KeyPairGenerator getInstance(String) would return a KeyPairGenerator with the specified method. The most popular method is RSA. So the following statement get a RSA public key generator:
134
original message encrypted with public key encrypted message encrypted message original message decrypted with private key original message encrypted with private key encrypted message encrypted message original message decrypted with public key
135
Java classes for the public key methods KeyPairGenerator generator=KeyPairGenerator("RSA"); The following method of KeyPairGenerator would initial the KeyPairGenerator to generate a key of the specified byte length: public void initialize(int length) For example, the following statement would initialize a KeyPairGenerator to generate a key of size 2048 bits long: generator.initialize(2048);
136
Java classes for the public key methods The following statement create a key pair: KeyPair key=generator.generateKeyPair(); The following statement finds the public key: PublicKey publicKey=key.getPublic(); The following statement finds the private key: Privatekey privatekey=key.getPrivate();
137
Java classes for the public key methods After obtaining the key pair, we can use Cipher to encode or decode a message like what we have done in the secret key algorithm. The following statement initializes a Cipher to be used to encrypt a message using the private key: Cipher cipher=Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, key.getPrivate());
138
Java classes for the public key methods The following statement initializes a Cipher to be used to encrypt a message using the public key: Cipher cipher=Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
139
Java classes for the public key methods The following statement initializes a Cipher to be used to decrypt a message using the private key: Cipher cipher=Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, key.getPivate());
140
Java classes for the public key methods The following statement initializes a Cipher to be used to decrypt a message using the public key: Cipher cipher=Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, key.getPublic());
141
Java classes for the public key methods Then, we can use the update and doFinal method Cipher to encrypt or decrypt the message. If the message is short, we can simply use the doFinal straight away to decrypt the message.
142
Digital signature When you receive a message from a person, how do you be sure that the message is really from that person? This can be done by using digital signature.
143
Digital signature A digital signature is like a MAC but the key used is the private key of the key pair. To produce a digital signature, we first create a message digest and then encrypt the message digest with the private key of the message. We would say that we sign the message with the private key.
144
Digital signature Then, the sender would send the message together with the digital signature. The receiver would then first use the public key of the sender to decrypt the digital signature and then get back the message digest. The receiver then calculate another message digest from the message.
145
Digital signature If the two digital digests match, then we can sure of two things: integrity. That is the message has not been changed by another person. authentication. The message is really from the supposed sender because only he/she has the private key.
146
message digest hash digital signature encrypt with private key message digital signature send to the recipient message digest hash message digest decrypt with public key compare
147
Java classes to create a digital signature First we create a key pair first like what we did for the public key method. However, we would specify that we want to use the DSA algorithm which is a digital signature algorithm: KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
148
Java classes to create a digital signature Then, we initialize the key as before: kpg.initialize(1024); Then, we can use this key pair generator to generate a key pair: KeyPair keyPair=kpg.generateKeyPair();
149
Java classes to create a digital signature We can then get the public key and private key of the key pair: PublicKey publicKey=keyPair.getPublic(); PrivateKey privateKey=keyPair.getPrivate(); You should then save the two keys to a files and send the public key to your recipients.
150
Java classes to create a digital signature Now, assume that we want to create a digital signature of a message. Signature sig=Signature.getInstance("DSA"); sig.initSign(privateKey); Then, you can use the update method of Signature to check the contents of the message: sig.update(message);
151
Java classes to create a digital signature The digital signature is generated by invoked by the method sign: sig.sign(); This method returns an array of bytes which is the digital signature of the message. To verify a signature, we need the following statements: Signature sig=Signature.getInstance("DSA"); sig.initVerify(publicKey);
152
Java classes to create a digital signature Then, we use the update method of signature to put in the content of the message just like what we did when we generated the digitial message. sig.update(message);
153
Java classes to create a digital signature Then, we can check whether the message has been correctly signed by invoking the method verify of Signature: sig.verify(); This method would return true or false depending on whether the message has been correctly signed or not.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.