Package tahrir.io.serialization.serializers

Source Code of tahrir.io.serialization.serializers.RSAPrivateKeySerializer

package tahrir.io.serialization.serializers;

import java.io.*;
import java.lang.reflect.Type;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

import tahrir.io.serialization.TrSerializer;
import tahrir.tools.TrUtils;

public class RSAPrivateKeySerializer extends TrSerializer {

  public RSAPrivateKeySerializer() {
    super(RSAPrivateKey.class);
  }

  @Override
  protected RSAPrivateKey deserialize(final Type type, final DataInputStream dis) throws IOException {
    final byte[] bytes = new byte[dis.readInt()];
    TrUtils.readAllBytes(bytes, dis);
    try {
      return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(bytes));
    } catch (final Exception e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  protected void serialize(final Type type, final Object object, final DataOutputStream dos) throws IOException {
    final RSAPrivateKey key = (RSAPrivateKey) object;
    final byte[] encoded = new PKCS8EncodedKeySpec(key.getEncoded()).getEncoded();
    dos.writeInt(encoded.length);
    dos.write(encoded);
  }


}
TOP

Related Classes of tahrir.io.serialization.serializers.RSAPrivateKeySerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.