import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SealedObject;
public class SealedObjectExample {
public static void main(String[] args) {
String creatiCard = "1234567890";
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
Key key = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
System.out.println("Encrypting the Object");
SealedObject so = new SealedObject(creatiCard, cipher);
System.out.println("Decrypting the Object");
//String decryptedCrediteCard = (String) so.getObject(key);
cipher.init(Cipher.DECRYPT_MODE, key);
String decryptedCrediteCard = (String) so.getObject(cipher);
System.out.println("Credit card Number : " + decryptedCrediteCard);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |