Urgent help!!!!..blowfish bad padding exception


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Urgent help!!!!..blowfish bad padding exception

  1. #1
    Join Date
    May 2004
    Posts
    1

    Unhappy Urgent help!!!!..blowfish bad padding exception

    Hi ,
    I have been working on encryption using the blowfish algorithm.The client side encrypts the data from a file stores the encrypted version in another file and sends the key to the server.The server decrypts the encrypted file using the key.I get no errors but a Badpadding exception at the server side.Pls help !!!!!!.




    //The client side progSocket c;
    String localInputFile="test.txt";
    String localOutputFile="encrypt.txt";



    KeyGenerator keyGenerator=KeyGenerator.getInstance("Blowfish");
    keyGenerator.init(128);
    SecretKey secretKey=keyGenerator.generateKey();
    Cipher cipherOut=Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherOut.init(Cipher.ENCRYPT_MODE,secretKey);

    FileInputStream fin=new FileInputStream(localInputFile);
    FileOutputStream fout=new FileOutputStream(localOutputFile);
    CipherOutputStream cout=new CipherOutputStream(fout,cipherOut);
    int size=fin.available();
    byte b[]=new byte[size];
    fin.read(b);

    int l=b.length;
    System.out.println("File len"+l);

    byte e[];
    e=cipherOut.doFinal(b,0,b.length);

    System.out.println("Cipher length"+e.length);
    int block=cipherOut.getBlockSize();
    System.out.println("block size"+block);
    cout.write(e,0,e.length);


    fout.close();
    fin.close();
    cout.close();


    byte[] blowfishKeyData = secretKey.getEncoded();
    int klen=blowfishKeyData.length;
    System.out.println(klen);
    System.out.println(blowfishKeyData);
    c=new Socket(InetAddress.getLocalHost(),1024);
    OutputStream out=c.getOutputStream();
    out.write(blowfishKeyData);
    out.close();
    c.close();



    //server side code

    ServerSocket sock=new ServerSocket(1024);
    Socket csock= sock.accept();

    InputStream is=csock.getInputStream();
    DataInputStream in=new DataInputStream(is);
    byte [] key=new byte[16];
    in.read(key);

    System.out.println(key);
    SecretKeySpec keySpec = new SecretKeySpec(key,"Blowfish");
    Cipher sessionCipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    sessionCipher.init(Cipher.DECRYPT_MODE, keySpec);
    FileInputStream din=new FileInputStream("encrypt.txt");
    FileOutputStream dout=new FileOutputStream("decrypt.txt");
    CipherInputStream cin=new CipherInputStream(din,sessionCipher);

    int size=din.available();
    System.out.println("The enc file size"+size);
    byte c[]=new byte[size];

    cin.read(c);
    System.out.println("Bytes in cipher file"+ c.length);

    byte d[];
    d=sessionCipher.doFinal(c,0,c.length);
    System.out.println("Bytes after decryption"+d.length);
    dout.write(d,0,d.length);

    //Exception occurring

    Badpadding Exception:final block not properly padded

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    read up on it... youre probably putting the wrong amount of data into the cipher.. say it operates on 1024 block chunks, and you put 4090 bytes into it.. well thats 3 blocks of 1024, plus a block that is 1018, which is too short..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center