DES-CBC

August 24, 2019 ยท View on GitHub

WARNING: This is not compliant with the W3 WebCrypto specification.

Interfaces

DesCbcParams interface

interface DesCbcParams {
    // The initialization vector. MUST be 8 bytes.
    iv: BufferSource;
}

Operations

OperationParametersResult
generateKeyDesKeyGenParamsCryptoKey
importKeyNoneCryptoKey
exportKeyNoneJsonWebKey or BufferSource
encryptDesCbcParamsArrayBuffer
decryptDesCbcParamsArrayBuffer
wrapKeyDesCbcParamsArrayBuffer
unwrapKeyDesCbcParamsCryptoKey

Generate key

const key = await crypto.subtle.generateKey(
  {
    name: "DES-CBC",
    length: 64, // 64-bit only
  },
  false, // extractable
  ["encrypt", "decrypt", "wrapKey", "unwrapKey"], // key usages
);

Import key

const key = await crypto.subtle.importKey(
  "raw",   // raw or jwk
  rawData, // BufferSource
  "DES-CBC",
  false,   // extractable
  ["encrypt", "decrypt"],
);

Export key

const raw = await crypto.subtle.exportKey(
  "raw", // raw or jwk
  key,
);

Encrypt

const iv = crypto.getRandomValues(new Uint8Array(8));

const encData = await crypto.subtle.encrypt(
  {
    name: "DES-CBC",
    iv, // BufferSource
  },
  key,  // DES key
  data, // BufferSource
);

Decrypt

const data = await crypto.subtle.decrypt(
  {
    name: "DES-CBC",
    iv, // BufferSource
  },
  key,  // DES key
  encData, // BufferSource
);

Wrap key

const iv = crypto.getRandomValues(new Uint8Array(8));

const wrappedKey = await crypto.subtle.wrapKey(
  "pkcs8",   // raw, pkcs8, spki, or jwk
  anyKey,    // Crypto key
  key,       // DES key
  {
    name: "DES-CBC",
    iv, // BufferSource
  },
);

Unwrap key

const unwrappedKey = await crypto.subtle.unwrapKey(
  "pkcs8",    // raw, pkcs8, spki, or jwk
  wrappedKey, // BufferSource
  key,        // DES key
  {
    name: "DES-CBC",
    iv, // BufferSource
  },
  {
    name: "RSA-PSS",
    hash: "SHA-256",
  }
  false,      // extractable
  ["sign", "verify"],
);