Class RsaPrivateKeySpec

java.lang.Object
zeroecho.core.alg.rsa.RsaPrivateKeySpec
All Implemented Interfaces:
Destroyable, AlgorithmKeySpec

public final class RsaPrivateKeySpec extends Object implements AlgorithmKeySpec, Destroyable
Encoded RSA private key specification in PKCS#8 format.

This spec wraps the DER-encoded PKCS#8 representation of an RSA private key and is used for importing keys into KeyFactory or algorithm builders (see RsaAlgorithm). It provides safe cloning of the encoded form and supports simple marshal/unmarshal via PairSeq.

Usage

// Import a PKCS#8 encoded key
byte[] pkcs8 = Files.readAllBytes(Path.of("rsa_private.der"));
RsaPrivateKeySpec spec = new RsaPrivateKeySpec(pkcs8);

// Retrieve encoded form
byte[] encoded = spec.encoded();

// Marshal for storage or transmission
PairSeq seq = RsaPrivateKeySpec.marshal(spec);

// Rebuild from serialized representation
RsaPrivateKeySpec parsed = RsaPrivateKeySpec.unmarshal(seq);

Instances defensively copy their input and may be destroyed to wipe the owned encoding. The encoded key material remains sensitive and should be handled with care.

Since:
1.0
  • Constructor Details

    • RsaPrivateKeySpec

      public RsaPrivateKeySpec(byte[] pkcs8)
      Constructs a new RSA private key spec from a PKCS#8-encoded key.
      Parameters:
      pkcs8 - DER-encoded PKCS#8 private key; cloned internally
      Throws:
      NullPointerException - if pkcs8 is null
  • Method Details

    • encoded

      public byte[] encoded()
      Returns a clone of the PKCS#8-encoded private key.
      Returns:
      defensive copy of the encoded key
    • marshal

      public static PairSeq marshal(RsaPrivateKeySpec spec)
      Marshals this spec to a PairSeq.

      The output sequence contains:

      • type = "RSA-PRIV"
      • pkcs8.b64 = <Base64 encoding of PKCS#8 bytes>
      Parameters:
      spec - RSA private key spec
      Returns:
      pair sequence with type and base64-encoded key material
    • unmarshal

      public static RsaPrivateKeySpec unmarshal(PairSeq p)
      Reconstructs an RSA private key spec from a marshaled PairSeq.

      Expects an entry pkcs8.b64 containing the Base64-encoded PKCS#8 private key. The type field is ignored for forward compatibility.

      Parameters:
      p - serialized pair sequence
      Returns:
      RSA private key spec with decoded key material
      Throws:
      IllegalArgumentException - if the sequence does not contain a pkcs8.b64 entry
    • destroy

      public void destroy()
      Specified by:
      destroy in interface Destroyable
    • isDestroyed

      public boolean isDestroyed()
      Specified by:
      isDestroyed in interface Destroyable