46 篇文章 0 订阅

#### 1.1 加密原理

AES 是一种对称加密算法，使用相同的密钥进行加密和解密。以下是一个使用 AES 进行加密和解密的 JavaScript 示例：

const crypto = require('crypto');

// 加密函数
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), Buffer.alloc(16, 0)); // 使用 AES-256-CBC 模式
let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}

// 解密函数
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), Buffer.alloc(16, 0)); // 使用 AES-256-CBC 模式
let decrypted = decipher.update(encryptedData, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}

const key = 'thisisaverysecretkeythatshouldbe32chars!'; // 密钥（必须是32字节）

console.log(Encrypted: ${encrypted}); console.log(Decrypted:${decrypted});


#### 1.2 加密算法拓展

const crypto = require('crypto');

// 3DES 加密函数
function encrypt3DES(data, key) {
const cipher = crypto.createCipheriv('des-ede3-cbc', Buffer.from(key), Buffer.alloc(8, 0)); // 使用 3DES 模式
let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}

// 3DES 解密函数
function decrypt3DES(encryptedData, key) {
const decipher = crypto.createDecipheriv('des-ede3-cbc', Buffer.from(key), Buffer.alloc(8, 0)); // 使用 3DES 模式
let decrypted = decipher.update(encryptedData, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}

const key = 'thisisaverysecretkey'; // 密钥（必须是24字节）
const encrypted = encrypt3DES(data, key);
const decrypted = decrypt3DES(encrypted, key);

console.log(Encrypted: ${encrypted}); console.log(Decrypted:${decrypted});


#### 1.3 加盐与魔改

const crypto = require('crypto');

// 带盐加密函数
function encryptWithSalt(data, key, salt) {
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key + salt), Buffer.alloc(16, 0)); // 使用 AES-256-CBC 模式
let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}

// 带盐解密函数
function decryptWithSalt(encryptedData, key, salt) {
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key + salt), Buffer.alloc(16, 0)); // 使用 AES-256-CBC 模式
let decrypted = decipher.update(encryptedData, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}

const salt = crypto.randomBytes(16).toString('hex'); // 生成随机盐
const key = 'thisisaverysecretkeythatshouldbe32chars!'; // 密钥（必须是32字节）
const encrypted = encryptWithSalt(data, key, salt);
const decrypted = decryptWithSalt(encrypted, key, salt);

console.log(Salt: ${salt}); console.log(Encrypted:${encrypted});
console.log(Decrypted: ${decrypted});  ### 2. 🧩 Cookie 和 Session 机制 CookieSession 是 Web 应用中用于管理用户状态的重要机制。Cookie 存储在客户端，而 Session 存储在服务器端。 #### 2.1 Cookie 机制 Cookie 是存储在浏览器中的小块数据，用于在多个请求中保持用户状态。以下是设置和读取 Cookie 的 JavaScript 示例： // 设置 Cookie function setCookie(name, value, days) { let expires = ""; if (days) { const date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; } // 读取 Cookie function getCookie(name) { const nameEQ = name + "="; const ca = document.cookie.split(';'); for(let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) === ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); } return null; } // 示例使用 setCookie('username', 'JohnDoe', 7); console.log('Username:', getCookie('username'));  #### 2.2 Session 机制 Session 通常由服务器端管理，用于存储与用户相关的信息。每个 Session 通过唯一的标识符来区分。 const express = require('express'); const session = require('express-session'); const app = express(); app.use(session({ secret: 'your-secret-key', resave: false, saveUninitialized: true, cookie: { secure: false } })); app.get('/', (req, res) => { if (req.session.views) { req.session.views++; res.send(Views:${req.session.views});
} else {
req.session.views = 1;
res.send('Welcome! This is your first visit.');
}
});

app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});


#### 3.1 钩子技巧

const axios = require('axios');

// 创建拦截器
axios.interceptors.request.use(config => {
return config;
}, error => {
return Promise.reject(error);
});

// 发起请求
axios.get('https://example.com')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});


#### 3.2 钩子处理重定向

const axios = require('axios');

// 发起请求并处理重定向
axios({
method: 'get',
url: 'https://example.com/redirect',
maxRedirects: 0 // 禁用自动重定向
}).then(response => {
}).catch(error => {
if (error.response && error.response.status === 302) {
// 处理重定向响应
return axios.get(location); // 发起重定向请求
} else {
console.error('Request failed:', error);
}
}).then(finalResponse => {
console.log('Final Response:', finalResponse.data);
});


### 4. 🧩 scw_sc_v2 调试

const crypto = require('crypto');

// scw_sc_v2 加密函数
function encryptScwScV2(data, key) {
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), Buffer.alloc(16, 0)); // 使用 AES-256-CBC 模式
let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}

// scw_sc_v2 解密函数
function decryptScwScV2(encryptedData, key) {
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), Buffer.alloc(16, 0)); // 使用 AES-256-CBC 模式
let decrypted = decipher.update(encryptedData, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}

const key = 'thisisaverysecretkeythatshouldbe32chars!'; // 密钥（必须是32字节）
const encrypted = encryptScwScV2(data, key);
const decrypted = decryptScwScV2(encrypted, key);

console.log(Encrypted: ${encrypted}); console.log(Decrypted:${decrypted});


### 5. 🧩 jsl Cookie 调试

jsl 是一种 Web 加密算法，常用于 Cookie 数据的加密和解密。以下是调试 jsl 加密的示例：

const crypto = require('crypto');

// jsl 加密函数
function encryptJsl(data, key) {
const cipher = crypto.createCipheriv('aes-128-cbc', Buffer.from(key), Buffer.alloc(16, 0)); // 使用 AES-128-CBC 模式
let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}

// jsl 解密函数
function decryptJsl(encryptedData, key) {
const decipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(key), Buffer.alloc(16, 0)); // 使用 AES-128-CBC 模式
let decrypted = decipher.update(encryptedData, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}

const key = 'thisisaverysecretkey'; // 密钥（必须是16字节）
const encrypted = encryptJsl(data, key);
const decrypted = decryptJsl(encrypted, key);

console.log(Encrypted: ${encrypted}); console.log(Decrypted:${decrypted});


### 6. 🔄 重定向 Cookie 调试

const axios = require('axios');

// 发起请求并跟踪重定向
axios({
method: 'get',
url: 'https://example.com/redirect',
maxRedirects: 0 // 禁用自动重定向
}).then(response => {
}).catch(error => {
if (error.response && error.response.status === 302) {
console.log('Redirect Location:', location);
return axios.get(location); // 发起重定向请求
} else {
console.error('Request failed:', error);
}
}).then(finalResponse => {
console.log('Final Response:', finalResponse.data);
});


• 18
点赞
• 17
收藏
觉得还不错? 一键收藏
• 打赏
• 1
评论
11-11 2169
03-23 2107
07-11 460
09-10 1354
06-11 7627
10-25 5596
01-07
07-23 4572
03-23 4884
01-18 319
12-13 5043

Switch616

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。