Common Encryption and Decryption Algorithms in JavaScript

Introduction to Chen Shixiong

๐Ÿ‘‹ Programmer Chen Shixiong, a popular Bilibili content creator, teaches in a clear and concise manner.

๐Ÿš€ Previously worked as a senior backend developer at a leading company.

๐Ÿ’ป Proficient in multiple languages including Java, Python, and JavaScript, with a long-term focus on reverse engineering, web scraping, and practical backend development.

๐Ÿ”ฅ In 2021, released the first set of Zero-Basis Practical Course on JS Reverse Engineering on Bilibili, characterized by clarity and practical content without fluff, helping countless students start from scratch and successfully apply it in their work.

Course Features

  • โœ… Clear and Understandable โ€” Eliminates tedious theory, directly addressing core knowledge points.
  • โœ… Practical Content without Fluff โ€” Students generally feedback that “one listening is enough to understand”.
  • โœ… Real Case Driven โ€” Practical summaries from real projects.

๐Ÿ“ฉ Contact Information

  • WeChat:<span>wanzhuancode</span>
  • QQ:<span>3041439327</span>

๐Ÿ”‘ 1. Hash Algorithms (Irreversible)

Commonly used for signatures, verification, and password storage (not for decryption).

  • MD5

    import CryptoJS from "crypto-js";
    
    const hash = CryptoJS.MD5("hello").toString();
    console.log(hash); // 5d41402abc4b2a76b9719d911017c592
    
    • Features: Generates a 32-character hexadecimal string, irreversible.
    • Applications: Password hashing, file integrity verification.
  • SHA Series (SHA-1, SHA-256, SHA-512)

    const sha256 = CryptoJS.SHA256("hello").toString();
    console.log(sha256);
    
    • More secure than MD5.

๐Ÿ” 2. Symmetric Encryption (Encrypt + Decrypt)

Uses the same key for both encryption and decryption, commonly used for local storage and API encryption.

  • AES (Advanced Encryption Standard)

    const secret = "1234567890abcdef"; // 16/24/32 byte key
    
    const ciphertext = CryptoJS.AES.encrypt("hello", secret).toString();
    console.log(ciphertext);
    
    const bytes = CryptoJS.AES.decrypt(ciphertext, secret);
    const original = bytes.toString(CryptoJS.enc.Utf8);
    console.log(original); // hello
    
    • One of the most commonly used symmetric encryption algorithms.
  • DES / 3DES

    const key = "12345678";
    const encrypted = CryptoJS.DES.encrypt("hello", key).toString();
    const decrypted = CryptoJS.DES.decrypt(encrypted, key).toString(CryptoJS.enc.Utf8);
    
    • Previously common, now less secure.
  • RC4

    • Stream cipher, lightweight but not very secure.

๐Ÿ” 3. Asymmetric Encryption

Uses public and private keys for encryption and decryption, commonly used for data transmission security and digital signatures.

  • RSA

    import JSEncrypt from "jsencrypt";
    
    const encryptor = new JSEncrypt();
    encryptor.setPublicKey(`-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----`);
    const encrypted = encryptor.encrypt("hello");
    
    const decryptor = new JSEncrypt();
    decryptor.setPrivateKey(`-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----`);
    const decrypted = decryptor.decrypt(encrypted);
    
    • Public key encryption, private key decryption.
  • ECC (Elliptic Curve Cryptography)

    • More efficient and secure, but complex to implement, commonly found in SSL/TLS.

๐Ÿงพ 4. Encoding (Strictly Not Encryption)

Commonly used for data transmission and obfuscation, not a secure encryption method.

  • Base64

    const str = "hello";
    const encoded = btoa(str);     // aGVsbG8=
    const decoded = atob(encoded); // hello
    
  • Hex Encoding

    const hex = Buffer.from("hello").toString("hex"); // 68656c6c6f
    const back = Buffer.from(hex, "hex").toString();  // hello
    
  • URL Encoding

    const encoded = encodeURIComponent("ไฝ ๅฅฝ");
    const decoded = decodeURIComponent(encoded);
    

๐Ÿ“Œ Summary

  • Hashing (MD5, SHA) โ†’ Only encrypts, irreversible.
  • Symmetric Encryption (AES, DES) โ†’ Same key for encryption and decryption.
  • Asymmetric Encryption (RSA, ECC) โ†’ Public key encryption, private key decryption.
  • Encoding (Base64, URL) โ†’ Used only for transmission and storage, not considered secure encryption.

Previous Articles

What is Wasm?

How to Determine Data Types in JS

JS Constructor Functions

How to Determine Data Types in JS

JS Reverse Engineering Practical – this Binding

Zero-Basis Practical Course on JS Reverse Engineering (JS Objects)

Common Encryption and Decryption Algorithms in JavaScript

JS Reverse Engineering VIP Membership Website

  • All site materials and tools are downloadable

  • VIP articles are free to read

  • Includes courseware, materials, documents, and notes

    Scan to follow

Leave a Comment