Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

JCOAsymmetricKey.java

00001 package edu.virtualschool.jco;
00002 
00003 import java.io.ByteArrayInputStream;
00004 import java.io.ByteArrayOutputStream;
00005 import java.io.IOException;
00006 import java.io.ObjectInputStream;
00007 import java.io.ObjectOutputStream;
00008 import java.io.Serializable;
00009 
00010 import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
00019 public abstract class JCOAsymmetricKey implements JCOKey
00020 {
00029   public JCOSealedBytes seal(Serializable object)
00030     throws JCOFault
00031   {
00032     try
00033     {
00034       ByteArrayOutputStream bos = new ByteArrayOutputStream();
00035       ObjectOutputStream oos = new ObjectOutputStream(bos);
00036       oos.writeObject(object);
00037       oos.close();
00038       byte[] bytes = encryptBytes(bos.toByteArray());
00039       return new JCOSealedBytes(bytes);
00040     }
00041     catch (IOException e)  { throw new JCOFault("IO Exception", e); }
00042   }
00043   public Object unseal(JCOSealedBytes sealedObject)
00044     throws JCOFault
00045   {
00046     try
00047     {
00048       byte[] bytes = decryptBytes(sealedObject.bytes);
00049       ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
00050       ObjectInputStream oos = new ObjectInputStream(bis);
00051       Object object = oos.readObject();
00052       oos.close();
00053       return object;
00054     }
00055     catch (IOException e) { throw new JCOFault(e); }
00056     catch (ClassNotFoundException e) { throw new JCOFault(e); }
00057   }
00058   public abstract byte[] encryptBytes(byte[] bytes) throws JCOFault;
00059   public abstract byte[] decryptBytes(byte[] bytes) throws JCOFault;
00060   public abstract AsymmetricKeyParameter getParameters();
00061   
00063   public final String toString()
00064   {
00065     return JCOBase64.encode(getEncoded());
00066   }
00068   public final int hashCode() { return toString().hashCode(); }
00069 }