I'm having some issues converting FileInputStream and FileOutputStream into CipherInputStream and CipherOutputStream for my 3DES encryption algorithm. Could someone direct me on the right path on how to do it? I end up getting the error: "CipherInputStream cannot be resolved to a type". Sorry if this is obvious.
// author Alexander Matheakis
public static void main(String[] args) throws Exception {
// file to be encrypted
FileInputStream inputFile = new FileInputStream("plainfile.txt");
// encrypted file
FileOutputStream outputFile = new FileOutputStream("C:\\Users\\islan\\OneDrive\\Documents\\Encryptor\\plainfile.des");
// password to encrypt the file
String passKey = "tkfhkggovubm";
byte[] salt = new byte[8];
SecureRandom r = new SecureRandom();
r.nextBytes(salt);
PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
SecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance("PBEWithSHA1AndDESede");
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
outputFile.write(salt);
byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inputFile.read(input)) != -1) {
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null)
outputFile.write(output);
}
byte[] output = cipher.doFinal();
if (output != null)
outputFile.write(output);
inputFile.close();
outputFile.flush();
outputFile.close();
// author Alexander Matheakis