Skip to main content

encrypt

Callable


  • encrypt a private key with a password, returns a V3 JSON Keystore

    Read more: https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition


    Parameters

    • privateKey: Bytes

      The private key to encrypt, 32 bytes.

    • password: string | Uint8Array

      The password used for encryption.

    • optionaloptions: CipherOptions

      Options to configure to encrypt the keystore either scrypt or pbkdf2

    Returns Promise<KeyStore>

    Returns a V3 JSON Keystore

    Encrypt using scrypt options:


    web3.eth.accounts.encrypt(
    '0x67f476289210e3bef3c1c75e4de993ff0a00663df00def84e73aa7411eac18a6',
    '123',
    {
    n: 8192,
    iv: web3.utils.hexToBytes('0xbfb43120ae00e9de110f8325143a2709'),
    salt: web3.utils.hexToBytes('0x210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd'),
    }).then(console.log)

    > {
    version: 3,
    id: 'c0cb0a94-4702-4492-b6e6-eb2ac404344a',
    address: 'cda9a91875fc35c8ac1320e098e584495d66e47c',
    crypto: {
    ciphertext: 'cb3e13e3281ff3861a3f0257fad4c9a51b0eb046f9c7821825c46b210f040b8f',
    cipherparams: { iv: 'bfb43120ae00e9de110f8325143a2709' },
    cipher: 'aes-128-ctr',
    kdf: 'scrypt',
    kdfparams: {
    n: 8192,
    r: 8,
    p: 1,
    dklen: 32,
    salt: '210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd'
    },
    mac: 'efbf6d3409f37c0084a79d5fdf9a6f5d97d11447517ef1ea8374f51e581b7efd'
    }
    }

    Encrypting using pbkdf2 options:

    web3.eth.accounts.encrypt('0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709',
    '123',
    {
    iv: 'bfb43120ae00e9de110f8325143a2709',
    salt: '210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd',
    c: 262144,
    kdf: 'pbkdf2',
    }).then(console.log)

    >
    {
    version: 3,
    id: '77381417-0973-4e4b-b590-8eb3ace0fe2d',
    address: 'b8ce9ab6943e0eced004cde8e3bbed6568b2fa01',
    crypto: {
    ciphertext: '76512156a34105fa6473ad040c666ae7b917d14c06543accc0d2dc28e6073b12',
    cipherparams: { iv: 'bfb43120ae00e9de110f8325143a2709' },
    cipher: 'aes-128-ctr',
    kdf: 'pbkdf2',
    kdfparams: {
    dklen: 32,
    salt: '210d0ec956787d865358ac45716e6dd42e68d48e346d795746509523aeb477dd',
    c: 262144,
    prf: 'hmac-sha256'
    },
    mac: '46eb4884e82dc43b5aa415faba53cc653b7038e9d61cc32fd643cf8c396189b7'
    }
    }