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
javax.crypto.BadPaddingException
(when run through Eclipse) and
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);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
skeySpec = new SecretKeySpec(raw, "AES");
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);
}
}