> 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/generatesecretkey.md).

# GenerateSecretKey

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

## Method Signature

```
GenerateSecretKey(algorithm=[string], keySize=[numeric])
```

### Arguments

| Argument    | Type      | Required | Description                                                                                                                                                                | Default |
| ----------- | --------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `algorithm` | `string`  | `false`  | <p>The algorithm to use for generating the key. The default<br>is AES. Example values are: AES, DES, DESede, Blowfish,<br>HmacSHA1, HmacSHA256, HmacSHA384, HmacSHA512</p> | `AES`   |
| `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

### Generate an AES 128 bit Key

Generate an AES key and use it to encrypt and decrypt a secret.

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

```java
ex = {};
ex.KEY = generateSecretKey( "AES" );
ex.SECRET = "top secret";
ex.ENCRYPTED = encrypt( ex.SECRET, ex.KEY, "AES", "Base64" );
ex.DECRYPTED = decrypt( ex.ENCRYPTED, ex.KEY, "AES", "Base64" );
writeDump( ex );

```

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJxLrVCwVaiuteZKrdBz9HH3B%2FKUgpxNlMACYY4%2Boa4gEVNTE0MjI4igt2skUMg9NS%2B1KLEkNTg1uSi1xDu1UkMBZoImWJmrnzNQmWteclFlQQlYEmyajgLECB005S6uIOUuqQjlQAOwKk4pzS0AKQCxAaeBNOk%3D)

```java
ex = {};
ex.ALGO = "RC4";
ex.VALUE = "554122";
ex.KEY = GenerateSecretKey( ex.ALGO );
ex.ENC = Encrypt( ex.VALUE, ex.KEY, ex.ALGO );
ex.DEC = Decrypt( ex.ENC, ex.KEY, ex.ALGO );
dump( ex );

```

## 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)
* [GeneratePBKDFKey](/boxlang-language/reference/built-in-functions/encryption/generatepbkdfkey.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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
