# KeyToPem()

Converts a Java key object or Base64-encoded key to PEM format.

**Requires Extension:** [Crypto Extension](https://download.lucee.org/#17AB52DE-B300-A94B-E058FC978BE4542D)

```
KeyToPem( key=any, algorithm=string );
```

**Returns:** string

# Arguments

| Argument | Type | Required | Description | Default |
|----------|------|----------|-------------|---------|
| key | any | Yes | Java Key object or Base64-encoded key string |  |
| algorithm | string | No | Algorithm name (RSA, EC, etc). Required when key is Base64-encoded. Not needed for Java Key objects or PEM strings (auto-detected). |  |

# Examples

```cfml
// Convert a Java key object back to a PEM string
// Useful for storing or transmitting keys
keyPair = GenerateKeyPair( "RSA-2048" );

// Convert key objects to PEM strings
privateKey = PemToKey( keyPair.private );
pem = KeyToPem( privateKey );
// "-----BEGIN PRIVATE KEY-----\nMIIE..."

publicKey = PemToKey( keyPair.public );
pem = KeyToPem( publicKey );
// "-----BEGIN PUBLIC KEY-----\nMIIB..."
```







# Categories

[Cryptography](../../categories/crypto.md)

# See Also

[GenerateKeyPair()](generatekeypair.md), [KeyToJwk()](keytojwk.md), [PemToKey()](pemtokey.md), [ValidateKeyPair()](validatekeypair.md)