# javax.xml.crypto.dsig.keyinfo

***

**1. Generating a KeyInfo Factory:**

```java
KeyInfoFactory factory = KeyInfoFactory.getInstance("DOM", "BC");
```

**2. Generating a KeyValue KeyInfo:**

```java
KeyValue keyValue = factory.newKeyValue(key);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(keyValue));
```

**3. Generating an X509Data KeyInfo:**

```java
X509Certificate cert = ...;
X509Data certData = factory.newX509Data(Collections.singletonList(cert));
KeyInfo info = factory.newKeyInfo(Collections.singletonList(certData));
```

**4. Generating a retrieval method KeyInfo:**

```java
RetrievalMethod retrievalMethod = factory.newRetrievalMethod(uri, type);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(retrievalMethod));
```

**5. Generating a PGPData KeyInfo:**

```java
PGPPublicKey key = ...;
PGPData pgpData = factory.newPGPData(key.getEncoded(), "application-pgp-encrypted");
KeyInfo info = factory.newKeyInfo(Collections.singletonList(pgpData));
```

**6. Generating an SPKIData KeyInfo:**

```java
byte[] spki = ...;
SPKIData spkiData = factory.newSPKIData(spki);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(spkiData));
```

**7. Generating a MgmtData KeyInfo:**

```java
XMLGregorianCalendar date = ...;
MgmtData mgmtData = factory.newMgmtData(date, "contact@example.com");
KeyInfo info = factory.newKeyInfo(Collections.singletonList(mgmtData));
```

**8. Generating a X509IssuerSerial KeyInfo:**

```java
X509IssuerSerial issuerSerial = factory.newX509IssuerSerial(issuerName, serialNumber);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(issuerSerial));
```

**9. Generating a KeyName KeyInfo:**

```java
String name = "KeyName";
KeyName keyName = factory.newKeyName(name);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(keyName));
```

**10. Generating a octet string KeyInfo:**

```java
String value = "octect string";
OctetString octect = factory.newOctetString(value.getBytes());
KeyInfo info = factory.newKeyInfo(Collections.singletonList(octect));
```

**11. Generating a RSAKeyName KeyInfo:**

```java
String keyName = "RSAKeyName";
RSAKeyName rsaKeyName = factory.newRSAKeyName(keyName);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(rsaKeyName));
```

**12. Generating a DSAKeyName KeyInfo:**

```java
String keyName = "DSAKeyName";
DSAKeyName dsaKeyName = factory.newDSAKeyName(keyName);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(dsaKeyName));
```

**13. Generating a X509SubjectName KeyInfo:**

```java
String subjectName = "X509SubjectName";
X509SubjectName x509SubjectName = factory.newX509SubjectName(subjectName);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(x509SubjectName));
```

**14. Generating a RetrievalMethod URI KeyInfo:**

```java
String uri = "uri://";
RetrievalMethod uriData = factory.newRetrievalMethod(uri);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(uriData));
```

**15. Generating a RetrievalMethod Transform KeyInfo:**

```java
Element element = ...;
Transform transform = ...;
RetrievalMethod transformData = factory.newRetrievalMethod(element, transform);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(transformData));
```

**16. Generating a X509SKI KeyInfo:**

```java
byte[] skI = ...;
X509SKI x509SKI = factory.newX509SKI(skI);
KeyInfo info = factory.newKeyInfo(Collections.singletonList(x509SKI));
```

**17. Generating a X509CRL KeyInfo:**

```java
X509CRL crl = ...;
X509CRL x509CRL = factory.newX509CRL(crl.getEncoded());
KeyInfo info = factory.newKeyInfo(Collections.singletonList(x509CRL));
```

**18. Generating a KeyInfoFactory from XML:**

```java
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File("keyInfo.xml"));
KeyInfoFactory keyInfoFactory = KeyInfoFactory.getInstance("DOM", "BC");
KeyInfo keyInfo = keyInfoFactory.unmarshalKeyInfo(doc.getDocumentElement());
```

**19. Marshaling a KeyInfo to XML:**

```java
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element root = doc.createElement("KeyInfo");
doc.appendChild(root);
KeyInfoFactory keyInfoFactory = KeyInfoFactory.getInstance("DOM", "BC");
keyInfoFactory.marshalKeyInfo(keyInfo, root);
doc.save(new File("keyInfo.xml"));
```

**20. Generating a KeyInfoFactory from a Signature:**

```java
XMLSignature signature = ...;
KeyInfoFactory keyInfoFactory = signature.getKeyInfoFactory();
```

**21. Marshaling a KeyInfo to a Signature:**

```java
XMLSignature signature = ...;
KeyInfoFactory keyInfoFactory = signature.getKeyInfoFactory();
KeyInfo keyInfo = ...;
signature.setKeyInfo(keyInfoFactory.marshalKeyInfo(keyInfo));
```

**22. Unmarshalling a KeyInfo from a Signature:**

```java
XMLSignature signature = ...;
KeyInfoFactory keyInfoFactory = signature.getKeyInfoFactory();
KeyInfo keyInfo = signature.getKeyInfo(keyInfoFactory);
```

**23. Unmarshalling a KeyInfo from a RetrievalMethod:**

```java
RetrievalMethod retrievalMethod = ...;
KeyInfoFactory keyInfoFactory = retrievalMethod.getKeyInfoFactory();
KeyInfo keyInfo = keyInfoFactory.unmarshalKeyInfo(retrievalMethod.getElement());
```

**24. Generating a KeyValue KeyInfo from a Key:**

```java
Key key = ...;
KeyValue keyValue = KeyValue.getInstance(key.getEncoded());
```

**25. Generating an X509Data KeyInfo from a Certificate:**

```java
X509Certificate cert = ...;
X509Data certData = X509Data.getInstance(Collections.singletonList(cert));
```

**26. Generating a retrieval method KeyInfo from a URI and Type:**

```java
String uri = "uri://";
String type = "type";
RetrievalMethod retrievalMethod = RetrievalMethod.getInstance(uri, type);
```

**27. Generating a PGPData KeyInfo from a PublicKey:**

```java
PGPPublicKey key = ...;
PGPData pgpData = PGPData.getInstance(key.getEncoded(), "application-pgp-encrypted");
```

**28. Generating an SPKIData KeyInfo from a PublicKey:**

```java
byte[] spki = ...;
SPKIData spkiData = SPKIData.getInstance(spki);
```

**29. Generating a MgmtData KeyInfo from a Date and Contact:**

```java
XMLGregorianCalendar date = ...;
String contact = "contact@example.com";
MgmtData mgmtData = MgmtData.getInstance(date, contact);
```

**30. Generating a X509IssuerSerial KeyInfo from an IssuerName and SerialNumber:**

```java
X500Name issuerName = ...;
BigInteger serialNumber = ...;
X509IssuerSerial issuerSerial = X509IssuerSerial.getInstance(issuerName, serialNumber);
```

\*\*31.
