SlideShare a Scribd company logo
encryptFile. java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class encryptFile {
// The crypt() function encrypts or decrypts a byte array input, using
// an 16-byte initialization vector (init), 16-byte password (pass),
// and an integer mode (either Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE)
public static byte[] crypt(byte[] input, byte[] init, byte[] pass, int mode) {
// TODO - Fill this out.
}
// The cryptFile() function opens a file at a specified string path,
// then passes in the init, pass, and mode values to the crypt() function
// to either encrypt or decrypt the contents of the file. It then writes
// the encrypted or decrypted data back to the file. Note that it should
// overwrite the existing file - so don't try it on a file that's actually
// worth anything!
public static void cryptFile(String path, byte[] init, byte[] pass, int mode) {
// TODO - Fill this out.
}
// The menu() function provides a user interface for the script. It should
// prompt the user to enter a file path, 16-byte initialization vector,
// 16-byte password, and a mode (encrypt or decrypt). If the password or
// initialization vector are too short or too long, the function should
// re-prompt the user to re-enter the value.
public static void menu() {
// TODO - Fill this out.
}
// Just need to call the menu() function here
public static void main(String[] args) {
// Tests for crypt();
byte[] plain = "Hello World".getBytes();
byte[] pass = "aaaabbbbccccdddd".getBytes();
byte[] init = "gggggggggggggggg".getBytes();
byte[] cipher = crypt(plain, init, pass, Cipher.ENCRYPT_MODE);
byte[] decrypted = crypt(cipher, init, pass, Cipher.DECRYPT_MODE);
// This should print "Hello World"
System.out.println(new String(decrypted));
// Uncomment below to test menu section once complete
//menu();
}
}
than ideal - you generally want to use standardized implementations of cryptographic functions,
as it's much less likely that they'll have internal defects or flaws that pose a security risk. For this
lab, we'll use the javax.crypto libraries in Java. These libraries provide access to a number of
cryptographically-related functions, including the ability to encrypt in AES and DES. A skeleton
file has been provided called encryptFile.java. The crypt() function The first function we'll
complete is the crypt() function. This function takes three byte arrays: an input, which is either a
ciphertext or plaintext, a init, which is a 16 -byte initialization vector (for Chaining Block Mode),
and a pass, which is a 16 -byte key. It also takes a mode, which is an integer. The Cipher module
includes the modes declared as final static integers: Cipher.ENCRYPT_MODE
Cipher.DECRYPT_MODE Inside the function, the first thing we need to do is pass the
initialization vector and the password to the IvParameterSpec and SecretKeySpec objects
respectively. These objects prepare the IV and password for use, and check that they are
appropriate. They must each by 16 bytes in length, no longer and no shorter - this usually equals
16 ASClI characters. The IV byte array can be passed directly into the constructor of a new
IvParameterSpec object: new IvParameterSpec(init); However, when creating a SecretKeySpec
object, you must also specify (in a String) the type of cipher that you are using. In our case, we'll
use AES, and pass in our password byte array object: new SecretKeySpec(pass, "AES"); We can
then create our Cipher object. Rather than use new like we do for most objects, we instead make
a call to the Cipher.getInstance() function, passing in a String that defines several aspects of our
cipher: - The cipher to be used (i.e. AES) - The mode of operation (i.e. CBC (chaining block
cipher) or ECB (electronic code book) - The padding to be used (i.e. PKCS5 standard for
padding) This will create a new Cipher object, which we can then use. Here's an example of what
the getInstance() constructor should look like:
Cipher.getInstance("AES/CBC/PKCSSPADDING"); Then, we need to call the init method on
our new Cipher object. The init method takes three inputs: the integer mode, the SecretKeySpec
object we created, and the IvParameterSpec that we created. Finally, we can perform our
encryption/decryption. We'll take the input byte array and pass it to the dofinal() function of our
initialized Cipher object. This will return a new byte array, which is our output. If we are
performing encryption, the output will be our ciphertext; if we are performing decryption, the
output will be our plaintext. You should then return this bytearray. Note that the above will
require a try/except or a throws declaration around the Cipher-related lines. The cryptFile()
function Now we have a function that can encrypt or decrypt byte arrays at will, so let's put it to
use. The cryptFile() function takes a String path for a file, plus the bytearrays init and pass, and
the integer mode. Inside of the function, we first need to check our file path and create a Path
object. We can do this by just calling the Paths.get() function on our String path; this will return
a new Path object. We can then get a bytearray of the file contents using the Files module, which
has a function Files.readAllBytes(path). Pass in your Path object that you created. This will
return a bytearray of the file contents. Pass the file contents as the input parameter to the crypt
function, along with the init, pass, and mode parameters. This will return the bytearray output
that is either a ciphertext or a plaintext (depending on if you are encrypting or decrypting).
Finally, write the bytearray back to the original file using the Files.write() function. This takes
the Path object and the bytearray as arguments: Files.write(file, output); This function will work
with any type of file, including both plaintext and binary files. You will notice that binary files,
like images, no longer work properly when encrypted. This is because the file header and related
information is encrypted as well, and your computer will be unable to parse what the file is
supposed to be. In order to make it readable again, you just need to decrypt the file using the
same IV and password. You should verify this works by trying to encrypt a text file and an
image. The menu() function Finally, let's put an interface on the script. This is fairly
straightforward and just requires a couple Scanner calls to get user input, along with a couple
loops to catch incorrect-length IV's and passwords. I'll leave this section up to you, but your code
should: Create a new crypt() function (name of your choice) that implements an alternate cipher
available in the Cipher object, and add a prompt to your menu() that allows the user to select
between AES and the alternate cipher. When you are done, submit your finished Java file on the
course Moodle Page. - Prompt the user to enter a filepath - Prompt for an initialization vector,
and re-prompt if the user provides an IV that is not exactly 16 bytes in length (use a loop that
checks the length() of the input bytearray). - Do the same as above, but for the password (make
sure it's 16 bytes long) - Prompt the user for either "encrypt" or "decrypt", and then pass in the
correct Cipher_MODE value (as shown earlier in this writeup). Here's an example of what the
output should look like while in use: Enter File Path: src/tux.png Enter Initialization Vector (16
chars): aaabbbbccccdddd Enter Password ( 16 chars): hello Enter Password ( 16 chars):
notlongenough Enter Password (16 chars): thisis16bytesyes Enter mode: encrypt or decrypt:
encrypt Done The easiest way to do testing for this is to copy files directly into your Eclipse
project folder, and then reference them with a path starting in src. This makes sure that you won't
accidentally encrypt other files on your machine (possibly irreversibly). & sre cosc 232 >
caesar.java > D demo.java > encryptFile.java > MyRandom.java > D sandbox.java > [D
stream.java (D) test2.java tux.png Using DES The crypto library supports many different ciphers
and encryption standards, including the DES and RSA encryption algorithms. Check out the
documentation here: https://docs.oracle.com/iavase/7/docs/api/iavax/crypto/Cipher.html

More Related Content

DOCX
import java-io-IOException- import java-nio-file-Files- import java-ni.docx
PPT
Java Symmetric
PDF
Apache avro data serialization framework
PPS
Advance Java
PDF
File Handling in Java.pdf
ODT
Best Of Jdk 7
PDF
Lecture10
PPTX
Serialization in java
import java-io-IOException- import java-nio-file-Files- import java-ni.docx
Java Symmetric
Apache avro data serialization framework
Advance Java
File Handling in Java.pdf
Best Of Jdk 7
Lecture10
Serialization in java

Similar to encryptFile- java import java-io-IOException- import java-nio-file-Fil.docx (20)

PPT
Taking User Input in Java
PPT
Corba
PDF
Java sockets
PDF
PDF
Develop an encryption and decryption algorithm Your program should a.pdf
PPTX
ExtraFileIO.pptx
ODP
Biopython
ODT
Java practical
PDF
5java Io
PDF
Java IO Stream, the introduction to Streams
PPTX
interface in java explained in detailed form
PDF
Change the code in Writer.java only to get it working. Must contain .pdf
PDF
Hibernate Import.Sql I18n
PDF
IO Streams, Serialization, de-serialization, autoboxing
PDF
What`s new in Java 7
PDF
3.Lesson Plan - Input.pdf.pdf
PDF
djkkfhulkgyftfdtrdrsdsjjjjjjjjjjjjjjjjjjj
PPT
Java căn bản - Chapter12
PPT
Chapter 12 - File Input and Output
Taking User Input in Java
Corba
Java sockets
Develop an encryption and decryption algorithm Your program should a.pdf
ExtraFileIO.pptx
Biopython
Java practical
5java Io
Java IO Stream, the introduction to Streams
interface in java explained in detailed form
Change the code in Writer.java only to get it working. Must contain .pdf
Hibernate Import.Sql I18n
IO Streams, Serialization, de-serialization, autoboxing
What`s new in Java 7
3.Lesson Plan - Input.pdf.pdf
djkkfhulkgyftfdtrdrsdsjjjjjjjjjjjjjjjjjjj
Java căn bản - Chapter12
Chapter 12 - File Input and Output

More from akilaha (20)

DOCX
Exercise 4-1 (Algo) Computing revenues- expenses- and income LO C1 Fil.docx
DOCX
Exercise 3- -10 marks- Draw a 4-bit Serial In- Serial Out register usi.docx
DOCX
Exercise 2-4 At this point- we discussed sequence- decomposition- and.docx
DOCX
Exercise 2- Apply the following operations in binary system (using 8 b.docx
DOCX
ExE RCise The Integumentary System Name Ubininebate Basic Structure of.docx
DOCX
Ex 4- People tend to use lower case letters in computer code to denote.docx
DOCX
Every employee of the company has access to HRIS of that firm- True Fa.docx
DOCX
Event A occurs with probability 0-51- Event B occurs with probability.docx
DOCX
Events A and B are mutually exclusive- Which of the following statemen.docx
DOCX
Eve has just discovered and decrypted the file that associates each us.docx
DOCX
Evaluate the sustainability of IT services- devices and day-to-day ope.docx
DOCX
euil- her tobil oost of producing quith- e- Herneit is C-25-000+509 Ho.docx
DOCX
Ethicai hackiers are hered to evaluate and dedend agelnet theats woth.docx
DOCX
Essay question - A few years ago Angelina Jolie had a double mastectom.docx
DOCX
Essentials of Human Resources Please a new answer not one posted How w.docx
DOCX
Essay - How data mining and social network analysis were implemented (.docx
DOCX
Error Control involves taking one of three actions- (1) Toss the frame.docx
DOCX
erieski- 0-470-760-29 fuire in the liot b-2-4 Tres oy.docx
DOCX
Eroms tle following Triel Bolance of ABC compeny- Prepure Trading and.docx
DOCX
Erika categorized her spending for this month into four categories- Re.docx
Exercise 4-1 (Algo) Computing revenues- expenses- and income LO C1 Fil.docx
Exercise 3- -10 marks- Draw a 4-bit Serial In- Serial Out register usi.docx
Exercise 2-4 At this point- we discussed sequence- decomposition- and.docx
Exercise 2- Apply the following operations in binary system (using 8 b.docx
ExE RCise The Integumentary System Name Ubininebate Basic Structure of.docx
Ex 4- People tend to use lower case letters in computer code to denote.docx
Every employee of the company has access to HRIS of that firm- True Fa.docx
Event A occurs with probability 0-51- Event B occurs with probability.docx
Events A and B are mutually exclusive- Which of the following statemen.docx
Eve has just discovered and decrypted the file that associates each us.docx
Evaluate the sustainability of IT services- devices and day-to-day ope.docx
euil- her tobil oost of producing quith- e- Herneit is C-25-000+509 Ho.docx
Ethicai hackiers are hered to evaluate and dedend agelnet theats woth.docx
Essay question - A few years ago Angelina Jolie had a double mastectom.docx
Essentials of Human Resources Please a new answer not one posted How w.docx
Essay - How data mining and social network analysis were implemented (.docx
Error Control involves taking one of three actions- (1) Toss the frame.docx
erieski- 0-470-760-29 fuire in the liot b-2-4 Tres oy.docx
Eroms tle following Triel Bolance of ABC compeny- Prepure Trading and.docx
Erika categorized her spending for this month into four categories- Re.docx

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Sports Quiz easy sports quiz sports quiz
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Classroom Observation Tools for Teachers
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
01-Introduction-to-Information-Management.pdf
PPTX
master seminar digital applications in india
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPH.pptx obstetrics and gynecology in nursing
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Sports Quiz easy sports quiz sports quiz
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
Classroom Observation Tools for Teachers
FourierSeries-QuestionsWithAnswers(Part-A).pdf
TR - Agricultural Crops Production NC III.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Computing-Curriculum for Schools in Ghana
01-Introduction-to-Information-Management.pdf
master seminar digital applications in india
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

encryptFile- java import java-io-IOException- import java-nio-file-Fil.docx

  • 1. encryptFile. java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class encryptFile { // The crypt() function encrypts or decrypts a byte array input, using // an 16-byte initialization vector (init), 16-byte password (pass), // and an integer mode (either Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE) public static byte[] crypt(byte[] input, byte[] init, byte[] pass, int mode) { // TODO - Fill this out. } // The cryptFile() function opens a file at a specified string path, // then passes in the init, pass, and mode values to the crypt() function // to either encrypt or decrypt the contents of the file. It then writes // the encrypted or decrypted data back to the file. Note that it should // overwrite the existing file - so don't try it on a file that's actually // worth anything! public static void cryptFile(String path, byte[] init, byte[] pass, int mode) {
  • 2. // TODO - Fill this out. } // The menu() function provides a user interface for the script. It should // prompt the user to enter a file path, 16-byte initialization vector, // 16-byte password, and a mode (encrypt or decrypt). If the password or // initialization vector are too short or too long, the function should // re-prompt the user to re-enter the value. public static void menu() { // TODO - Fill this out. } // Just need to call the menu() function here public static void main(String[] args) { // Tests for crypt(); byte[] plain = "Hello World".getBytes(); byte[] pass = "aaaabbbbccccdddd".getBytes(); byte[] init = "gggggggggggggggg".getBytes(); byte[] cipher = crypt(plain, init, pass, Cipher.ENCRYPT_MODE); byte[] decrypted = crypt(cipher, init, pass, Cipher.DECRYPT_MODE); // This should print "Hello World" System.out.println(new String(decrypted)); // Uncomment below to test menu section once complete //menu(); }
  • 3. } than ideal - you generally want to use standardized implementations of cryptographic functions, as it's much less likely that they'll have internal defects or flaws that pose a security risk. For this lab, we'll use the javax.crypto libraries in Java. These libraries provide access to a number of cryptographically-related functions, including the ability to encrypt in AES and DES. A skeleton file has been provided called encryptFile.java. The crypt() function The first function we'll complete is the crypt() function. This function takes three byte arrays: an input, which is either a ciphertext or plaintext, a init, which is a 16 -byte initialization vector (for Chaining Block Mode), and a pass, which is a 16 -byte key. It also takes a mode, which is an integer. The Cipher module includes the modes declared as final static integers: Cipher.ENCRYPT_MODE Cipher.DECRYPT_MODE Inside the function, the first thing we need to do is pass the initialization vector and the password to the IvParameterSpec and SecretKeySpec objects respectively. These objects prepare the IV and password for use, and check that they are appropriate. They must each by 16 bytes in length, no longer and no shorter - this usually equals 16 ASClI characters. The IV byte array can be passed directly into the constructor of a new IvParameterSpec object: new IvParameterSpec(init); However, when creating a SecretKeySpec object, you must also specify (in a String) the type of cipher that you are using. In our case, we'll use AES, and pass in our password byte array object: new SecretKeySpec(pass, "AES"); We can then create our Cipher object. Rather than use new like we do for most objects, we instead make a call to the Cipher.getInstance() function, passing in a String that defines several aspects of our cipher: - The cipher to be used (i.e. AES) - The mode of operation (i.e. CBC (chaining block cipher) or ECB (electronic code book) - The padding to be used (i.e. PKCS5 standard for padding) This will create a new Cipher object, which we can then use. Here's an example of what the getInstance() constructor should look like: Cipher.getInstance("AES/CBC/PKCSSPADDING"); Then, we need to call the init method on our new Cipher object. The init method takes three inputs: the integer mode, the SecretKeySpec object we created, and the IvParameterSpec that we created. Finally, we can perform our encryption/decryption. We'll take the input byte array and pass it to the dofinal() function of our initialized Cipher object. This will return a new byte array, which is our output. If we are performing encryption, the output will be our ciphertext; if we are performing decryption, the output will be our plaintext. You should then return this bytearray. Note that the above will require a try/except or a throws declaration around the Cipher-related lines. The cryptFile() function Now we have a function that can encrypt or decrypt byte arrays at will, so let's put it to use. The cryptFile() function takes a String path for a file, plus the bytearrays init and pass, and the integer mode. Inside of the function, we first need to check our file path and create a Path object. We can do this by just calling the Paths.get() function on our String path; this will return a new Path object. We can then get a bytearray of the file contents using the Files module, which has a function Files.readAllBytes(path). Pass in your Path object that you created. This will return a bytearray of the file contents. Pass the file contents as the input parameter to the crypt function, along with the init, pass, and mode parameters. This will return the bytearray output that is either a ciphertext or a plaintext (depending on if you are encrypting or decrypting). Finally, write the bytearray back to the original file using the Files.write() function. This takes the Path object and the bytearray as arguments: Files.write(file, output); This function will work with any type of file, including both plaintext and binary files. You will notice that binary files, like images, no longer work properly when encrypted. This is because the file header and related
  • 4. information is encrypted as well, and your computer will be unable to parse what the file is supposed to be. In order to make it readable again, you just need to decrypt the file using the same IV and password. You should verify this works by trying to encrypt a text file and an image. The menu() function Finally, let's put an interface on the script. This is fairly straightforward and just requires a couple Scanner calls to get user input, along with a couple loops to catch incorrect-length IV's and passwords. I'll leave this section up to you, but your code should: Create a new crypt() function (name of your choice) that implements an alternate cipher available in the Cipher object, and add a prompt to your menu() that allows the user to select between AES and the alternate cipher. When you are done, submit your finished Java file on the course Moodle Page. - Prompt the user to enter a filepath - Prompt for an initialization vector, and re-prompt if the user provides an IV that is not exactly 16 bytes in length (use a loop that checks the length() of the input bytearray). - Do the same as above, but for the password (make sure it's 16 bytes long) - Prompt the user for either "encrypt" or "decrypt", and then pass in the correct Cipher_MODE value (as shown earlier in this writeup). Here's an example of what the output should look like while in use: Enter File Path: src/tux.png Enter Initialization Vector (16 chars): aaabbbbccccdddd Enter Password ( 16 chars): hello Enter Password ( 16 chars): notlongenough Enter Password (16 chars): thisis16bytesyes Enter mode: encrypt or decrypt: encrypt Done The easiest way to do testing for this is to copy files directly into your Eclipse project folder, and then reference them with a path starting in src. This makes sure that you won't accidentally encrypt other files on your machine (possibly irreversibly). & sre cosc 232 > caesar.java > D demo.java > encryptFile.java > MyRandom.java > D sandbox.java > [D stream.java (D) test2.java tux.png Using DES The crypto library supports many different ciphers and encryption standards, including the DES and RSA encryption algorithms. Check out the documentation here: https://docs.oracle.com/iavase/7/docs/api/iavax/crypto/Cipher.html