Introduction

Note: If you don't have Netbeans installed on your workstation, please install it from the link above. You don't have administrator rights so install it to the folder where you are able to write. E.g., C:\PV181\Netbeans.

Basic JCA architecture:
Overview

Architecture

Providers

There are multiple default providers installed.


Engine classes


SecureRandom

SecureRandom

MessageDigest

Cipher

Cipher

Signature

MAC

MAC

Generator vs. KeyFactory

KeyFactory

KeyFactory

SecretKeyFactory

KeyPairGenerator

KeyPairGenerator

KeyGenerator

KeyAgreement

KeyAgreement

KeyStore


Strong cryptography


1. SecureRandom


2. MessageDigest

Filename SHA-256 checksum MD5 checksum
file_a.bin 230cb8e5f966c9d4618040fee7e010f8350794d0029df32c40fe8796d872bf29 e64db39c582fe33b35df742e8c23bd55
file_b.bin c1627b1968253cbc8595b1b4c951f949acbd1d6001ae366e108c20cfbb5232f3 3bf834b2853fbbace062cfe1f93f3776
file_c.bin aeedd172bcbc5c16a161844b689a465b96739a554d85b96138423aefec701a18 bec261a2d2a8921cb4cf78cc87c3d565
file_d.bin 73cf8ba20aa05ba3c81387669e9c4b300742cfc5297569157712b4d6e2658638 79f2807a930062c358ecb65a484bd4d1

Hint: You may use Globals.bytesToHex(buffer, false); to encode byte[] byte array to a hex-coded string.
Hint 2: You may use InputStream to read from URL directly: InputStream is01 = new URL("http://www.fi.muni.cz/~xklinec/java/file_a.bin").openStream();
Hint 3: Boiler plate code for InputStream processing is here.
Hint 4: Getting different hashes? Pay attention to URL and count number of bytes already hashed vs. file size.

3. AES Encryption

Question: How many bits does IV have? How many bits does key have?
IV KEY Ciphertext
AAAAAAAAAAAAAAAAAAAAAA== AAAAAAAAAAAAAAAAAAAAAA== 6VMSY9xFduwNsiyn8mGZdLG6/NXb3ziw81MBSfaKozs=
FiikDkkW+k+oW2biRnC1zQ== eUaq9at/s29swOs5EEWv8Q== vDoRZgpnJ2/yCnW7ogatKoBlR3XBsViSz5Dfj2ExLl8=
tPIljLHaDSa8vXwrnDZiCg== 0y4bBloL0Ppbuy3o8AK6Vw== N2HNL2GCfEahFJ+9ieUuKzns4zp10nsWqN3SKN5s0x1uOn2BNn1s7bkqbQuTSYLFf/ow3kUQL7kk6HRWTOf/aGmvkD61udKlN70Eby1Zgik=

Hint1: You may use javax.xml.bind.DatatypeConverter class for Base64 encoding & decoding. Java 10+ may need JVM param: --add-modules java.xml.bind
Hint2: To construct AES encryption key you may use: new SecretKeySpec(key0, "AES");
Hint2: To construct IV you may use: new IvParameterSpec(iv0);

4. Signature verification

Two signatures are swapped, find them and verify them.

You will need a certificate: PEM encoded X509 certificate and DER encoded X509 certificate.
Note: Signature files are base64 encoded. You have to perform base64 decoding before signature verification.

Hint 1: You will need to construct X509Certificate object. Here is how.
Hint 2: You may use InputStrem to read from URL directly: InputStream is01 = new URL("http://www.fi.muni.cz/~xklinec/java/file_a.bin").openStream();
Hint 3: In order to convert a stream to a byte array you may use the following snippet.
Hint 4: This snippet was used to generate a signature. By changing it you can produce a signature verifier.

Homework assignment #1


Protocol Buffers