Skip to content

Latest commit

 

History

History
119 lines (102 loc) · 2.75 KB

DES_CBC.md

File metadata and controls

119 lines (102 loc) · 2.75 KB

DES-CBC

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

Operation Parameters Result
generateKey DesKeyGenParams CryptoKey
importKey None CryptoKey
exportKey None JsonWebKey or BufferSource
encrypt DesCbcParams ArrayBuffer
decrypt DesCbcParams ArrayBuffer
wrapKey DesCbcParams ArrayBuffer
unwrapKey DesCbcParams CryptoKey

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"],
);