Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: decryption-problem


Message #1 by "Sanjay S Gurav" <sgurav1@l...> on Tue, 23 Jul 2002 15:20:01 -0500
hi,
      Sorry for the pasting the program here. I am doing directory
decryption program. I have done directory encryption which is working. I
have directory with encrypted files. But when I tried for directory
decryption it is giving run-time error. Actually error is coming in last
6-8 lines. It is giving messege:

Exception in thread "main" java.io.IOException:error processing
stream:javax.crypto.BadPaddingException: pad block corrupted
 at javax.crypto.CipherInputStream.nextChunk(CipherInputStream.java:118)
at javax.crypto.CipherInputStream.read(CipherInputStream.java, compiled
code)
at decr.decrypt (decr.java, compiled code)
at decr.main (decr.java:27)


Will anyone suggest what need to be done.

Thanks,

Sanjay










import java.security.*;
 import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.io.File;
import java.awt.*;


/**
 *   This class encrypts and decrypts a file using CipherStreams
 *   and a 256-bit Rijndael key stored in the filesystem.
 */

public class decr
{
  private static String KEY_FILENAME="rijndaelkey.bin";
  private static int ITERATIONS=1000;

  public static void main (String  args [] )
   throws Exception
{
    // Convert the password into a char array

 char [] password = "sasquatch".toCharArray();
 createKey (password);
 decrypt(password);

}

  public static void createKey(char [] password)
  throws Exception
  {
    System.out.println("Encryption starts here");
    System.out.println("Generating a Rijndael key...");

    // Create a Rijndael key
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Rijndael");
    keyGenerator.init(256);
    Key key = keyGenerator.generateKey();

    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(char [] 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);

     // Create the PBE cipher
   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.DECRYPT_MODE, pbeKey, pbeParamSpec);

     // Decrypt the key bytes
     byte[] decryptedKeyBytes = cipher.doFinal(encryptedKeyBytes);

     // Create the key from the key bytes
     SecretKeySpec key = new SecretKeySpec(decryptedKeyBytes, "Rijndael");
     return key;

  }
  /**
   * Creates a 256-bit Rijndael key and stores it to
   * the filesystem as a KeyStore.
   */

 /**
   * Decrypt a file using Rijndael. Load the key
   * from the filesystem, given a password.
   */
  private static void decrypt(char [] password)
  throws Exception
  {
   String fileinput="j:/jdk1.2.2/bin/lsu";
 File f1 = new File(fileinput);
  if (f1.isDirectory()) {
  String m[]=f1.list();
for (int j=0; j<m.length; j++)
{
   File f2 = new File(fileinput+"/"+m[j]);
  if (f2.isDirectory())
{ System.out.println (m[j]+"is a directory");
           }
else
{

           //   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");
          byte[] iv = new byte[16];
      FileInputStream fis = new FileInputStream(fileinput+"/"+m[j]);
    FileOutputStream fos =new FileOutputStream("j:/jdk1.2.2/bin/osu/"
+m[j]);


 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);
        System.out.println("This is a directory");
    }
      System.out.println("directory");
    cis.close();
    fos.close();
          System.out.println("Hi");
  }
}
}
else System.out.println("This is not a directory");
}
}



  Return to Index