Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Cryptography: code in c++ or java Communication scenario: Alice (as a Client) ne

ID: 3810150 • Letter: C

Question

Cryptography: code in c++ or java

Communication scenario: Alice (as a Client) needs to send messages to Bob (as a Server). Alice and Bob each have a pair of under the RSA cryptosystem (their key pairs are different), and they know each other’s public key beforehand (the public keys can be hard coded into the program or read from a file).

Step 1: Set up a shared secret key: Alice and Bob set up a shared secret key using the following method: Alice generates a random key k, encrypts it using Bob’s public key with the RSA algorithm, and sends the ciphertext to Bob. Bob receives the ciphertext and then decrypts it to get the key k.

Step 2: Message encryption and decryption: Alice sends a 25-byte message to Bob. This message is encrypted using AES with the key k distributed in Step 1. Bob decrypts the message using his copy of key k.

Step 3: HMAC-based Authentication: Alice sends a 30-byte message to Bob. This message is authenticated with an HMAC generated with key k (distributed in Step 1) using SHA-256 as the underlying hash algorithm. Bob verifies the HMAC to see if the message is from Alice and if it has been modified during transit.

Step 4: Digital Signature-based authentication: Alice sends a 40-byte message to Bob. This message is authenticated with a RSA signature computed over the hash of the message with the SHA-256 hash algorithm. Bob verifies the RSA signature to see if the message is from Alice and if it has been modified during transit.

To evaluate Step 1, your program needs to print the k at Alice and the k decrypted by Bob to see if they are the same. To evaluate Step 2, your program needs to print the message at Alice and the message decrypted by Bob to see if they are the same. To evaluate Step 3, your program needs to print the HMAC computed by Alice and the HMAC computed by Bob to see if they are the same. To evaluate Step 4, the server’s (Bob’s) program needs to print the result of RSA signature verification operation over the received signature to see if it is the same as the message generated by Alice.

Explanation / Answer

package org.owasp.crypto; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.Cipher; import java.security.NoSuchAlgorithmException; import java.security.InvalidKeyException; import java.security.InvalidAlgorithmParameterException; import javax.crypto.NoSuchPaddingException; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import sun.misc.BASE64Encoder; public class DES { public static void main(String[] args) { String strDataToEncrypt = new String(); String strCipherText = new String(); String strDecryptedText = new String(); try{ /** * Step 1. Generate a DES key using KeyGenerator * */ KeyGenerator keyGen = KeyGenerator.getInstance("DES"); SecretKey secretKey = keyGen.generateKey(); /** * Step2. Create a Cipher by specifying the following parameters * a. Algorithm name - here it is DES * b. Mode - here it is CBC * c. Padding - PKCS5Padding */ Cipher desCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); /* Must specify the mode explicitly as most JCE providers default to ECB mode!! */ /** * Step 3. Initialize the Cipher for Encryption */ desCipher.init(Cipher.ENCRYPT_MODE,secretKey); /** * Step 4. Encrypt the Data * 1. Declare / Initialize the Data. Here the data is of type String * 2. Convert the Input Text to Bytes * 3. Encrypt the bytes using doFinal method */ strDataToEncrypt = "Hello World of Encryption using DES "; byte[] byteDataToEncrypt = strDataToEncrypt.getBytes(); byte[] byteCipherText = desCipher.doFinal(byteDataToEncrypt); strCipherText = new BASE64Encoder().encode(byteCipherText); System.out.println("Cipher Text generated using DES with CBC mode and PKCS5 Padding is " +strCipherText); /** * Step 5. Decrypt the Data * 1. Initialize the Cipher for Decryption * 2. Decrypt the cipher bytes using doFinal method */ desCipher.init(Cipher.DECRYPT_MODE,secretKey,desCipher.getParameters()); //desCipher.init(Cipher.DECRYPT_MODE,secretKey); byte[] byteDecryptedText = desCipher.doFinal(byteCipherText); strDecryptedText = new String(byteDecryptedText); System.out.println(" Decrypted Text message is " +strDecryptedText); } catch (NoSuchAlgorithmException noSuchAlgo) { System.out.println(" No Such Algorithm exists " + noSuchAlgo); } catch (NoSuchPaddingException noSuchPad) { System.out.println(" No Such Padding exists " + noSuchPad); } catch (InvalidKeyException invalidKey) { System.out.println(" Invalid Key " + invalidKey); } catch (BadPaddingException badPadding) { System.out.println(" Bad Padding " + badPadding); } catch (IllegalBlockSizeException illegalBlockSize) { System.out.println(" Illegal Block Size " + illegalBlockSize); } catch (InvalidAlgorithmParameterException invalidParam) { System.out.println(" Invalid Parameter " + invalidParam); } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote