Decryption problem while - Urgent

Can any one suggest why this exception araises:java.security.spec.InvalidKeySpecException: java.io.EOFException: EOF encountered in middle of object.Thanks,nareshG.
[192 byte] By [JavaBaby@79a] at [2007-11-27 7:04:13]
# 1
> Can any one suggest why this exception araises:> > java.security.spec.InvalidKeySpecException:> java.io.EOFException: EOF encountered in middle of> object.> Yes! You have a bug in your code.
sabre150a at 2007-7-12 18:55:29 > top of Java-index,Security,Cryptography...
# 2
Thank you very much.i am trying to find more information that this Exception describes:This exception is thrown when a particular padding mechanism is expected for the input data but the data is not padded properly
JavaBaby@79a at 2007-7-12 18:55:29 > top of Java-index,Security,Cryptography...
# 3

Can any one tell me what could be the problem in this code:

package temp;

import java.io.DataInputStream;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.security.InvalidKeyException;

import java.security.KeyFactory;

import java.security.NoSuchAlgorithmException;

import java.security.NoSuchProviderException;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.spec.EncodedKeySpec;

import java.security.spec.InvalidKeySpecException;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

import javax.crypto.BadPaddingException;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;

import org.bouncycastle.util.encoders.Base64;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class test {

public static final String CREDIT_CARD_ENCRYPTION_ALGORITHM = "RSA/ECB/PKCS1Padding";

//Key Provider Factory

public static final String KEY_PROVIDER = "RSA";

//Provider used for RSA Algorithm

public static final String PROVIDER = "BC";

public static final String PUBLIC_KEY_FILE = "C:\\public1024.key";

public static final String PRIVATE_KEY_FILE = "C:\\private1024.key";

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

test t = new test();

String cardNumber = "1234567894561111";

try{

String eCard = t.encrypt(cardNumber);

System.out.println("-ENCRYPTION-"+eCard);

String dCard = t.decrypt(eCard);

System.out.println("DECRYPTION-"+dCard);

}catch (Exception e)

{e.printStackTrace();

}

}

public static String encrypt(String s) throws SecurityException

