Question

Hello, please help me with the following question, below is the code for the tutorial provided fo...

Hello, please help me with the following question, below is the code for the tutorial

provided for us is the following tutorial and the code in java
http://tutorials.jenkov.com/java-cryptography/index.html

https://docs.google.com/document/d/1_xZ09sbNjy9BCjgQhZrrRop4UrtXmUk2yKwSghIe1Cs/edit?fbclid=IwAR3ATn39Jm9odLYtvvndhDR6NQTZGb9yiiSpAamDwTwj5Hyk0B-cVraMqOw

, thank you for your help in advance,

Create a Java program based on the Java Cryptography API to do the following:

  1. Accept a user password from the user
  2. Accept a file name from the user
  3. Encrypt/Decrypt the file using symmetric key techniques with CBC
  4. The key should be based on the password. Ever one should come up with their own way to create a key from the password.
  5. The IV should be based on the password as well
1 0
Add a comment Improve this question Transcribed image text
Answer #1

please find the java program written below :

--------------------------------------------------------------------------------------------------------

import java.util.*;
import java.io.*;

import java.security.*;

//importing security packages

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

public class test {

    static final String ALGORITHM = "AES";             //defining that we will use AES algorithm
    static final String TRANSFORMATION = "AES";

    public static void main (String[] args) {
        InputReader in = new InputReader(System.in);      //creating input reader
        PrintWriter w = new PrintWriter(System.out);     //creating writer

        System.out.println("please enter your name : ");        //taking username as input
        String name = in.nextLine();

        System.out.println("Please enter the input file name : ");
        File inputFile = new File(in.nextLine());

        System.out.println("Enter the password for secret key");       //taking password input for key
        //using make key method for using password as key with some manupulation
        String key = makeKey(in.nextLine());

        File encryptedFile = new File("encrypted.txt");
        File decryptedFile = new File("decrypted.txt");


        encryptWithAES(key, inputFile, encryptedFile);

        decryptWithAES(key, encryptedFile, decryptedFile);


        w.close();
    }
    //defining encrypt method
    public static void encryptWithAES(String key, File inputFile, File outputFile) {

        doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
    }

    //defining decrypt method
    public static void decryptWithAES(String key, File inputFile, File outputFile) {
        doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
    }
    //making key from given password
    public static String makeKey(String pass) {
        while (pass.length() < 16) {
            pass += pass;
        }
        return pass.substring(0, 16);

    }
    //function for encryption and decryption
    public static void doCrypto(int cipherMode, String key, File inputFile, File outputFile) {
        try {
            //taking secret key and algorithm to be used
            Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(cipherMode, secretKey);

            //getting input file stream
            FileInputStream inputStream = new FileInputStream(inputFile);
            byte[] inputBytes = new byte[(int) inputFile.length()];
            inputStream.read(inputBytes);

            //creating output byte stream
            byte[] outputBytes = cipher.doFinal(inputBytes);

            //creating output file from output byte stream
            FileOutputStream outputStream = new FileOutputStream(outputFile);
            outputStream.write(outputBytes);

            inputStream.close();
            outputStream.close();

        }
        //throwing exception if error occurs
        catch (NoSuchPaddingException | NoSuchAlgorithmException
                    | InvalidKeyException | BadPaddingException
                    | IllegalBlockSizeException | IOException ex) {
            System.out.println("Error encrypting/decrypting file: " + ex);
        }
    }

}

-------------------------------------------------------------------------------------------------------------------

please find the screenshots of code, its execution along with different input and output files :

Cvavatestjava-Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Praject Help 1544r java static final strinCuavaitestjava- Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Praject Preferences Help 1544r java statSearch Tocls yo.txt-Search Results in java Home Shae Search Search Results in java vyo.tt yu.lxt Date medified: 16-03-2019 21Search Tocls enc-Search Results in java Home Shae Search Search Results in java encrypted.Lxt Quick access Desitop Date medifSearch Tocls de Search Results in java Home Shae Search > search Results in Java ˇ File Edit Fomat w Help DecodeClienljava hi

