Exemplo n.º 1
0
  public static void writeRSAPrivateKey(
      Writer _out, RSAPrivateCrtKey obj, CipherSpec cipher, char[] passwd) throws IOException {
    assert (obj != null);
    BufferedWriter out = makeBuffered(_out);
    RSAPrivateKeyStructure keyStruct =
        new RSAPrivateKeyStructure(
            obj.getModulus(),
            obj.getPublicExponent(),
            obj.getPrivateExponent(),
            obj.getPrimeP(),
            obj.getPrimeQ(),
            obj.getPrimeExponentP(),
            obj.getPrimeExponentQ(),
            obj.getCrtCoefficient());

    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);
    aOut.writeObject(keyStruct);
    aOut.close();
    byte[] encoding = bOut.toByteArray();

    if (cipher != null && passwd != null) {
      writePemEncrypted(out, PEM_STRING_RSA, encoding, cipher, passwd);
    } else {
      writePemPlain(out, PEM_STRING_RSA, encoding);
    }
  }