{

String encryptedText = null;

try

{

java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

javax.crypto.Cipher c = javax.crypto.Cipher.getInstance(CREDIT_CARD_ENCRYPTION_ALGORITHM ,PROVIDER);

String publicKeyFile = PUBLIC_KEY_FILE;

FileInputStream fis = new FileInputStream(publicKeyFile);

PublicKey key = getPublicKey(fis);

byte[] cipherText = encrypt(s.getBytes("UTF8"),key);

encryptedText = encodeBASE64(cipherText);

} catch (NoSuchPaddingException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (NoSuchProviderException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (FileNotFoundException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

throw new SecurityException(e);

}

return encryptedText;

}

private static byte[] encrypt(byte[] text, PublicKey key) throws SecurityException

{

byte[] cipherText = null;

try

{

// get an RSA cipher object and print the provider

javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(CREDIT_CARD_ENCRYPTION_ALGORITHM,PROVIDER);

// encrypt the plaintext using the public key

cipher.init(cipher.ENCRYPT_MODE, key);

cipherText = cipher.doFinal(text);

}

catch (NoSuchProviderException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (InvalidKeyException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (NoSuchPaddingException e) {

e.printStackTrace();

throw new SecurityException(e);

}catch (IllegalBlockSizeException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (BadPaddingException e) {

e.printStackTrace();

throw new SecurityException(e);

}

return cipherText;

}

/**

* @author jtirumalaset

* Decrypt BASE64 encoded text using private key

* @param text The encrypted text, encoded as BASE64

* @param key The private key

* @return The unencrypted text encoded as UTF8

* @throws java.lang.Exception

*/

public static String decrypt(String s) throws SecurityException

{

String result = null;

String privateKeyFile = PRIVATE_KEY_FILE;

try

{

java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

FileInputStream f1 = new FileInputStream(privateKeyFile);

PrivateKey key = getPrivateKey(f1);

// decrypt the text using the private key

byte[] dectyptedText = decrypt(decodeBASE64(s),key);

result = new String(dectyptedText, "UTF8");

} catch (FileNotFoundException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (IOException e) {

e.printStackTrace();

throw new SecurityException(e);

}

return result;

}

/**

* @author jtirumalaset

* Decrypt text using private key

* @param text The encrypted text

* @param key The private key

* @return The unencrypted text

* @throws java.lang.Exception

*/

private static byte[] decrypt(byte[] text, PrivateKey key) throws SecurityException

{

byte[] dectyptedText = null;

try

{

// decrypt the text using the private key

javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(CREDIT_CARD_ENCRYPTION_ALGORITHM,PROVIDER);

cipher.init(cipher.DECRYPT_MODE, key);

dectyptedText = cipher.doFinal(text);

} catch (NoSuchProviderException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (InvalidKeyException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (NoSuchPaddingException e) {

e.printStackTrace();

throw new SecurityException(e);

}catch (IllegalBlockSizeException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (BadPaddingException e) {

e.printStackTrace();

throw new SecurityException(e);

}

return dectyptedText;

}

/**

* @author jtirumalaset

* Decode BASE64 encoded string to bytes array

* @param text The string

* @return Bytes array

* @throws IOException

*/

private static byte[] decodeBASE64(String text) throws IOException

{

BASE64Decoder b64 = new BASE64Decoder();

return b64.decodeBuffer(text);

}

/**

* @author jtirumalaset

* Encode bytes array to BASE64 string

* @param bytes

* @return Encoded string

*/

private static String encodeBASE64(byte[] bytes)

{

BASE64Encoder b64 = new BASE64Encoder();

return b64.encode(bytes);

}

/**

* @author jtirumalaset

* Reads file from the given location and convert it into publicKey type

* @param f

* @return

* @throws SecurityException

*/

private static PublicKey getPublicKey(FileInputStream f)throws SecurityException{

PublicKey pubkey = null;

try{

String key = readFile(f);

EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(Base64.decode(key));

KeyFactory kf = KeyFactory.getInstance(KEY_PROVIDER,PROVIDER);

pubkey = kf.generatePublic(pubKeySpec);

} catch (NoSuchProviderException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (InvalidKeySpecException e) {

e.printStackTrace();

throw new SecurityException(e);

}

return pubkey;

}

/**

* @author jtirumalaset

* Reads file from the given location and convert it into privateKey type

* @param f

* @return

* @throws SecurityException

*/

private static PrivateKey getPrivateKey(FileInputStream f) throws SecurityException{

PrivateKey privKey = null;

try {

String key = readFile(f);

EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Base64.decode(key));

System.out.println("===="+privKeySpec.getEncoded());

System.out.println("RSA"+privKeySpec.getFormat());

KeyFactory kf = KeyFactory.getInstance(KEY_PROVIDER,PROVIDER);

privKey = kf.generatePrivate(privKeySpec);

} catch (NoSuchProviderException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch (InvalidKeySpecException e) {

e.getMessage();

e.printStackTrace();

throw new SecurityException(e);

}

return privKey;

}

/**

* @author jtirumalaset

* Reading the records from the key files.

* @param file

* @return

*/

public static String readFile(FileInputStream file) throws SecurityException

{

DataInputStream data = new DataInputStream(file);

byte[] publickey = new byte[1024];

String tmp = null;

String record = "";

try{

while ((tmp=data.readLine())!=null){

record = record + tmp;

}

record = record.trim();

}

catch(FileNotFoundException e) {

e.printStackTrace();

throw new SecurityException(e);

} catch(IOException e){

e.printStackTrace();

throw new SecurityException(e);

} finally {

try {

file.close();

} catch(IOException e){

e.printStackTrace();

throw new SecurityException(e);

}

}

return record;

}

}

JavaBaby@79a at 2007-7-12 18:55:29 > top of Java-index,Security,Cryptography...
# 4
getting this exception now:javax.crypto.BadPaddingException: unknown block type2007-06-09 14:23:51,937 INFO [STDOUT]at org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(Unknown Source)2007-06-09 14:23:51,937 INFO [STDOUT]at
JavaBaby@79a at 2007-7-12 18:55:29 > top of Java-index,Security,Cryptography...
# 5
Use the [code] tags around your code please. http://forum.java.sun.com/help.jspa?sec=formatting
DarumAa at 2007-7-12 18:55:29 > top of Java-index,Security,Cryptography...