Anda di halaman 1dari 11

Tutorial 4 Cryptography Algorithms

Tutorial 4 Cryptography Algorithms in


JAVA
Objectives
Getting started with Java
Basic Java Programming
Java Cryptography Extension
sing Java to encrypt and decrypt text
!les
Exercises
Tutorial for CS 5285 Information Security for E-commerce 1
Tutorial 4 Cryptography Algorithms
" Getting started with Java
1.1 Something aout !a"a
A programming Language developed by Sun
Microsystems
Object Oriented, platform-independent, interpreted,
multithreading
A ava source program is compiled to ava byte-code!
A ava virtual machine sitting on each platform,
interpreting ava byte-code to local machine
instructions and e"ecuting them
#$o %inds of ava program&
'1( java application
)un on local machine, no need to embed it into
*#ML
*ave full access to all machine resources 'dis%+(
',( java program
-t is application on the -nternet
.ou must embed it into *#ML pages to run it!
Secure, no access outside virtual machine
Tutorial for CS 5285 Information Security for E-commerce ,
Tutorial 4 Cryptography Algorithms
1.2 #a$or steps for running $a"a program
/dit source program!
.ou can use 0ote1ad or other /ditor program to $rite
your source program
compile your java program to byte code'e!g! your java
program is program!java(
$a"ac program!java
run your program 'application or applet(
a( Application
$a"a program
( Applet
2irst, you should $rite a *#ML file 'e!g! program!html(
to invo%e this java apple, then you can use applet"ie%er
program.html or use -/, 0etscape to open this *#ML file!
Alternatively, you may use commercially available
visual environment such as builder&
http&33$$$!borland!com3jbuilder3
Tutorial for CS 5285 Information Security for E-commerce 4
ava
Source
5ode
ava
5ompiler
ava
6yte
5ode
ava
7irtual
Machine
0ative
machine
-nstruction
Tutorial 4 Cryptography Algorithms
/"amples
One simple java application
import java!io!89
public class *ello:itty ;
public static void main'String args<=( ;
System!out!println'>*ello:itty?(9
@
@
One simple java applet
import java!applet!89
import java!a$t!89
public class *ello:itty, e"tends Applet ;
public void paint'Araphics g( ;
g!dra$String'B*ello :ittyB,1C,1C(9
@
@
$rite a html file to embed this applet
D*#MLE
D*/AFE
D#-#L/EA *#ML to test appletD3#-#L/E
D3*/AFE
D6OF.E
Dapplet codeGB*ello:itty,!classB HidthGBICB *eightGB1CCBE
D3appletE
D36OF.E
D3*#MLE
Tutorial for CS 5285 Information Security for E-commerce J
Tutorial 4 Cryptography Algorithms
# Basic Java Programming
6ecause this course is not a program designing course, $e
donKt teach the details of ava programming! .ou can chec%
follo$ing L)L to get useful materials!
Javaso$t % &un' http'((www)*avaso$t)com %
www)sun)com
+eveloper)com' http'((*ava)developer)com
+ownload the tutorial'
http'((we,#)*ava)sun)com(docs(,oo-s(tutorial(in$orma
tion(download)html

2urthermore, you can find a lot of boo%s on java in the
library!
.ou should grasp a lot of basic %no$ledge about java
programming, such as variables, constants, Arithmetic,
)elational, Shift, Logical and 5onditional operators,
5ontrol statements 'including selection statements,
repetition statements and /"ception *andling Statements(
, and some advanced techniMues in java 'such as AH#,
multi-thread, soc%et(!
Tutorial for CS 5285 Information Security for E-commerce N
Tutorial 4 Cryptography Algorithms
. Java Cryptography Extension
#he standard ava Fevelopment :it comes $ith a security
frame$or% called the !a"a Cryptography Architecture
'5A(!
#o encrypt or decrypt data, you must use the !a"a
Cryptography E&tension '5/( or a third-party encryption
library! 5/ has been integrated into the ava
#M
, SF:,
Standard /dition, v 1!J, or you can do$nload the 5/ from
Sun at http&33java!sun!com3products3jce3inde"!html
#he ava Security A1- is a set of pac%ages that are used for
$riting secure programs in ava! -n particular, the classes
and interfaces in the follo$ing pac%ages are part of the
Security A1-&
java!security!:ey
java!security!1rivate:ey
java!security!1ublic:ey
java"!crypto!Secret:ey
Lse to encrypt and sign
messages
java!crypto!5ipher 5ipher
java!security!MessageFigest Message digest function
java!security!Signature Figital signature
java!security!cert!5ertificate Authentication
java!security!:ey2actory
java"!crypto!:eyAgreement
java"!crypto!:eyAenerator
java"!crypto!Secret:ey2actory
Symmetric :eys and
Asymmetric :eys
management
java!security!Secure)andom
Secure random number
generator
ava"!crypto!Mac Message Authentication 5ode
Tutorial for CS 5285 Information Security for E-commerce O
Tutorial 4 Cryptography Algorithms
4 sing Java to encrypt and decrypt text
!les
J!1 An application to encrypt te"t files
import java!io!89
import java"!crypto!89
import java"!crypto!spec!89
public class /ncrypt2ile ;
public static void main'String args<=( ;
if 'args!length D 1( ;
System!out!println'BLsage& java /ncrypt2ile Dfile nameEB(9
System!e"it'-1(9
@
try ;
2ile des2ile G ne$ 2ile'Bencrypt!desB(9
2ile-nputStream fis9
2ileOutputStream fos9
5ipher-nputStream cis9
33 5reation of Secret %ey
byte %ey<= G Babcd/2A*B!get6ytes'(9
Secret:eySpec secret:ey G ne$ Secret:eySpec'%ey,BF/SB(9
33 5reation of 5ipher objects
5ipher encrypt G
5ipher!get-nstance'BF/S3/5631:5SN1addingB(9
encrypt!init'5ipher!/05).1#PMOF/, secret:ey(9
33 Open the 1lainte"t file
Tutorial for CS 5285 Information Security for E-commerce Q
Tutorial 4 Cryptography Algorithms
try ;
fis G ne$ 2ile-nputStream'args<C=(9
@ catch'-O/"ception err( ;
System!out!println'B5annot open file+B(9
System!e"it'-1(9
@
cis G ne$ 5ipher-nputStream'fis, encrypt(9

