Experiment with asymmetric keys. first generate a public/secret key pair using R
ID: 3603415 • Letter: E
Question
Experiment with asymmetric keys.
first generate a public/secret key pair using RSA. generate a private key (“privatekey.pem”) using this command
>> openssl genrsa -out private-key.pem 1024
and the corresponding public key
(“public-key.pem”) using this command: >> openssl rsa -in private-key.pem -pubout -out public-key.pem
2. Now you are ready to encrypt some data. Create a very small text file, say input.txt.
To encrypt this file, do this: >> openssl rsautl -encrypt -pubin -inkey public-key.pem -in input.txt -out input.enc
This will generate an encrypted output file called “input.enc”.
To decrypt the file, do >> openssl rsautl -decrypt -inkey private-key.pem -in input.enc -out answer.txt This will generate a decrypted version of “input.enc” called “answer.txt”. You can verify that answer.txt and input.txt are the same by comparing the hash outputs.
Question 1. Create an input file that is similar to the input.txt file you created before (e.g. extra spacing, punctuation, etc.). Encrypt the file using the same key as before. What can you observe from the encrypted cipher text?
Question 2. Create a different public/secret key pair. Using SHA, determine whether the two key pairs are the same or not. Now attempt to decrypt the cipher text that was encrypted using a different public key. Describe what happens.
Explanation / Answer
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.