eneter.messaging.dataprocessing.serializing
Class RsaSerializer

java.lang.Object
  extended by eneter.messaging.dataprocessing.serializing.RsaSerializer
All Implemented Interfaces:
ISerializer

public class RsaSerializer
extends java.lang.Object
implements ISerializer

Serializer using RSA. The serialization:

  1. Incoming data is serialized by underlying serializer (e.g. XmlStringSerializer)
  2. The random key is generated and used with AES algorythm to encrypt the serialized data.
  3. The random key for AES is encrypted by RSA using the public key.
  4. The serialized data consits of AES encrypted data and RSA encrypted key for AES.
The deserialization:
  1. The receiver decrypts the AES key by RSA using its private key.
  2. Decrypted key is used to decrypt AES encrypted data.
  3. Decrypted data is deserialized by underlying serialized (e.g. XmlStringSerializer)
  4. The deserialization returns deserialized data.

 String aDataToSerialize = "Hello";
 
 KeyPairGenerator aKeyPairGenerator = KeyPairGenerator.getInstance("RSA");
 aKeyPairGenerator.initialize(1024);
 KeyPair aKeyPair = aKeyPairGenerator.generateKeyPair();
 RSAPrivateKey aPrivateKey = (RSAPrivateKey)aKeyPair.getPrivate();
 RSAPublicKey aPublicKey = (RSAPublicKey)aKeyPair.getPublic();
 
 RsaSerializer aSerializer = new RsaSerializer(aPublicKey, aPrivateKey);
 
 Object aSerializedData = aSerializer.serialize(aDataToSerialize, String.class);
 
 String aDeserializedData = aSerializer.deserialize(aSerializedData, String.class);
 
 


Constructor Summary
RsaSerializer(java.security.interfaces.RSAPublicKey publicKey, java.security.interfaces.RSAPrivateKey privateKey)
          Constructs the RSA serializer with default paraneters.
RsaSerializer(java.security.interfaces.RSAPublicKey publicKey, java.security.interfaces.RSAPrivateKey privateKey, int aesBitSize, ISerializer underlyingSerializer)
          Constructs the RSA serializer with custom parameters.
 
Method Summary
<T> T
deserialize(java.lang.Object serializedData, java.lang.Class<T> clazz)
          Deserializes data.
<T> java.lang.Object
serialize(T dataToSerialize, java.lang.Class<T> clazz)
          Serializes data.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RsaSerializer

public RsaSerializer(java.security.interfaces.RSAPublicKey publicKey,
                     java.security.interfaces.RSAPrivateKey privateKey)
Constructs the RSA serializer with default paraneters. It uses XmlStringSerializer and it will generate 128 bit key for the AES algorythm.

Parameters:
publicKey - public key used for serialization. It can be null if the serializer will be used only for deserialization.
privateKey - private key used for deserialization. It can be null if the serializer will be used only for serialization.

RsaSerializer

public RsaSerializer(java.security.interfaces.RSAPublicKey publicKey,
                     java.security.interfaces.RSAPrivateKey privateKey,
                     int aesBitSize,
                     ISerializer underlyingSerializer)
Constructs the RSA serializer with custom parameters.

Parameters:
publicKey - publicKey public key used for serialization. It can be null if the serializer will be used only for deserialization.
privateKey - private key used for deserialization. It can be null if the serializer will be used only for serialization.
aesBitSize - size of the random key generated for the AES encryption, 128, 256, ...
underlyingSerializer - underlying serializer used to serialize/deserialize data e.g. XmlStringSerializer
Method Detail

serialize

public <T> java.lang.Object serialize(T dataToSerialize,
                                      java.lang.Class<T> clazz)
                           throws java.lang.Exception
Serializes data.

Specified by:
serialize in interface ISerializer
Parameters:
dataToSerialize - Data to be serialized.
clazz - represents the serialized type.
Returns:
Object representing the serialized data. Based on the serializer implementation it can be byte[] or String.
Throws:
java.lang.Exception - If the serialization fails.

deserialize

public <T> T deserialize(java.lang.Object serializedData,
                         java.lang.Class<T> clazz)
              throws java.lang.Exception
Deserializes data.

Specified by:
deserialize in interface ISerializer
Parameters:
serializedData - Data to be deserialized.
Returns:
Deserialized object.
Throws:
java.lang.Exception - If the deserialization fails.