33 Hrite to the /ncrypted file
fos G ne$ 2ileOutputStream'des2ile(9
byte<= b G ne$ byte<I=9
int i G cis!read'b(9
$hile 'i +G -1( ;
fos!$rite'b, C, i(9
i G cis!read'b(9
@
fos!flush'(9
fos!close'(9
cis!close'(9
fis!close'(9
@ catch'/"ception e(;
e!printStac%#race'(9
@
@
@
J!, An application to decrypt te"t files
import java!io!89
import java"!crypto!89
import java"!crypto!spec!89
public class Fecrypt2ile ;
public static void main'String args<=( ;
Tutorial for CS 5285 Information Security for E-commerce I
Tutorial 4 Cryptography Algorithms
try ;
2ile des2ile G ne$ 2ile'Bencrypt!desB(9
2ile des2ile6is G ne$ 2ile'Bdecrypt!desB(9
2ile-nputStream fis9
2ileOutputStream fos9
5ipher-nputStream cis9
33 5reation of Secret %ey
byte %ey<= G Babcd/2A*B!get6ytes'(9
Secret:eySpec secret:ey G ne$ Secret:eySpec'%ey,BF/SB(9
33 5reation of 5ipher objects
5ipher decrypt G
5ipher!get-nstance'BF/S3/5631:5SN1addingB(9
decrypt!init'5ipher!F/5).1#PMOF/, secret:ey(9
33 Open the /ncrypted file
fis G ne$ 2ile-nputStream'des2ile(9
cis G ne$ 5ipher-nputStream'fis, decrypt(9
33 Hrite to the Fecrypted file
fos G ne$ 2ileOutputStream'des2ile6is(9
byte<= b G ne$ byte<I=9
int i G cis!read'b(9
$hile 'i +G -1( ;
fos!$rite'b, C, i(9
i G cis!read'b(9
@
fos!flush'(9
fos!close'(9
cis!close'(9
fis!close'(9
@ catch'/"ception e(;
e!printStac%#race'(9
Tutorial for CS 5285 Information Security for E-commerce R
Tutorial 4 Cryptography Algorithms
@
@
@
J!4 An application to retrieve $eb pages
import java!net!89
import java!io!89
public class Lrl)etriever ;
public static void main'String<= args( ;
if 'args!length+G1( ;
System!out!println'BLsage& Lrl)etriever DL)LEB(9
System!e"it'-1(9
@
try ;
L)L urlGne$ L)L'args<C=(9
6uffered-nputStream bufferGne$
6uffered-nputStream'url!openStream'((9
Fata-nputStream inG ne$ Fata-nputStream'buffer(9
String line9
$hile ''lineGin!readLine'((+Gnull(
System!out!println'line(9
in!close'(9
@ catch'MalformedL)L/"ception mue( ;
System!out!println'args<C=SBis an invalid L)L&BSmue(9
@ catch'-O/"ception ioe( ;
System!out!println'B-O/"ception& BSioe(9
@
@
@
Tutorial for CS 5285 Information Security for E-commerce 1C
Tutorial 4 Cryptography Algorithms
/ Exercises
)un the programs above
5hec% if you can supply a %ey as user inputT
Hhat other encryption algorithms you may useT And #ry
them!

Hrite a java program to retrieve the *#ML file at L)L


http&33$$$!cs!cityu!edu!h%3Udeng3 , encrypt the contents
and store it into a local file >deng!enc?, then decrypt the
file >deng!enc? and store it into a local file >deng!dec?!
#ry to encrypt your emails sent to your friends!

Tutorial for CS 5285 Information Security for E-commerce 11

Anda mungkin juga menyukai