Transcript Slide 1

Java Assignment Related
Message digest

MessageDigest class


getInstance(“SHA”)
to feed in



update(byte [])
update(byte)
digest(); digest(byte [])

Returned as byte array
Digesting stream

Example


DigestInputStream in = new DigestInputStream( new
FileInputStream(args[0]), md);
Producing byte array

DataOutputStream to ByteOutputStream



ByteArrayOutputStream byteOut = new
ByteArrayOutputStream();
DataOutputStream dataOut = new
DataOutputStream(byteOut);
You can do


dataOut.writeLong(l)
byteOut.toByteArray()
Key, KeyGenerator


Key: top-level interface for all keys. defines the functionality of all key objects.
KeyGenerator:

This class provides the functionality of a (symmetric) key
generator.

static KeyGenerator getInstance(String algorithm)


void init(SecureRandom random)


Generates a KeyGenerator object for the specified algorithm.
Initializes this key generator.
SecretKey generateKey()

Generates a secret key.
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(new SecureRandom());
SecretKey key = kg.generateKey();
KeyPairGenerator

Examples:


KeyPairGenerator kpg = KeyPairGenerator.getInstance("ElGamal")
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
kpg.initialize(1024);
KeyPair pair = kpg.genKeyPair();
public PublicKey getPublic() - This method returns the public key.
public PrivateKey getPrivate() - This method returns the private key.
Cipher,

For encryptio/decryption


static Cipher getInstance(String transformation)


Assymtric and symmentric key systems
Generates a Cipher object that implements the specified
transformation. You can use “DES/ECB/PKCS5Padding”
void init(int opmode, Key key)





Initializes this cipher with a key.
public
public
public
public
static final int ENCRYPT_MODE
static final int DECRYPT_MODE
final byte[] update (byte [])
final byte[] doFinal()
CipherOutputstream
CipherInputstream

CipherOutputStream(OutputStream os, Cipher c)


void write(byte[] b)


Constructs a CipherInputStream from an InputStream and a Cipher.
int read(byte[] b)


Writes b.length bytes from the specified byte array to this output stream.
CipherInputStream(InputStream is, Cipher c)


Constructs a CipherOutputStream from an OutputStream and a Cipher.
Reads up to b.length bytes of data from this input stream into an array of
bytes.
void close()

Closes this output/input stream and releases any system resources
associated with this stream.
Big Integer

BeigInteger(1, messagebytes)


x/y mod z


BigInteger.valueOf(1)
x.multiply(y.modPow(kOne.negate(), z)).mod(z)
k relatively prime to z

k.gcd(z).equals(kOne) == true
Java Security API packages







Java.security
Java.security.cert
Java.security.interfaces
Java.security.spec
Javax.crypto
Javax.crypto.interfaces
Javax.crypto.spec
Some Abbreviations
 JCA – java cryptographic architecture
 JCE – java cryptographic extensions
 Access Control
Class or Interface
Description
java.security.cert.Certificate
A cryptographic certificate
javax.crypto.Cipher
A cipher
java.security.Key , java.security.PrivateKey ,
java.security.PublicKey , javax.crypto.SecretKey
A key, used for signing or encryption
javax.crypto.KeyAgreement
A secret key exchange protocol
java.security.KeyFactory
Translates public and private keys from one format
to another
javax.crypto.KeyGenerator
Creates keys for symmetric ciphers
java.security.KeyPairGenerator
Creates pairs of public and private keys for signing
or encryption
javax.crypto.Mac
A Message Authentication Code (MAC)
java.security.MessageDigest
A cryptographic hash function
javax.crypto.SecretKeyFactory
Translates secret keys from one format to another
java.security.SecureRandom
A cryptographically strong random number engine
java.security.Signature
A digital signature
Factory Methods

JCA extensively use factory methods


MessageDigest md5; md5 =
MessageDigest.getInstance("MD5");
Overloaded version accepts

Algorithm & Provider
KeyPairGenerator kpg =
KeyPairGenerator("ElGamal", "Jonathan");
Cipher, keyGenerator, KeyPairGenerator,
MessageDigest, Signature