美文网首页
分析各大常用的JS加密优缺点

分析各大常用的JS加密优缺点

作者: 麻瓜三号 | 来源:发表于2023-09-17 22:57 被阅读0次
  1. Base64编码:

    • 优点:

      • 简单,易于实现。

      • 不是真正的加密,只是编码,可以用于数据传输和存储。

    • 缺点:

      • 不提供数据保密性,容易被解码。
    • 示例代码:

      <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="js" cid="n221" mdtype="fences" style="box-sizing: border-box; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgb(59 130 246 / 0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">// 编码
      const encodedData = btoa('Hello, World!');
      console.log(encodedData);

      // 解码
      const decodedData = atob(encodedData);
      console.log(decodedData);</pre>

  2. 哈希函数:

    • 优点:

      • 提供数据完整性验证。

      • 相同输入始终生成相同的哈希值。

    • 缺点:

      • 不可逆,无法还原原始数据。

      • 容易受到彩虹表攻击。

    • 示例代码: 使用JavaScript的crypto.subtle来计算SHA-256哈希值的示例代码:

      <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="js" cid="n241" mdtype="fences" style="box-sizing: border-box; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgb(59 130 246 / 0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">async function calculateSHA256Hash(data) {
      const encoder = new TextEncoder();
      const dataBuffer = encoder.encode(data);
      const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer);
      const hashArray = Array.from(new Uint8Array(hashBuffer));
      const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
      return hashHex;
      }

      const originalData = 'Hello, World!';
      calculateSHA256Hash(originalData).then(hash => {
      console.log(hash); // SHA-256哈希值
      });</pre>

  3. 对称加密:

    • 优点:

      • 加密和解密速度快。

      • 适用于大量数据加密。

    • 缺点:

      • 密钥管理可能复杂。

      • 需要安全地传输密钥。

    • 示例代码: 使用Web Crypto API进行AES加密的示例代码:

      <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="js" cid="n261" mdtype="fences" style="box-sizing: border-box; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgb(59 130 246 / 0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">async function encryptData(data, key) {
      const encoder = new TextEncoder();
      const dataBuffer = encoder.encode(data);
      const encryptedData = await crypto.subtle.encrypt({ name: 'AES-GCM', iv: new Uint8Array(12) }, key, dataBuffer);
      return new Uint8Array(encryptedData);
      }

      async function decryptData(encryptedData, key) {
      const decryptedData = await crypto.subtle.decrypt({ name: 'AES-GCM', iv: new Uint8Array(12) }, key, encryptedData);
      const decoder = new TextDecoder();
      return decoder.decode(decryptedData);
      }

      // 生成随机AES密钥
      crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt']).then(key => {
      const originalData = 'Hello, World!';
      encryptData(originalData, key)
      .then(encryptedData => decryptData(encryptedData, key))
      .then(decryptedData => {
      console.log(decryptedData); // 解密后的数据
      });
      });</pre>

  4. 非对称加密:

    • 优点:

      • 安全性高,一个密钥用于加密,另一个用于解密。

      • 适用于安全通信和数字签名。

    • 缺点:

      • 加密和解密速度相对较慢。

      • 密钥管理复杂。

    • 示例代码: 使用Node.js中的crypto模块执行RSA加密的示例代码:

      <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="js" cid="n281" mdtype="fences" style="box-sizing: border-box; --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-pan-x: ; --tw-pan-y: ; --tw-pinch-zoom: ; --tw-scroll-snap-strictness: proximity; --tw-ordinal: ; --tw-slashed-zero: ; --tw-numeric-figure: ; --tw-numeric-spacing: ; --tw-numeric-fraction: ; --tw-ring-inset: ; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgb(59 130 246 / 0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; --tw-blur: ; --tw-brightness: ; --tw-contrast: ; --tw-grayscale: ; --tw-hue-rotate: ; --tw-invert: ; --tw-saturate: ; --tw-sepia: ; --tw-drop-shadow: ; --tw-backdrop-blur: ; --tw-backdrop-brightness: ; --tw-backdrop-contrast: ; --tw-backdrop-grayscale: ; --tw-backdrop-hue-rotate: ; --tw-backdrop-invert: ; --tw-backdrop-opacity: ; --tw-backdrop-saturate: ; --tw-backdrop-sepia: ; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">const crypto = require('crypto');

      // 生成RSA密钥对
      const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
      modulusLength: 2048,
      publicKeyEncoding: {
      type: 'spki',
      format: 'pem',
      },
      privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem',
      },
      });

      const originalData = 'Hello, World!';

      // 加密
      const encryptedData = crypto.publicEncrypt(publicKey, Buffer.from(originalData));
      console.log(encryptedData.toString('base64'));

      // 解密
      const decryptedData = crypto.privateDecrypt(privateKey, encryptedData);
      console.log(decryptedData.toString());</pre>

  5. JS在线加密

js在线加密是一款国内顶尖的js保护方案,已经有许多客户使用。同时配备了一键在线解密其他简单加密的功能.

相关文章

  • 常用的加解密算法的优缺点、应用场景总结

    常用的加解密算法的优缺点、应用场景总结 一、加解密的基础知识 1、对称密钥加密 对称密钥加密(一个密钥),也叫做共...

  • 0.R语言学习前期准备

    重点摘要:数据分析的原则,步骤; 常用数据分析的工具,数据挖掘的工具; R语言的优缺点; ...

  • 常用js数据加密

    1.base64加密 (1)introduction base64是网络上最常见的用于传输8bit字节码的编码方式...

  • iOS - 程序加固APP安全防护

    常用的加固方式 URL编码加密 对iOS app中出现的URL进行编码加密,防止URL被静态分析 本地数据加密 对...

  • 加密方式方法总结

    加密方式有哪几种 每种加密方式的优缺点 怎么根据需要选择加密方式

  • 用到的模块

    mongoose操作mongodb utility一些加密工具,常用工具 log4js日志工具

  • 最新python模拟登录知乎

    步骤: 第一步:抓包查看登陆接口 第二步:分析js文件,提取加密请求参数的js脚本 第三步:分析js文件,构造需要...

  • iOS代码静态分析工具选择

    本文对比iOS常用的几种静态分析工具,分析优缺点,并从中选出适合当前工程的工具,本文的应用系统为macOS。 cl...

  • 常用的加密方式

    常用的加密方法: MD5加密 AES加密 BASE64加密 常用加密的地方: 1)本地数据加密 对NSUserDe...

  • 会python真的可以为所欲为——爆破前端加密登录

    做安全测试的时候经常会遇到前端登录数据包加密,又懒得去分析js看加密算法,特别一些做了混淆的,分析起来那叫一个恶心...

网友评论

      本文标题:分析各大常用的JS加密优缺点

      本文链接:https://www.haomeiwen.com/subject/wwowvdtx.html