Results 1 to 2 of 2

Thread: Padding Problem

  1. #1
    Srikar is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default Padding Problem

    Hello all,

    I am not new to Java but new to this forums but and JCE and i wanted to write a program that Encrypts a file and also another program that decrypts it. As far Encryption is concerned i have been successful but When it comes to Decryption things aren't looking bright i have some or the other Problem with it. plz help me out .

    Here is the Code for my Programs
    Encryption

    Java Code:
    import java.io.*;
    import javax.crypto.*;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.*;
    import javax.swing.*;
    class MyJCE
    {
    public static void main(String args[])throws Exception
    {
    Provider sunjce = new com.sun.crypto.provider.SunJCE();
    Security.addProvider(sunjce);
    JFileChooser jfc = new JFileChooser();
    int selection= jfc.showOpenDialog(null);
    if(selection==JFileChooser.APPROVE_OPTION)
    {
    FileInputStream fis = new FileInputStream(jfc.getSelectedFile());
    System.out.println("Selected file " + jfc.getSelectedFile());
    try{
    KeyGenerator kg = KeyGenerator.getInstance("DESede");
    SecretKey key= kg.generateKey();
    byte[] mkey=key.getEncoded();
    System.out.println(key);
    
    SecretKeySpec skey = new SecretKeySpec(mkey, "DESede");
    
    
    Cipher cipher=Cipher.getInstance("DESede/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE,skey);
    
    byte[] data= new byte[fis.available()];
    //reading the file into data byte array
    
    byte[] result= cipher.update(data);
    byte[] enc= new byte [fis.read(result)];
    System.out.println("Encrypted =" + result);
    File fi= new File("/home/srikar/Encrypted");
    FileOutputStream fos= new FileOutputStream(fi);
    fos.write(enc);
    fos.close();
    byte[] encodedSpeckey = skey.getEncoded();
    FileOutputStream ks= new FileOutputStream("./key.txt");
    ks.write(encodedSpeckey);
    System.out.println("Key written to a file");
    }//try
    catch(Exception ex)
    {
    ex.printStackTrace();
    }//catch
    
    }
    }
    }
    This Creates a Encrypted File. and a Encrypted key.txt

    Java Code:
    import java.io.*;
    import javax.crypto.*;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.*;
    import javax.swing.*;
    class Decrypt
    {
    public static void main(String[] args)
    {
    try
    {
    JFileChooser jfc = new JFileChooser();
    int selection= jfc.showOpenDialog(null);
    if(selection==JFileChooser.APPROVE_OPTION)
    {
    FileInputStream fis = new FileInputStream(jfc.getSelectedFile());
    System.out.println("Selected file " + jfc.getSelectedFile());
    
    //Read from the Encrypted Data
    int ll= (int)jfc.getSelectedFile().length();
    
    byte[] buffer = new byte[ll];
    int bytesRead=fis.read(buffer);
    byte[] data= new byte[bytesRead];
    System.arraycopy(buffer,0,data,0,bytesRead);
    
    
    //Read the Cipher Settings 
    FileInputStream rkey= new FileInputStream("./key.txt");
    bytesRead = rkey.read(buffer);
    byte[] encodedKeySpec=new byte[bytesRead];
    System.arraycopy(buffer,0,encodedKeySpec,0,bytesRead);
    
    //Recreate the Secret Symmetric Key
    SecretKeySpec skeySpec= new SecretKeySpec(encodedKeySpec,"DESede");
    
    //create the cipher for Decrypting
    Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE,skeySpec);
    
    byte[] decrypted= cipher.update(data);
    
    FileOutputStream fos= new FileOutputStream("/home/srikar/Decrypted");
    fos.write(decrypted);
    }//if
    }//try
    catch(Exception e)
    {
    e.printStackTrace();
    }//catch
    
    }//main
    }//class

    this Decrypt.java is expected to decrypt the above encrypted file but this simply creates a plaintext file of the same size as the Encrypted file but its contents are unreadable.

    Or I endup with Exceptions like BadPadding or IllegalBlockSize Exception if i use any other Algorithm .

    Please help out :( :confused::(
    thanx in advance

  2. #2
    Srikar is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default Done

    I have Rectified my mistake in this particular program actually the Encryption part is wrong as i read nothing into the Encrypted File and that was causing trouble. But sorted out. thankz all

Similar Threads

  1. OAEP Padding Problem by encryption
    By Buhmann in forum Advanced Java
    Replies: 1
    Last Post: 09-07-2009, 02:19 PM
  2. Replies: 4
    Last Post: 04-17-2009, 10:48 PM
  3. Single Component Padding
    By subb in forum AWT / Swing
    Replies: 2
    Last Post: 03-28-2009, 08:09 PM
  4. padding informaiton for PKCS#5
    By nanaji in forum Advanced Java
    Replies: 3
    Last Post: 07-31-2008, 06:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •