---
title: "decrypt | Grafana k6 documentation"
description: "decrypt decrypts some encrypted data"
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# decrypt

The `decrypt()` method decrypts some encrypted data.

## Usage

![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```none
decrypt(algorithm, key, data)
```

## Parameters

Expand table

| Name        | Type                                                                                                                                                                                                                    | Description                                                                                                                                                  |
|-------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `algorithm` | [AesCbcParams](/docs/k6/latest/javascript-api/crypto/aescbcparams/), [AesCtrParams](/docs/k6/latest/javascript-api/crypto/aesctrparams/), or [AesGcmParams](/docs/k6/latest/javascript-api/crypto/aesgcmparams/) object | Defines the algorithm to use and any extra-parameters. The values given for the extra parameters must match those used in the corresponding \[encrypt] call. |
| `key`       | [CryptoKey](/docs/k6/latest/javascript-api/crypto/cryptokey/)                                                                                                                                                           | The [key](/docs/k6/latest/javascript-api/crypto/cryptokey/) to use for decryption.                                                                           |
| `data`      | `ArrayBuffer`, `TypedArray`, or `DataView`                                                                                                                                                                              | The encrypted data to be decrypted (also known as *ciphertext*).                                                                                             |

### Supported algorithms

Expand table

| AES-CBC                                                               | AES-CTR                                                               | AES-GCM                                                               | RSA-OAEP                                                                |
|-----------------------------------------------------------------------|-----------------------------------------------------------------------|-----------------------------------------------------------------------|-------------------------------------------------------------------------|
| ✅ [AesCbcParams](/docs/k6/latest/javascript-api/crypto/aescbcparams/) | ✅ [AesCtrParams](/docs/k6/latest/javascript-api/crypto/aesctrparams/) | ✅ [AesGcmParams](/docs/k6/latest/javascript-api/crypto/aesgcmparams/) | ✅ [RsaOaepParams](/docs/k6/latest/javascript-api/crypto/rsaoaepparams/) |

## Return Value

A `Promise` that resolves to a new `ArrayBuffer` containing the decrypted data.

## Throws

Expand table

| Type                 | Description                                                                                                                                                                                  |
|----------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `InvalidAccessError` | Raised when the requested operation is not valid with the provided key. For instance when an invalid encryption algorithm is used, or a key not matching the selected algorithm is provided. |
| `OperationError`     | Raised when the operation failed for an operation-specific reason. For instance, if the algorithm size is invalid, or errors occurred during the process of decrypting the ciphertext.       |

## Example

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
export default async function () {
  const plaintext = stringToArrayBuffer('Hello, World!');

  /**
   * Generate a symmetric key using the AES-CBC algorithm.
   */
  const key = await crypto.subtle.generateKey(
    {
      name: 'AES-CBC',
      length: 256,
    },
    true,
    ['encrypt', 'decrypt']
  );

  /**
   * Encrypt the plaintext using the AES-CBC key with
   * have generated.
   */
  const iv = crypto.getRandomValues(new Uint8Array(16));
  const ciphertext = await crypto.subtle.encrypt(
    {
      name: 'AES-CBC',
      iv: iv,
    },
    key,
    plaintext
  );

  /**
   * Decrypt the ciphertext using the same key to verify
   * that the resulting plaintext is the same as the original.
   */
  const deciphered = await crypto.subtle.decrypt(
    {
      name: 'AES-CBC',
      iv: iv,
    },
    key,
    ciphertext
  );

  console.log(
    'deciphered text == original plaintext: ',
    arrayBufferToHex(deciphered) === arrayBufferToHex(plaintext)
  );
}

function arrayBufferToHex(buffer) {
  return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('');
}

function stringToArrayBuffer(str) {
  const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
  const bufView = new Uint16Array(buf);
  for (let i = 0, strLen = str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}
```