Add a comment
Know the answer?
Add Answer to:
Hello, please help me with the following question, below is the code for the tutorial provided fo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded...

    In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded based on the efficiency of your implementation. You will not be awarded any points if you use simple nested loops to implement the below tasks. You should use one or more of the below data structures: - ArrayList : - JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html - Tutorial: http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html Question You are provided with...

  • Java code. Need help with either 1. or 2. Nothing too complicated and the code needs...

    Java code. Need help with either 1. or 2. Nothing too complicated and the code needs at least one loop and 3 switch statements! Artificial Intelligence elements in programming. Creating stories with user input. For this assignment, you can do one of two things 1. Create a chat box that allows a conversation to go on as long as the user wants . Welcome the user when they start. . look for key words or other things that could guide...

  • Please help me with this Java problem. Would greatly appreciate comments in the code as well:...

    Please help me with this Java problem. Would greatly appreciate comments in the code as well: Define a class called Counter whose internal "count" variable is a basic integer counter. This class track that count from its instantiation in the constructor (can be set to any positive integer, or 0). The count should never be allowed to be negative. Include methods that will set the counter to 0, increase the count by 1, and decrease the count by 1. Include...

  • I need help with my javascript project, I've started on it but I can't seem to...

    I need help with my javascript project, I've started on it but I can't seem to get ym java scripts to work with my index.html. Can someone please help me fix my code? I need the main.js & scratchpad.js to work with my index.html .Thank you Directions: Go to the following link: https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics On that page, scroll down to "A 'hello world' example". Follow the step-by-step instructions to download their code and edit it in your text editor of choice....

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Chapter 06 Applied Cryptography 1. How is integrity provided? A. Using two-way hash functions and digital...

    Chapter 06 Applied Cryptography 1. How is integrity provided? A. Using two-way hash functions and digital signatures B. Using one-way hash functions and digital signatures C. By applying a digital certificate D. By using asymmetric encryption 2. Which term refers to the matching of a user to an account through previously shared credentials? A. Nonrepudiation B. Digital signing C. Authentication D. Obfuscation 3. Which term refers to an arranged group of algorithms? A. Crypto modules B. Cryptographic service providers (CSPs)...

  • Please help with writing this code. The language is C++. I'm using vim. Thanks Problem Statement...

    Please help with writing this code. The language is C++. I'm using vim. Thanks Problem Statement A good password has many requirements. Humans have a hard time meeting these requirements left to their own devices. You are tasked with creating a program that will generate a password based on what the user wants in the password The user should be able to choose if they want a password with: letters upper case "lower case *numbers The user should also provide...

  • JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information...

    JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information being exposed, it is becoming more and more important to find ways to protect our sensitive data. In this program we will develop a simple tool that helps users generate strong passwords, encrypt and decrypt data using some cyphering techniques. You will need to create two classes. The first class is your driver for the application and contains the main method. Name this class...

  • C++ Code

    "For two thousand years, codemakers have fought to preserve secrets while codebreakers have tried their best to reveal them." - taken from Code Book, The Evolution of Secrecy from Mary, Queen of Scots to Quantum Cryptography by Simon Singh.The idea for this machine problem came from this book.You will encrypt and decrypt some messages using a simplified version of a code in the book. The convention in cryptography is to write the plain text in lower case letters and the encrypted text in upper case...

  • Java Netbeans code Option 1: Authentication System For security-minded professionals, it is important that only the...

    Java Netbeans code Option 1: Authentication System For security-minded professionals, it is important that only the appropriate people gain access to data in a computer system. This is called authentication. Once users gain entry, it is also important that they only see data related to their role in a computer system. This is called authorization. For the zoo, you will develop an authentication system that manages both authentication and authorization. You have been given a credentials file that contains credential...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT