Using C, C++, Java, or Python 1. Introduction: In this assignment, you will impl
ID: 3801194 • Letter: U
Question
Using C, C++, Java, or Python
1. Introduction:
In this assignment, you will implement secure communications between two parties, Alice and Bob. For simplicity, the sending of a message from the sender to the receiver will be simulated through writing the message into a file by the sender and reading the message from the file by the receiver. This assignment is designed to practice key distribution, encryption/decryption, and integrity protection with secret key cryptography and public key cryptography.
2. Task Description:
Communication scenario: Alice (as a Client) needs to send messages to Bob (as a Server). Alice and Bob each have a pair of <public key, private key> 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; /** * @author Joe Prasanna Kumar * This program provides the following cryptographic functionalities * 1. Encryption using DES * 2. Decryption using DES * * The following modes of DES encryption are supported by SUNJce provider * 1. ECB (Electronic code Book) - Every plaintext block is encrypted separately * 2. CBC (Cipher Block Chaining) - Every plaintext block is XORed with the previous ciphertext block * 3. PCBC (Propogating Cipher Block Chaining) - * 4. CFB (Cipher Feedback Mode) - The previous ciphertext block is encrypted and this enciphered block is XORed with the plaintext block to produce the corresponding ciphertext block * 5. OFB (Output Feedback Mode) - * * High Level Algorithm : * 1. Generate a DES key * 2. Create the Cipher (Specify the Mode and Padding) * 3. To Encrypt : Initialize the Cipher for Encryption * 4. To Decrypt : Initialize the Cipher for Decryption * * Need for Padding : * Block ciphers operates on data blocks on fixed size n. * Since the data to be encrypted might not always be a multiple of n, the remainder of the bits are padded. * PKCS#5 Padding is what will be used in this program * */ 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); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.