3 Replies - 459 Views - Last Post: 03 October 2012 - 08:22 PM Rate Topic: -----

#1 liftthis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 08-May 05

Need help creating an exception for wrong password in decrypt method

Posted 03 October 2012 - 06:32 PM

The following code works but I am trying to prevent decrypting a file if you have the wrong key.
Any ideas?


// Example code showing how to use Java's Password-Based encryption. This
// example is a simplified form of the code in the documentation for the
// java cryptography architecture.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
//import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
//import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.imageio.ImageIO;
import javax.crypto.spec.DESKeySpec;


public class sample
{

    static String key = "squirrel123";
    static String key1 = "squirrel1234";

    public static void main(String[] arg) throws Exception
    {


      encryptFile(key, "c:\\users\\test\\rose.jpg",
      "c:\\users\\test\\encrypt.jpg","JPG");

      decryptFile(key1, "c:\\users\\test\\encrypt.jpg",
      "c:\\users\\test\\decrypted_output.jpg","JPG");


    }



    public static void encryptFile(String key, String
                  nonEncryptedFile, String EncryptedFile, String
                  typeFile) throws GeneralSecurityException,
                  FileNotFoundException
    {


       DESKeySpec dks = new DESKeySpec(key.getBytes());
       SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
       SecretKey desKey = skf.generateSecret(dks);
       Cipher pbeCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");


      if((typeFile.equals("JPG"))||
         (typeFile.equals("GIF"))||
         (typeFile.equals("PNG"))||
         (typeFile.equals("BMP")))
      {

        File inputFile = new File(nonEncryptedFile);

        BufferedImage input = null;

        pbeCipher.init(Cipher.ENCRYPT_MODE, desKey);

        try
        {
          input = ImageIO.read(inputFile);
        }
          catch (IOException e)
        {
          // TODO Auto-generated catch block
         e.printStackTrace();
        }

        FileOutputStream output = null;

        try
        {
          output = new FileOutputStream(EncryptedFile);
        }
        catch (FileNotFoundException e)
        {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

        CipherOutputStream cos = new CipherOutputStream(output,
         pbeCipher);

        try
        {
          ImageIO.write(input,typeFile,cos);
        }
          catch (IOException e1)
        {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }

        try
        {
          cos.close();
        }
        catch (IOException e)
        {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
     }// end of if



    }// end of encrypt file


    public static void doCopy(InputStream is, OutputStream os) 
    throws IOException
    {
       byte[] bytes = new byte[64];
       int numBytes;
       while ((numBytes = is.read(bytes)) != -1) {
         os.write(bytes, 0, numBytes);
       }
       os.flush();
       os.close();
       is.close();
    }

    public static void decryptFile(String key, String encryptedFile,
                        String decryptedFile, String typeFile)throws 
                     GeneralSecurityException, FileNotFoundException
    {

       DESKeySpec dks = new DESKeySpec(key1.getBytes());
       SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
       SecretKey desKey = skf.generateSecret(dks);
       Cipher pbeCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

       try
       {
         pbeCipher.init(Cipher.DECRYPT_MODE, desKey);

         System.out.println("Decrypt mode!");

         // Decrypt the ciphertext and then print it out.
         FileInputStream output = null;

         if((typeFile.equals("JPG"))||
            (typeFile.equals("GIF"))||
            (typeFile.equals("PNG"))||
            (typeFile.equals("BMP")))
         {

            try {
              output = new FileInputStream(encryptedFile);
            } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }

            CipherInputStream cis = new CipherInputStream(output,
                  pbeCipher);
            BufferedImage input = null;
            try {
               input = ImageIO.read(cis);
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }

            FileOutputStream out = null;

            try {
              out = new FileOutputStream(decryptedFile);
            } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }

            try {
              ImageIO.write(input,typeFile, out);
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }

            try {
              cis.close();
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }

         }// end if


       } catch (IOException e ){
         System.out.println(" key is invalid");
       }

    }// end of decrypt file

} 



This post has been edited by liftthis: 03 October 2012 - 06:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help creating an exception for wrong password in decrypt method

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need help creating an exception for wrong password in decrypt method

Posted 03 October 2012 - 06:35 PM

Please indent your code correctly, it is a nightmare to read

How/wher do you validate that the key is valid ?
Was This Post Helpful? 0
  • +
  • -

#3 liftthis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 08-May 05

Re: Need help creating an exception for wrong password in decrypt method

Posted 03 October 2012 - 06:56 PM

I know that I am not checking for the key valid correctly. I think I need to check for padding but I need some help. Has anybody ever done this?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need help creating an exception for wrong password in decrypt method

Posted 03 October 2012 - 08:22 PM

Difficult to throw an Exception if you don't know what would justify throwing it

Do you even know what you should consider as a valid key ? Do you have a validation mechanism that validates that a key matches an encrypted stream of data ? Is your architecture designed to support one ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1