Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code from Java Programming Oracle Press. Unfortunately, I can't find the error
in this code. Could someone point out where is the problem?

The exception I'm getting is
Java
javax.crypto.BadPaddingException
(when run through Eclipse) and
Java
javax.crypto.IllegalBlockSizeException
(in console).

To give some context, I haven't yet been introduced to the crypto methods used
in this program. However, the author chose to implement the code in order to make an example
of Externalizable interface.

Thank you!

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class ExternalizableTestApp {

	public static void main(String[] args) {
		try {
			Customer customer = new Customer(1, "1234-5678-9876");
			System.out.println("Before saving object: ");
			System.out.println("ID: " + customer.getId() + " CC:"
					+ customer.getCreditCard());
			ObjectOutputStream outstream = new ObjectOutputStream(
					new FileOutputStream("customer.dat"));
			outstream.writeObject(customer);
			outstream.close();
			ObjectInputStream inputStream = new ObjectInputStream(
					new FileInputStream("customer.dat"));
			customer = (Customer) inputStream.readObject();
			System.out.println("After retrieving object: ");
			System.out.println("ID: " + customer.getId() + " CC:"
					+ customer.getCreditCard());
			inputStream.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

class Customer implements Externalizable {
	private int id;
	private String creditCard;
	private static Cipher cipher;
	private static SecretKeySpec skeySpec;

	static {
		try {
			createCipher();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	public String getCreditCard() {
		return creditCard;
	}

	public int getId() {
		return id;
	}

	public Customer() {
		id = 0;
		creditCard = "";
	}

	public Customer(int id, String ccNumber) {
		this.id = id;
		this.creditCard = ccNumber;
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		try {
			out.write(id);
			encrypt();
			out.writeUTF(creditCard);
			System.out.println("After ecryption: ");
			System.out.println("ID: " + id + " CC:" + creditCard);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		try {
			id = in.read();
			String str = in.readUTF();
			decrypt(str);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private static void createCipher() throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128);
		// Generate the secret key specs.
		SecretKey skey = kgen.generateKey();
		byte[] raw = skey.getEncoded();
		skeySpec = new SecretKeySpec(raw, "AES");
		// Instantiate the cipher
		cipher = Cipher.getInstance("AES");
	}

	private void encrypt() throws Exception {
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		byte[] buff = cipher.doFinal(creditCard.getBytes());
		creditCard = new String(buff);
	}

	private void decrypt(String str) throws Exception {
		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
		byte[] buff = cipher.doFinal(str.getBytes());
		creditCard = new String(buff);

	}

}
Posted
Updated 10-Feb-14 10:08am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900