or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

address.mdbase-encoding.mdcrypto-init.mdethereum.mdhashing.mdindex.mdjson-encryption.mdkey-derivation-pbkdf.mdkey-derivation.mdkeypairs.mdmnemonic.mdrandom.mdsignatures.md

json-encryption.mddocs/

0

# JSON Encryption

1

2

Secure JSON encryption and decryption with password protection, supporting multiple encoding formats and versions for secure data storage and transmission.

3

4

## Capabilities

5

6

### JSON Encryption

7

8

Encrypt data to password-protected JSON format.

9

10

```typescript { .api }

11

/**

12

* Encrypt data to JSON format with password protection

13

* @param data - Data bytes to encrypt

14

* @param contentType - Array describing content type

15

* @param passphrase - Password for encryption

16

* @returns Encrypted JSON object

17

*/

18

function jsonEncrypt(data: Uint8Array, contentType: string[], passphrase: string): EncryptedJson;

19

20

/**

21

* Format encrypted data as JSON

22

* @param encoded - Encoded encrypted data

23

* @param contentType - Optional content type array

24

* @returns Formatted encrypted JSON

25

*/

26

function jsonEncryptFormat(encoded: string, contentType?: string[]): EncryptedJson;

27

```

28

29

### JSON Decryption

30

31

Decrypt password-protected JSON data.

32

33

```typescript { .api }

34

/**

35

* Decrypt encrypted JSON with passphrase

36

* @param json - Encrypted JSON object

37

* @param passphrase - Password for decryption

38

* @returns Decrypted data bytes

39

*/

40

function jsonDecrypt(json: EncryptedJson, passphrase: string): Uint8Array;

41

42

/**

43

* Decrypt encrypted data directly

44

* @param encrypted - Encrypted data object

45

* @param passphrase - Password for decryption

46

* @returns Decrypted data bytes

47

*/

48

function jsonDecryptData(encrypted: EncryptedJson, passphrase: string): Uint8Array;

49

```

50

51

## Types

52

53

```typescript { .api }

54

type EncryptedJsonVersion = '0' | '1' | '2' | '3';

55

type EncryptedJsonEncoding = 'none' | 'scrypt' | 'xsalsa20-poly1305';

56

57

interface EncryptedJsonDescriptor {

58

/** Descriptor for the content */

59

content: string[];

60

/** The encoding (in current/latest versions this is always an array) */

61

type: EncryptedJsonEncoding | EncryptedJsonEncoding[];

62

/** The version of encoding applied */

63

version: EncryptedJsonVersion;

64

}

65

66

interface EncryptedJson {

67

/** The encoded string */

68

encoded: string;

69

/** The encoding used */

70

encoding: EncryptedJsonDescriptor;

71

}

72

```

73

74

## Usage Example

75

76

```typescript

77

import { jsonEncrypt, jsonDecrypt } from "@polkadot/util-crypto";

78

79

// Data to encrypt

80

const secretData = new TextEncoder().encode("sensitive information");

81

const contentType = ["pkcs8", "sr25519"];

82

const passphrase = "strong-password";

83

84

// Encrypt data

85

const encrypted = jsonEncrypt(secretData, contentType, passphrase);

86

console.log(encrypted.encoding.version); // Current version

87

console.log(encrypted.encoding.type); // Encoding methods used

88

89

// Decrypt data

90

const decrypted = jsonDecrypt(encrypted, passphrase);

91

const decryptedText = new TextDecoder().decode(decrypted);

92

console.log(decryptedText); // "sensitive information"

93

```

94

95

## Security Features

96

97

- **Password-based encryption** using Scrypt key derivation

98

- **XSalsa20-Poly1305** authenticated encryption

99

- **Version support** for format evolution

100

- **Content type metadata** for proper handling

101

- **Salt and nonce generation** for unique encryptions