Question

Hello, please help me with the follpowing question in java, thank you in advance

Hello,

please help me with the follpowing question in java, thank you in advance

0 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 follpowing question in java, thank you in advance
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
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