hi, I am trying to do directory encryption. But it is giving error while enlisting files as (Java.lang.FileDefNotFoundError). I am sending program herewith. Please guide me in this.
Program:
import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import java.io.*; import java.io.File; /** * This class encrypts and decrypts a file using CipherStreams * and a 256-bit Rijndael key stored in the filesystem. */
public class FileEncrypto {
private static String KEY_FILENAME="rijndaelkey.bin"; private static int ITERATIONS=1000; String dirname="/Bin";
public static void main (String[] args) throws IOException { System.err.println("check the error"); }
/** * Creates a 256-bit Rijndael key and stores it to * the filesystem as a KeyStore. */ private static void createKey(password) throws Exception { // System.out.println("Generating a Rijndael key...");
// System.out.println("Done generating the key.");
// Now we need to create the file with the key, // encrypting it with a password. byte[] salt = new byte[8]; SecureRandom random = new SecureRandom(); random.nextBytes(salt); PBEKeySpec pbeKeySpec = new PBEKeySpec(password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( "PBEWithSHAAndTwofish-CBC"); SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATIONS); Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC"); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
// Encrypt the key byte[] encryptedKeyBytes = cipher.doFinal(key.getEncoded());
// Write out the salt, and then the encrypted key bytes FileOutputStream fos = new FileOutputStream(KEY_FILENAME); fos.write(salt); fos.write(encryptedKeyBytes); fos.close();
}
/** * Loads a key from the filesystem */ private static Key loadKey( password) throws Exception { // Load the bytes from the encrypted key file. FileInputStream fis = new FileInputStream(KEY_FILENAME); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = 0; while ((i=fis.read()) != -1) { baos.write(i); } fis.close(); byte[] saltAndKeyBytes = baos.toByteArray(); baos.close();
// get the salt, which is the first 8 bytes byte[] salt = new byte[8]; System.arraycopy(saltAndKeyBytes,0,salt,0,8);
// get the encrypted key bytes int length = saltAndKeyBytes.length - 8; byte[] encryptedKeyBytes = new byte[length]; System.arraycopy(saltAndKeyBytes,8,encryptedKeyBytes,0,length);
// Decrypt the key bytes byte[] decryptedKeyBytes = cipher.doFinal(encryptedKeyBytes);
// Create the key from the key bytes SecretKeySpec key = new SecretKeySpec(decryptedKeyBytes, "Rijndael"); return key;
}
/** * Encrypt a file using Rijndael. Load the key * from the filesystem, given a password. */ private static void encrypt(char [] password, Inputdirectory, OutPutdirectory) throws Exception { System.out.println("Loading the key."); Key key = loadKey(password); System.out.println("Loaded the key.");
// Create a cipher using that key to initialize it Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");
// Now we need an Initialization Vector for the cipher in CBC mode. // We use 16 bytes, because the block size of Rijndael is 256 bits. SecureRandom random = new SecureRandom(); byte[] iv = new byte[16]; random.nextBytes(iv);
// Enlist the files in the directory if (f1.isdirectory() ) { String s[]=f1.list(); for (int i=0; i<s.length; i++) { File f = new file (s); FileInputStream fis = new FileInputStream(s); FileOutputStream fos = new FileOutputStream(s);
// Write the IV as the first 16 bytes in the file fos.write(iv); IvParameterSpec spec = new IvParameterSpec(iv);
System.out.println("Initializing the cipher.");
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
System.out.println("Encrypting the file...");
int theByte = 0; while ((theByte = fis.read()) != -1) { cos.write(theByte); } fis.close(); cos.close(); }
// Else println ("F1 is not a directory"); }
/** * Decrypt a file using Rijndael. Load the key * from the filesystem, given a password. */ private static void decrypt(char [] password, BIN, BIN) throws Exception { System.out.println("Loading the key."); Key key = loadKey(password); System.out.println("Loaded the key.");
// Create a cipher using that key to initialize it Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");
// Enlist the files in the directory and decrypt String m[]=f1.list(); for (int j=0; j<m.length; j++) { FileInputStream fis = new FileInputStream(m[j]); FileOutputStream fos = new FileOutputStream(m[j]);
// Read the IV from the file. It's the first 16 bytes. byte[] iv = new byte[16]; fis.read(iv);
IvParameterSpec spec = new IvParameterSpec(iv);
System.out.println("Initializing the cipher."); cipher.init(Cipher.DECRYPT_MODE, key, spec);
CipherInputStream cis = new CipherInputStream(fis, cipher);
System.out.println("Decrypting the file...");
int theByte = 0; while ((theByte = cis.read()) != -1) { fos.write(theByte); } cis.close(); fos.close(); } } // Else Println("f1 is not a diretory"); } } }