Implementação de Criptografia AES e Codificação Base64 em JavaScript

Criptografia AES

Criptografar

Chave e vetor de inicialização (IV) podem ser personalizados, mas devem ser idênticos durante a criptograifa e descriptografia.

import CryptoJS from "crypto-js";

function criptografarDados(texto, chave = 'chaveSecretaPadrao123') {
  const chaveProcessada = CryptoJS.enc.Utf8.parse(chave);
  
  if (typeof texto === 'object') {
    texto = JSON.stringify(texto);
  }

  const resultado = CryptoJS.AES.encrypt(texto, chaveProcessada, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });

  return resultado.toString();
}

Descriptografar

function descriptografarDados(textoCifrado, chave = 'chaveSecretaPadrao123') {
  const chaveProcessada = CryptoJS.enc.Utf8.parse(chave);
  
  const bytesDecifrados = CryptoJS.AES.decrypt(textoCifrado, chaveProcessada, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });

  return bytesDecifrados.toString(CryptoJS.enc.Utf8);
}

Codificação Base64

class CodificadorBase64 {
  constructor() {
    this.alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  }

  codificar(entrada) {
    let saida = "";
    let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    let i = 0;
    
    entrada = this._converterParaUTF8(entrada);
    
    while (i < entrada.length) {
      chr1 = entrada.charCodeAt(i++);
      chr2 = entrada.charCodeAt(i++);
      chr3 = entrada.charCodeAt(i++);
      
      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;
      
      if (isNaN(chr2)) enc3 = enc4 = 64;
      else if (isNaN(chr3)) enc4 = 64;
      
      saida += this.alfabeto.charAt(enc1) + this.alfabeto.charAt(enc2) +
               this.alfabeto.charAt(enc3) + this.alfabeto.charAt(enc4);
    }
    
    return saida;
  }

  decodificar(entrada) {
    let saida = "";
    let enc1, enc2, enc3, enc4;
    let i = 0;
    
    entrada = entrada.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    
    while (i < entrada.length) {
      enc1 = this.alfabeto.indexOf(entrada.charAt(i++));
      enc2 = this.alfabeto.indexOf(entrada.charAt(i++));
      enc3 = this.alfabeto.indexOf(entrada.charAt(i++));
      enc4 = this.alfabeto.indexOf(entrada.charAt(i++));
      
      saida += String.fromCharCode(
        (enc1 << 2) | (enc2 >> 4),
        ((enc2 & 15) << 4) | (enc3 >> 2),
        ((enc3 & 3) << 6) | enc4
      ).replace(/\0+$/, '');
    }
    
    return this._converterDeUTF8(saida);
  }

  _converterParaUTF8(texto) {
    texto = texto.replace(/\r\n/g, "\n");
    let utftext = "";
    
    for (let n = 0; n < texto.length; n++) {
      const c = texto.charCodeAt(n);
      
      if (c < 128) utftext += String.fromCharCode(c);
      else if (c > 127 && c < 2048) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      } else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
    }
    
    return utftext;
  }

  _converterDeUTF8(utftext) {
    let texto = "";
    let i = 0;
    
    while (i < utftext.length) {
      const c = utftext.charCodeAt(i);
      
      if (c < 128) {
        texto += String.fromCharCode(c);
        i++;
      } else if (c > 191 && c < 224) {
        const c2 = utftext.charCodeAt(i + 1);
        texto += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      } else {
        const c2 = utftext.charCodeAt(i + 1);
        const c3 = utftext.charCodeAt(i + 2);
        texto += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
    }
    
    return texto;
  }
}

Tags: AES Base64 javascript criptografia Codificação

Publicado em 7-16 08:12