> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/encryption/generatepbkdfkey.md).

# GeneratePBKDFKey

Generates an encoded encryption key using the specified algorithm and key size

## Method Signature

```
GeneratePBKDFKey(algorithm=[string], passphrase=[string], salt=[string], iterations=[numeric], keySize=[numeric])
```

### Arguments

| Argument     | Type      | Required | Description                                                                                                                                                                       | Default |
| ------------ | --------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `algorithm`  | `string`  | `true`   | The algorithm to use for generating the key. Algorithms supported are: PBKDF2WithHmacSHA1, PBKDF2WithHmacSHA224, PBKDF2WithHmacSHA256, PBKDF2WithHmacSHA384, PBKDF2WithHmacSHA512 | `AES`   |
| `passphrase` | `string`  | `true`   | The passphrase to use for generating the key. This is                                                                                                                             |         |
| `salt`       | `string`  | `true`   | The salt to use for generating the key. This is a random string                                                                                                                   |         |
| `iterations` | `numeric` | `false`  | The number of iterations to use for generating the key.                                                                                                                           |         |
| `keySize`    | `numeric` | `false`  | <p>The optional size of the key to generate. If not provided<br>the default key size for the algorithm will be used</p>                                                           |         |

## Examples

### Example PBKDF2 With HMAC SHA1

The `PBKDF2WithHmacSHA1` algorithm will work on older JVMs, or older versions of CF

[Run Example](https://try.boxlang.io/?code=eJxLT81LLUosSQ1w8nZx806t1FBQAjONwjNLMjxyE5ODPRwNlXQUlIpTk4tSS8CsxJySSiDD1MDAQEfB0MhCQdOaCwA5uBR8)

```java
generatePBKDFKey( "PBKDF2WithHmacSHA1", "secret", "salty", 5000, 128 );

```

Result: Y0MCpCe3zb0CNJvyXNUWEQ==

### More complex encryption example

```java
// some variables
password = "top_secret";
dataToEncrypt = "the most closely guarded secret";
encryptionAlgorithm = "AES";
keysize = 128;
algorithmVersion = 512;
PBKDFalgorithm = "PBKDF2WithHmacSHA" & algorithmVersion;
// Generate key as recommended in docs
length = keysize / 8;
multiplicator = 10 ^ length;
salt = Round( Randomize( 5, "SHA1PRNG" ) * multiplicator );
// The magic happens here
PBKDFKey = GeneratePBKDFKey( PBKDFalgorithm, password, salt, algorithmVersion, keysize );
encryptedData = encrypt( dataToEncrypt, PBKDFKey, encryptionAlgorithm, "BASE64" );
decryptedData = decrypt( encryptedData, PBKDFKey, encryptionAlgorithm, "BASE64" );
// Output
writeOutput( "<b>Generated PBKDFKey (Base 64)</b>: " & PBKDFKey );
writeOutput( "<br /><b>Data After Encryption</b>: " & encryptedData );
writeOutput( "<br /><b>Data After Decryption</b>: " & decryptedData );

```

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJxLKc0t0FBIT81LLUosSQ1w8nZx806t1FBQAjONwjNLMjxyE5ODPRwNlXQUlIpTk4tSS8CsxJySSiDD1MDAQEfB0MhCQVNB05pLQV9fIdLA17nAOdW4KsnA2c%2BrrDLCLzTcNdDWlgsACv4fBw%3D%3D)

```java
dump( generatePBKDFKey( "PBKDF2WithHmacSHA1", "secret", "salty", 5000, 128 ) );
 // Y0MCpCe3zb0CNJvyXNUWEQ==

```

## Related

* [Decrypt](/boxlang-language/reference/built-in-functions/encryption/decrypt.md)
* [DecryptBinary](/boxlang-language/reference/built-in-functions/encryption/decryptbinary.md)
* [Encrypt](/boxlang-language/reference/built-in-functions/encryption/encrypt.md)
* [EncryptBinary](/boxlang-language/reference/built-in-functions/encryption/encryptbinary.md)
* [GenerateSecretKey](/boxlang-language/reference/built-in-functions/encryption/generatesecretkey.md)
* [Hash](/boxlang-language/reference/built-in-functions/encryption/hash.md)
* [Hash40](/boxlang-language/reference/built-in-functions/encryption/hash40.md)
* [Hmac](/boxlang-language/reference/built-in-functions/encryption/hmac.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/encryption/generatepbkdfkey.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
