Question: I need some help with this Java Project please. 1. Copy the code from File Encryption and Decryption Simple Example. 2. Build and run the
I need some help with this Java Project please.
1. Copy the code from File Encryption and Decryption Simple Example.
2. Build and run the software. There is one small problem you will get when running it. If you read all of the web page text that the code comes from, it should be easy to fix.
3. Export the project as an executable jar file as in Creating a New Runnable JAR File
4. Verify that your jar file is not signed as in Verifying Signed JAR Files.
If you receive the "'jarsigner' is not recognized as an internal or external command, operable program or batch file." response to the jarsigner command,
There are 2 solutions to this problem.
1. Find where jarsigner.exe is located and add it to your path variable.
For me it is found at C:\Program Files\Java\jdk1.8.0_40\bin.
To see how to do this you can visit http://www.computerhope.com/issues/ch000549.htm (Links to an external site.)Links to an external site.
or refer to the "Before You Begin" section of out textbook (page xl).
2. When entering the command, supply the entire path.
For me it would be "C:\Program Files\Java\jdk1.8.0_40\bin\jarsigner" - verify DecryptEncrypt.jar
Option 2 is easier, but option 1 keeps you from needing to type that full path every time AND is the proper way to do it.
We will use the keytool to create a keystore before our second use of jarsigner - verify. The purpose of using it both before and after is to see the difference in the response to the command after signing the jar file.
5. Sign the executable jar file as in How to Create a (Self-)Signed Jar. There is a small problem in the signing step, but if you hover oxer each part of the command and read what each part means, their mistake should be obvious.
6. Verify that your jar file IS signed as in Verifying Signed JAR Files.
7. Submit all code and the jar file. No need to zip them.
File Encryption and Decryption Simple Example:
package net.codejava.crypto;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
/**
* A utility class that encrypts or decrypts a file.
* @author www.codejava.net
*
*/
public class CryptoUtils {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES";
public static void encrypt(String key, File inputFile, File outputFile)
throws CryptoException {
doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
}
public static void decrypt(String key, File inputFile, File outputFile)
throws CryptoException {
doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
}
private static void doCrypto(int cipherMode, String key, File inputFile,
File outputFile) throws CryptoException {
try {
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(cipherMode, secretKey);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
} catch (NoSuchPaddingException | NoSuchAlgorithmException
| InvalidKeyException | BadPaddingException
| IllegalBlockSizeException | IOException ex) {
throw new CryptoException("Error encrypting/decrypting file", ex);
}
}
}
package net.codejava.crypto;
public class CryptoException extends Exception {
public CryptoException() {
}
public CryptoException(String message, Throwable throwable) {
super(message, throwable);
}
}
package net.codejava.crypto;
import java.io.File;
/**
* A tester for the CryptoUtils class.
* @author www.codejava.net
*
*/
public class CryptoUtilsTest {
public static void main(String[] args) {
String key = "Mary has one cat1";
File inputFile = new File("document.txt");
File encryptedFile = new File("document.encrypted");
File decryptedFile = new File("document.decrypted");
try {
CryptoUtils.encrypt(key, inputFile, encryptedFile);
CryptoUtils.decrypt(key, encryptedFile, decryptedFile);
} catch (CryptoException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
