In this age encryption is very important as a means to keep

In this age encryption is very important as a means to keep data secure. We rely on encryption for everything from keeping our phone data safe to securely making transactions via the Internet. One very simple use of encryption is to keep the contents of files private /hidden in case they may accidentally fall into the \"wrong hands\". For example, you may want to encrypt the files on your laptop or flash drive in case you lose them or they are stolen from you. In this assignment you will implement a ivially simple encryption algorithm on text files using Java. The algorithm and the way you are implementing it would never be actually used, but it will give you some idea of how encryption is done and why it needs to be done well. You will likely study encryption in more detail in some other courses. This assignment will have two parts to it: Part One: You will implement a new Java class, called Crypto, in file Crypto java, which has the following requirements/specifications: Idea: The Crypto class will enable a user to encrypt or decrypt a Java text file utilizing a simple shift cipher (or Caesar Cipher). This simple (and ineffective) method of encryption considers 1- byte characters by their integer values, from 0-255. In a shift cipher, the key (value used to transform from the original data to the encrypted data and back) is a value from 1 to 255, which indicates how far to \"shift\" a character\'s value from its original position. For example, the ASCII character \'A\' has the numeric value 65, with the rest of the capital letters following in order. If the key were 4, then each \'A\' in the original file would transform to \'E\'; each B\' would transform to F and so on. Note that this process must be circular- so the character with value 255 would transform to the character with value (255 + 4) % 256-3. See how this shifting can be done in Java in handout ShiftDemo.java. The key for your Crypto class will be generated from a password phrase (String) that is entered when the object is created. Clearly, to do this you will need some way of transforming a String into an integer between 1 and 255 Since you are encrypting files, you will need a way to decrypt the files correctly, but only if the correct password is used for the decryption. This leads to an interesting problem- you must somehow include a clue as to how to decrypt a file within the file itself, to enable its decryption, without giving away the entire process to those who should not be able to decrypt the file. Clearly you cannot store the password that was used to generate the key within the file, as that way anyone could decrypt the file. You can solve this problem (again, in a greatly simplified manner) by storing a hash of the password used to encrypt the file at the beginning of the encrypted file. A hash is a mapping of the password into a number. Here is a brief explanation about how hashes work in this situation During encryption: 1) Assume password P has been used to generate key K that is used to encrypt regular file F into encrypted file E 2) Value H(P) is a hash of password P 3) H(P) is placed at the beginning of file E, followed by the encrypted file data

Solution

package assignment;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class Crypto {
   Cipher desCipher;
   SecretKey Key;
   KeyGenerator keygenerator;
   String fileName;
   String updatedfile= \"E:\\\\NewText.txt\";
   FileOutputStream out;
   byte[] text;
String s ;
   public Crypto(String fileName2) {
       this.fileName = fileName2;
   }
   public void encrpyt(){

               try{
                   keygenerator = KeyGenerator.getInstance(\"DES\");
                  SecretKey Key= keygenerator.generateKey();
                  BufferedReader br = null;
               desCipher = Cipher.getInstance(\"DES\");
             

           // This will reference one line at a time
           String line = null;

          
           // FileReader reads text files in the default encoding.
           FileReader fileReader = new FileReader(fileName);

           // Always wrap FileReader in BufferedReader.
           BufferedReader bufferedReader =
           new BufferedReader(fileReader);

           while((line = bufferedReader.readLine()) != null) {
           System.out.println(line);
           text =line.getBytes(\"UTF8\");
           desCipher.init(Cipher.ENCRYPT_MODE, Key);
                           byte[] textEncrypted = desCipher.doFinal(text);
                       s = new String(textEncrypted);
               System.out.println(s);
                           System.out.println(textEncrypted);
                           out = new FileOutputStream(updatedfile); // writing to a file
                                   out.write(textEncrypted);
                                   out.close();
                      
           }   

           // Always close files.
           bufferedReader.close();   
           }
           catch(FileNotFoundException ex) {
           System.out.println( \"Unable to open file \'\" +fileName+ \"\'\");
           }
           catch(IOException ex) {
           System.out.println(\"Error reading file \'\" + fileName + \"\'\");
          
           }
               catch(Exception e)
           {
       System.out.println(\"Exception\");
           }
          
          
   }  
          
              
              
          

  
  
   public void decrpyt(){
try{
   FileReader fileReader = new FileReader(updatedfile);
   String line = null;
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);

while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
   desCipher.init(Cipher.DECRYPT_MODE, Key);
//   byte[] textEncrypted = line;
       //   byte[] textDecrypted = desCipher.doFinal(textEncrypted);

//   s = new String(textDecrypted);
   System.out.println(s);
   }
}catch(Exception e)
       {
       System.out.println(\"Exception\");
       }
      
               }

}

****************************************

package assignment;

import java.util.Scanner;

public class fileManagement {
   public static void main(String[] args) {
      
Scanner scanner = new Scanner(System.in);
       String fileName = scanner.nextLine();
       Crypto fileEncryption = new Crypto(fileName );
       fileEncryption.encrpyt();
       fileEncryption.decrpyt();
   }
}

 In this age encryption is very important as a means to keep data secure. We rely on encryption for everything from keeping our phone data safe to securely maki
 In this age encryption is very important as a means to keep data secure. We rely on encryption for everything from keeping our phone data safe to securely maki
 In this age encryption is very important as a means to keep data secure. We rely on encryption for everything from keeping our phone data safe to securely maki

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site