This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging

1. Introduction

DES is a Data Encryption Standard (DES) algorithm. Officially licensed by the United States government in 1977, is a method of encrypting 64-bit data using a 56-bit key. A normal password is 8 bytes in length with a 56-bit encryption key, with each eighth bit used for parity. DES algorithm generally has two key points, the first is the encryption algorithm, the second is the data complement. The common encryption algorithms are ECB mode and CBC mode.

The ECB mode: ECB (electronic secret method) is actually very simple, is the data in accordance with 8 bytes a paragraph of DES encryption or decrypt to get a paragraph of 8 bytes of ciphertext or plaintext, the last paragraph is less than 8 bytes (generally complement 0 or F), according to the need to make up 8 bytes for calculation (parallel calculation), After that, the calculated data can be connected together according to the sequence, and each section of data does not affect each other.

Advantages: simple, conducive to parallel computing, error will not be transmitted;

Disadvantages: can’t hide the plaintext mode, may attack the plaintext actively;

CBC mode: Ciphertext group link mode. It is troublesome. The encryption steps are as follows:

  1. The data is first grouped into groups of 8 bytes to get D1D2…… Dn (if the data is not an integer multiple of 8, data complement is involved);
  2. The first set of data D1 and the result after the xOR of vector I are encrypted with DES to get the first set of ciphertext C1 (note: there is a term for vector I here, but vector I is not used in ECB mode).
  3. The second group of data D2 is encrypted with the encryption result C1 of the first group, and the later result is encrypted with DES to get the second group of ciphertext C2.
  4. And then you go on and on and on, and you get Cn
  5. Connect to C1C2C3…… in sequence Cn is the encryption result;

Advantages: It is not easy to be attacked, has better security than ECB, and is the standard of SSL and IPSec.

Disadvantages: adverse to parallel computing, error transfer, need to initialize vector IV;

2. Implement DES encryption and decryption in Vue

Install the crypto – js

Crypto – js NPM address

npm install crypto-js
Copy the code

2.1 the ECB mode

2.1.1 Encapsulation Method

// utils/des.js

import cryptoJs from 'crypto-js';

// Randomly generate a specified number of hexadecimal keys (this method is not required, you can also specify your own keys)
const generatekey = (num) = > {
  let library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let key = "";
  for (var i = 0; i < num; i++) {
    let randomPoz = Math.floor(Math.random() * library.length);
    key += library.substring(randomPoz, randomPoz + 1);
  }
  return key;
}
/* * message: the string to be decrypted, * key: the key (the same encryption and decryption keys) */
/ / DES encryption
const encryptDes = (message, key = 'abcd@1234') = > {
  var keyHex = cryptoJs.enc.Utf8.parse(key)
  var option = { mode: cryptoJs.mode.ECB, padding: cryptoJs.pad.Pkcs7 }
  var encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
  return encrypted.ciphertext.toString(); // Return the ciphertext in hex format, or base64 format if required: encrypted. ToString ()
}


/ / DES declassified
const decryptDes = (message, key = 'abcd@1234') = > {
  var keyHex = cryptoJs.enc.Utf8.parse(key)
  var decrypted = cryptoJs.DES.decrypt(
    {
      ciphertext: cryptoJs.enc.Hex.parse(message)
    }, // If message is in base64 format, pass the message directly without going to hex
    keyHex,
    {
      mode: cryptoJs.mode.ECB,
      padding: cryptoJs.pad.Pkcs7
    }
  )
  return decrypted.toString(cryptoJs.enc.Utf8)
}

export {
  generatekey,
  encryptDes,
  decryptDes,
}
Copy the code

2.1.2 Introduction

<script>
import { encryptDes, decryptDes,generatekey } from '@/utils/des.js'

export default {
  data() {
    return {
      info: {userName:'test'.passWord:'Hi@123'}}; },methods: {
    test(){
      const key = generatekey(8); Let key = 'des';
      
      // If it is an object/array, you need to convert json. stringify to a string first
      const encrypts = encryptDes(JSON.stringify(this.info), key);
      const dess = JSON.parse(decryptDes(encrypts, key));

      console.log(encrypts, dess)
    }
  },
  mounted() {
    this.test(); }}; </script>Copy the code

2.2 CBC mode

2.2.1 Encapsulation method

// utils/des.js

import cryptoJs from 'crypto-js';


// Randomly generate a specified number of hexadecimal keys (this method is not required; you can also specify a fixed key value)
const generatekey = (num) = > {
  let library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let key = "";
  for (var i = 0; i < num; i++) {
    let randomPoz = Math.floor(Math.random() * library.length);
    key += library.substring(randomPoz, randomPoz + 1);
  }
  return key;
}

/* * message: text to be encrypted/decrypted. Key: key to be encrypted/decrypted. Iv: offset, minimum 8 digits

/ / encryption
const encrypt =  (message, key = 'abcd@1234', iv = 'hello world') = > {
  const keyHex = cryptoJs.enc.Utf8.parse(key); / / the secret key
  const ivHex = cryptoJs.enc.Utf8.parse(iv); / / the offset
  const option = { iv: ivHex, mode: cryptoJs.mode.CBC, padding: cryptoJs.pad.Pkcs7 }; // Pkcs7 fill mode
  const encrypted = cryptoJs.DES.encrypt(message, keyHex, option);
  return encrypted.ciphertext.toString() // The encrypted text is in hex format
}

/ / decryption
const decrypt = (message, key = 'abcd@1234', iv = 'hello world') = > {
  const keyHex = cryptoJs.enc.Utf8.parse(key)
  const ivHex = cryptoJs.enc.Utf8.parse(iv)
  const decrypted = cryptoJs.DES.decrypt({
    ciphertext: cryptoJs.enc.Hex.parse(message)
  }, keyHex, {
    iv: ivHex,
    mode: cryptoJs.mode.CBC,
    padding: cryptoJs.pad.Pkcs7
  })
  return decrypted.toString(cryptoJs.enc.Utf8)
}

export {
  generatekey,
  encrypt,
  decrypt,
}
Copy the code

2.2.2 Introduction

<script>
import { encrypt, decrypt, generatekey } from '@/utils/des.js'

export default {
  data() {
    return {
      info: {userName:'test'.passWord:'Hi@123'}}; },methods: {
    test(){
      const key = generatekey(8); // Let key = 'des';
      
      // If it is an object/array, you need to convert json. stringify to a string first
      const encrypts = encrypt(JSON.stringify(this.info), key, '12345678');
      const dess = JSON.parse(decrypt(encrypts, key, '12345678'));

      console.log(encrypts, dess)
    }
  },
  mounted() {
    this.test(); }}; </script>Copy the code