当前位置: 首页 > news >正文

用Java NIO模拟HTTPS

HTTPS流程 

名词解释:
    R1:随机数1 R2:随机数2 R3:随机数3 publicKey:公钥 privateKey:私钥
step1 
客户端 client hello + R1  
服务端 server hello + R2 + publicKey(验证证书,证书包含公钥)
step2
客户端 R3 publicKey加密
服务端 privateKey解密
step3
客户端与服务端使用相同的对称密钥算法生成会话密钥
客户端 R3 + R1 + R2 -> 生成会话密钥
服务端 R3 + R1 + R2 -> 生成会话密钥
step4
正式通信 对称密钥(会话密钥)加密数据
 

HttpsServer


import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;public class HttpsServer {private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);private static Map<String, Object> keyMap;private static final byte[] randomList = new byte[3];private static volatile Map<SocketChannel, Boolean> channelBooleanMap = new ConcurrentHashMap<>();public static void main(String[] args) throws Exception {keyMap = init();scheduler.scheduleAtFixedRate(() -> {channelBooleanMap.forEach((k, v) -> {if (!v) {channelBooleanMap.remove(k);}});}, 0, 1, TimeUnit.SECONDS);startServer();}public static Map<String, Object> init() throws NoSuchAlgorithmException {Map<String, Object> map = EncryptUtil.initKey();return Collections.unmodifiableMap(map);}public static void startServer() throws Exception {ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();serverSocketChannel.configureBlocking(false);serverSocketChannel.bind(new InetSocketAddress(8080));Selector selector = Selector.open();serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);System.out.println("服务器监听....");while (true) {int select = selector.select();if (select > 0) {Set<SelectionKey> selectionKeys = selector.selectedKeys();Iterator<SelectionKey> iterator = selectionKeys.iterator();while (iterator.hasNext()) {SelectionKey key = iterator.next();iterator.remove();if (key.isValid()) {if (key.isAcceptable()) {ServerSocketChannel channel = (ServerSocketChannel) key.channel();SocketChannel socketChannel = channel.accept();System.out.println("连接:" + socketChannel.getRemoteAddress());socketChannel.configureBlocking(false);channelBooleanMap.put(socketChannel, true);// 为每个连接到的channel分配一个缓冲区,channel间互相隔离ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);socketChannel.register(selector, SelectionKey.OP_READ, buffer);}if (key.isReadable()) {SocketChannel channel = (SocketChannel) key.channel();try {ByteBuffer buffer = ByteBuffer.allocate(1024);byte b;int read, count = 0;while (true) {if ((read = channel.read(buffer)) > 0) {count += read;System.out.println("count:" + count);}if (count == 14) {buffer.flip();break;}if (count == 65) {buffer.flip();break;}if (count == 32) { // 正式通信buffer.flip();break;}}b = buffer.get(0);if (b == 13) { // 第一次同步消息byte[] array = buffer.array();System.out.println(new String(array, 1, 12));byte r1 = array[13]; // 随机数1 客户端发给服务端System.out.println("随机数1:" + r1); // 大小端randomList[0] = r1;} else if (b == 64) { // 第二次同步消息byte[] array = buffer.array();byte[] data = new byte[b];System.arraycopy(array, 1, data, 0, b);byte[] bytes = EncryptUtil.decryptByPrivateKey(data, EncryptUtil.getPrivateKey(keyMap));System.out.println("随机数3=" + bytes[0]);randomList[2] = bytes[0];System.out.println("randomList:" + Arrays.toString(randomList));// 生成会话密钥byte[] sessionKey = EncryptUtil.hmacSHA256(EncryptUtil.HmacSHA256_key.getBytes(), randomList);SetCache.add(sessionKey);System.out.println("会话密钥:" + Arrays.toString(sessionKey));} else { // 正式通信byte[] array = new byte[32];buffer.get(array);System.out.println("array=" + Arrays.toString(array));if (Arrays.compare(array, SetCache.get()) == 0) {System.out.println("会话密钥验证成功");} else {System.out.println("会话密钥验证失败");}}} catch (Exception e) {channelBooleanMap.put(channel, false);key.cancel();channel.close();System.out.println("有连接关闭...");break;}System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");// 注册事件:可能会触发多余的写事件channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
//                            if (flag) { // 通信结束标识
//                                channel.register(selector, SelectionKey.OP_READ);
//                            } else {
//                                channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
//                            }}if (key.isWritable()) {System.out.println("触发写事件....");SocketChannel channel = (SocketChannel) key.channel();ByteBuffer buffer = ByteBuffer.allocate(1024);String serverHello = "Server Hello";buffer.put(serverHello.getBytes());byte b = (byte) new Random().nextInt(Byte.MAX_VALUE);randomList[1] = b; // 随机数2 服务端发送给客户端buffer.put(b);// 发送公钥给客户端byte[] publicKey = EncryptUtil.getPublicKey(keyMap);byte len = (byte) publicKey.length;System.out.println("publicKey.length:" + len);buffer.put(len);buffer.put(publicKey);// 注意:往channel中写缓冲区前,必须切换到读模式,否则无法触发读事件buffer.flip();if (!channelBooleanMap.get(channel)) {System.out.println("通道已关闭...");channel.register(selector, key.interestOps() & ~SelectionKey.OP_WRITE);break;}channel.write(buffer);//                            channel.socket().getOutputStream().flush();//                            channel.write(ByteBuffer.wrap(serverHello.getBytes()));
//                            channel.write(ByteBuffer.wrap(new byte[]{b}));System.out.println(Arrays.toString(buffer.array()));System.out.println("随机数2:" + b);channel.register(selector, key.interestOps() & ~SelectionKey.OP_WRITE);}}}}}}
}

HttpsClient


import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;public class HttpsClient {private static final List<byte[]> key = new ArrayList<>();private static final byte[] randomList = new byte[3];private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);public static void main(String[] args) throws Exception {Socket socket = new Socket("localhost", 8080);socket.setSoLinger(true, 0); // 设置关闭连接时的等待时间
//        socket.setReuseAddress(true); // 设置端口重用String clientHello = "Client Hello";int anInt = new Random().nextInt(Byte.MAX_VALUE);System.out.println("Client 随机数1:" + anInt);randomList[0] = (byte) anInt;socket.getOutputStream().write(new byte[]{13});socket.getOutputStream().write(clientHello.getBytes());socket.getOutputStream().write(anInt);InputStream inputStream = socket.getInputStream();byte[] buffer = new byte[12];int read, count = 0;while (count < 12) {read = inputStream.read(buffer);count += read;}String cmd = new String(buffer);System.out.println("Server " + cmd);// 读取第二个随机数int read1 = inputStream.read();System.out.println("Server 随机数2:" + read1);randomList[1] = (byte) read1;// 读取公钥int len = inputStream.read();System.out.println("publicKey len: " + len);byte[] publicKey = new byte[len];int count2 = 0;while (count2 < len) {int read2 = inputStream.read(publicKey);count2 += read2;}key.add(publicKey);System.out.println("输入任何字符开始第二次通信...");System.in.read();// 客户端生成第三个随机数int r3 = new Random().nextInt(Byte.MAX_VALUE);byte[] bytes = {(byte) r3};randomList[2] = bytes[0];System.out.println("随机数3=" + Arrays.toString(bytes));byte[] data = EncryptUtil.encryptByPublicKey(bytes, publicKey);socket.getOutputStream().write(data.length); // 64socket.getOutputStream().write(data);System.out.println("randomList:" + Arrays.toString(randomList));// 生成会话密钥byte[] sessionKey = EncryptUtil.hmacSHA256(EncryptUtil.HmacSHA256_key.getBytes(), randomList);SetCache.add(sessionKey);System.out.println("会话密钥:" + Arrays.toString(sessionKey));System.out.println("密钥长度:" + SetCache.get().length);System.out.println("开始正式通信...");System.out.println("发送密钥....");socket.getOutputStream().write(SetCache.get());System.out.println("end...");socket.close();}public void test() throws IOException {SocketChannel channel = SocketChannel.open();channel.configureBlocking(false);Selector selector = Selector.open();channel.register(selector, SelectionKey.OP_CONNECT);channel.connect(new InetSocketAddress("localhost", 8080));while (true) {int select = selector.select();if (select > 0) {Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();while (iterator.hasNext()) {SelectionKey key = iterator.next();iterator.remove();if (key.isConnectable()) {if (channel.finishConnect()) {System.out.println("客户端连接成功...");channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ);}}if (key.isWritable()) {System.out.println("Client send...");SocketChannel channel1 = (SocketChannel) key.channel();ByteBuffer buffer = ByteBuffer.allocate(16);String ClientHello = "Client Hello";int r1 = new Random().nextInt(100);buffer.put(ClientHello.getBytes());buffer.putInt(r1);channel1.write(buffer);channel1.register(selector, key.interestOps() & ~SelectionKey.OP_WRITE);System.out.println("Client send end...");}if (key.isReadable()) {System.out.println("Client receive...");SocketChannel channel1 = (SocketChannel) key.channel();ByteBuffer buffer = ByteBuffer.allocate(16);channel1.read(buffer);byte[] array = buffer.array();System.out.println(new String(array, 0, 12));System.out.println(new String(array, 12, 16));channel1.register(selector, SelectionKey.OP_WRITE);}}}}}
}

EncryptUtil

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;public final class EncryptUtil {// 非对称加密算法private static final String KEY_ALGORITHM = "RSA";// 公钥private static final String PUBLIC_KEY = "PUBLIC_KEY";// 私钥private static final String PRIVATE_KEY = "PRIVATE_KEY";// RSA密钥长度 默认1024 必须为64的倍数private static final int KEY_SIZE = 512;public static final String HmacSHA256_key = "HmacSHA256_key";public static Map<String, Object> initKey() throws NoSuchAlgorithmException {// 实例化密钥对生成器KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);keyPairGenerator.initialize(KEY_SIZE);KeyPair keyPair = keyPairGenerator.generateKeyPair();RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();Map<String, Object> keyMap = new HashMap<>();keyMap.put(PUBLIC_KEY, publicKey);keyMap.put(PRIVATE_KEY, privateKey);return keyMap;}public static byte[] getPublicKey(Map<String, Object> keyMap) throws Exception {Key key = (Key) keyMap.get(PUBLIC_KEY);return key.getEncoded();}public static byte[] getPrivateKey(Map<String, Object> keyMap) throws NoSuchAlgorithmException {Key key = (Key) keyMap.get(PRIVATE_KEY);return key.getEncoded();}public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {// 取得公钥X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);// 对数据加密Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());cipher.init(Cipher.ENCRYPT_MODE, publicKey);return cipher.doFinal(data);}public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {// 取得私钥PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);// 生成私钥PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);// 对数据解密Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());cipher.init(Cipher.DECRYPT_MODE, privateKey);return cipher.doFinal(data);}/*** HmacSHA256算法,返回的结果始终是32位** @param key     加密的键,可以是任何数据* @param content 待加密的内容* @return 加密后的内容* @throws Exception*/public static byte[] hmacSHA256(byte[] key, byte[] content) throws Exception {Mac hmacSha256 = Mac.getInstance("HmacSHA256");hmacSha256.init(new SecretKeySpec(key, 0, key.length, "HmacSHA256"));byte[] hmacSha256Bytes = hmacSha256.doFinal(content);return hmacSha256Bytes;}
}

SetCache

public class SetCache {private static final byte[] cache = new byte[32];public static void add(byte[] key) {System.arraycopy(key, 0, cache, 0, 32);}public static byte[] get() {return cache;}
}

相关文章:

用Java NIO模拟HTTPS

HTTPS流程 名词解释&#xff1a; R1:随机数1 R2:随机数2 R3:随机数3 publicKey:公钥 privateKey:私钥 step1 客户端 client hello R1 服务端 server hello R2 publicKey(验证证书&#xff0c;证书包含公钥) step2 客户端 R3 publicKey加密 服务端 privateKey解密 s…...

基于YOLOv8的火车轨道检测识别系统:技术实现与应用前景

✨个人主页欢迎您的访问 ✨期待您的三连 ✨ ✨个人主页欢迎您的访问 ✨期待您的三连 ✨ ✨个人主页欢迎您的访问 ✨期待您的三连✨ ​​​ ​​​​​​​​​ ​​ 1. 引言&#xff1a;火车轨道检测领域概述 铁路运输作为国民经济的大动脉&#xff0c;其安全运行至关重要…...

解决 Ubuntu 上 Docker 安装与网络问题:从禁用 IPv6 到配置代理

解决 Ubuntu 上 Docker 安装与网络问题的实践笔记 在 Ubuntu&#xff08;Noble 版本&#xff09;上安装 Docker 时&#xff0c;我遇到了两个常见的网络问题&#xff1a;apt-get update 失败和无法拉取 Docker 镜像。通过逐步排查和配置&#xff0c;最终成功运行 docker run he…...

DVDFab Virtual Drive电脑DVD备份和制作软件 v13.0.3.7 中文绿色便携

前言 DVDFab Virtual Drive是一个很厉害的光盘处理软件&#xff0c;它能帮你做几件事情&#xff1a;复制你的DVD或蓝光光盘作为备份&#xff0c;把这些光盘里的内容转换成其他格式&#xff0c;还能把视频文件刻录到DVD或蓝光光盘上。这个软件很灵活&#xff0c;能处理像DVD、蓝…...

FISCO BCOS区块链Postman接口测试:高级应用与实战技巧 [特殊字符]

引言:为什么Postman是FISCO BCOS测试的利器? 在区块链开发领域,接口测试是确保系统稳定性和安全性的关键环节。作为国产领先的联盟链平台,FISCO BCOS在金融、政务、供应链等多个领域得到广泛应用。而Postman作为一款功能强大的API测试工具,凭借其直观的图形界面和丰富的测…...

a sort.py demo

这份代码展示了如何使用 sort.py。注意&#xff0c;此处&#xff0c;我将文件名改为 my_sort.py。 你并不能直接 copy 使用&#xff0c;因为环境&#xff0c;包&#xff0c;还有模型。 此处使用 SSD-MobileNetv2 进行物体检测&#xff0c;将结果传入以 np 数组的形式传入sort…...

Linux 入门八:Linux 多进程

一、概述 1.1 什么是进程&#xff1f; 在 Linux 系统中&#xff0c;进程是程序的一次动态执行过程。程序是静态的可执行文件&#xff0c;而进程是程序运行时的实例&#xff0c;系统会为其分配内存、CPU 时间片等资源。例如&#xff0c;输入 ls 命令时&#xff0c;系统创建进程…...

学习如何设计大规模系统,为系统设计面试做准备!

前言 在当今快速发展的技术时代&#xff0c;系统设计能力已成为衡量一名软件工程师专业素养的重要标尺。随着云计算、大数据、人工智能等领域的兴起&#xff0c;构建高性能、可扩展且稳定的系统已成为企业成功的关键。然而&#xff0c;对于许多工程师而言&#xff0c;如何有效…...

前端防御性编程

关于防御性编程 你是否遇到过&#xff0c;接口请求失败或者返回数据错误&#xff0c;导致系统白屏或者前端自身写的代码存在一些缺陷&#xff0c;导致整个系统不够健壮&#xff0c;从而导致系统白屏 常见的问题与防范 最常见的问题 访问了null或者undefined的属性 null.a …...

Java工具类-assert断言

我们可能经常在项目的单元测试或者一些源码中看到别人在使用assert关键字&#xff0c;当然也不只是Java语言&#xff0c;很多编程语言也都能看到&#xff0c;我们大概知道断言可以用于测试中条件的校验&#xff0c;但却不经常使用&#xff0c;本文总结了Java中该工具类的使用。…...

215. 数组中的第K个最大元素

1、题目分析 顾名思义。 2.算法原理 利用排序&#xff0c;再找到第k个最大的数字即可。 3.代码实操 class Solution { public:int findKthLargest(vector<int>& nums, int k) {sort(nums.begin(),nums.end());return nums[nums.size()-k];//123456真的方便啊} };…...

【2025年泰迪杯数据挖掘挑战赛】B题 详细解题思路+数据预处理+代码分享

目录 2025年泰迪杯B题详细解题思路问题一问题分析数学模型Python代码Matlab代码 问题二问题分析数学模型Python代码Matlab代码 问题三问题分析数学模型Python代码Matlab代码 问题四问题分析数学模型Python代码Matlab代码 2025年泰迪杯B题详细解题思路 初步分析整理了B题的赛题分…...

对shell脚本敏感命令进行加密执行

我要加密这条命令&#xff1a;rm /root/scripty.sh 如何利用openssl aes-256-cbc 实现加密和解密&#xff0c;并执行命令 加密、解密并执行命令的完整流程 以下是使用 openssl aes-256-cbc 加密命令 rm /root/scripty.sh&#xff0c;解密并执行的详细步骤&#xff1a; 1. 加密…...

SQL ⑦-索引

索引 索引是一种特殊的数据结构&#xff0c;它帮助数据库系统高效地找到数据。 索引通过一定的规则排列数据表中的记录&#xff0c;使得对表的查询可以通过对索引的搜索来加快速度。 索引好比书籍的目录&#xff0c;能帮助你快速找到相应的章节。 B树 B树是一种经常用于数…...

LinkedBlockingQueue使用场景有哪些

1、LinkedBlockingQueue 的特点 LinkedBlockingQueue 是 Java 中 java.util.concurrent 包下的一种阻塞队列&#xff0c;它有以下几个主要特点&#xff1a; 1.线程安全 LinkedBlockingQueue 是线程安全的&#xff0c;它内部使用了锁机制来确保多线程环境下的并发访问不会导致…...

关于难例损失函数小记

什么是难例损失函数&#xff08;Hard Example Loss Function&#xff09; 这玩意儿是深度学习训练中非常重要又很实用的一个概念&#xff0c;特别适用于处理 数据不平衡、模型收敛缓慢、或者**想让模型更“挑剔”**的场景。 &#x1f31f; 先从名字讲起&#xff1a; “难例”…...

小程序开发指南

小程序开发指南 目录 1. 小程序开发概述 1.1 什么是小程序1.2 小程序的优势1.3 小程序的发展历程 2. 开发准备工作 2.1 选择开发平台2.2 开发环境搭建2.3 开发模式选择 3. 小程序开发流程 3.1 项目规划3.2 界面设计3.3 代码开发3.4 基本开发示例3.5 数据存储3.6 网络请求3.7 …...

RCE漏洞学习

1&#xff0c;What is RCE&#xff1f; 在CTF&#xff08;Capture The Flag&#xff09;竞赛中&#xff0c;RCE漏洞指的是远程代码执行漏洞&#xff08;Remote Code Execution&#xff09;。这类漏洞允许攻击者通过某种方式在目标系统上执行任意代码&#xff0c;从而完全控制目…...

青少年编程考试 CCF GESP图形化编程 三级认证真题 2025年3月

图形化编程 三级 2025 年 03 月 一、单选题&#xff08;共 15 题&#xff0c;每题 2 分&#xff0c;共 30 分&#xff09; 1、2025 年春节有两件轰动全球的事件&#xff0c;一个是 DeepSeek 横空出世&#xff0c;另一个是贺岁 片《哪吒 2》票房惊人&#xff0c;入了全球票房榜…...

一、绪论(Introduction of Artificial Intelligence)

写在前面&#xff1a; 老师比较看重的点&#xff1a;对问题的概念本质的理解&#xff0c;不会考试一堆运算的东西&#xff0c;只需要将概念理解清楚就可以&#xff0c;最后一个题会出一个综合题&#xff0c;看潜力&#xff0c;前面的部分考的不是很深&#xff0c;不是很难&…...

多模态大语言模型arxiv论文略读(十五)

## Jailbreaking GPT-4V via Self-Adversarial Attacks with System Prompts ➡️ 论文标题&#xff1a;Jailbreaking GPT-4V via Self-Adversarial Attacks with System Prompts ➡️ 论文作者&#xff1a;Yuanwei Wu, Xiang Li, Yixin Liu, Pan Zhou, Lichao Sun ➡️ 研究机…...

漏洞报告:多短视频平台时间差举报滥用漏洞

漏洞标题&#xff1a;跨平台内容发布时序漏洞导致的恶意举报攻击向量 漏洞类型&#xff1a;逻辑缺陷/滥用机制 漏洞等级&#xff1a;中高风险 漏洞描述&#xff1a; 攻击者可利用多平台内容发布时间差&#xff0c;伪造原创证明对合法内容发起恶意举报。该漏洞源于平台间缺乏发…...

【LINUX】学习宝典

一.Linux系统常用单词翻译 1.new folder 新建文件夹 2.paste 粘贴 3.select all 全选 4.open in terminal 打开终端/命令行 5.keep aligned 保持对齐 6.organize deaktop by name按名称组织桌面 7.change background更改背景 8.cancel 取消 9.create创造 创建 10.wal…...

青少年编程考试 CCF GESP图形化编程 四级认证真题 2025年3月

图形化编程 四级 2025 年 03 月 一、单选题&#xff08;共 10 题&#xff0c;每题 2 分&#xff0c;共 30 分&#xff09; 1、2025 年春节有两件轰动全球的事件&#xff0c;一个是 DeepSeek 横空出世&#xff0c;另一个是贺岁片《哪吒 2》票房惊人&#xff0c;入了全球票房榜…...

学习海康VisionMaster之平行线查找

一&#xff1a;进一步学习了 今天学习下VisionMaster中的平行线查找&#xff0c;这个还是拟合直线的衍生应用&#xff0c;可以同时测量两条线段&#xff0c;输出中线 二&#xff1a;开始学习 1&#xff1a;什么是平行线查找&#xff1f; 按照传统的算法&#xff0c;必须是开两…...

小甲鱼第004讲:变量和字符串(下)| 课后测试题及答案

问答题: 0. 请问下面代码有没有毛病&#xff0c;为什么? 请问下面代码为什么会出错&#xff0c;应该如何解决&#xff1f; 答:这是由于在字符串中&#xff0c;反斜杠()会与其随后的字符共同构成转义字符。 为了避免这种不测情况的发生&#xff0c;我们可以在字符串的引号前面…...

2025 蓝桥杯省赛c++B组个人题解

声明 本题解为退役蒻苟所写&#xff0c;不保证正确性&#xff0c;仅供参考。 花了大概2个半小时写完&#xff0c;感觉比去年省赛简单&#xff0c;难度大概等价于 codeforces dv4.5 吧 菜鸡不熟悉树上背包&#xff0c;调了一个多小时 题目旁边的是 cf 预测分 所有代码均以通…...

2025蓝桥杯算法竞赛深度突破:创新题型与高阶策略全解析

一、新型算法范式实战 1.1 元启发式算法应用&#xff08;预测难度&#xff1a;★★★★&#xff09; 题目场景&#xff1a;星际货物装载 需在飞船载重限制下选择最优货物组合&#xff0c;引入遗传算法解决NP-Hard问题&#xff1a; 染色体编码&#xff1a;二进制串表示货物选择…...

网络流量管理-流(Flow)

1. 传统网络的问题&#xff1a;快递员送信模式 想象你每天要寄100封信给同一个朋友&#xff0c;传统网络的处理方式就像一个固执的快递员&#xff1a; 每封信都单独处理&#xff1a;检查地址、规划路线、盖章、装车…即使所有信的目的地、收件人都相同&#xff0c;也要重复100…...

Spring Boot对接马来西亚股票数据源API

随着对东南亚市场的兴趣日益增长&#xff0c;获取马来西亚股票市场的实时和历史数据变得尤为重要。本文将指导您如何使用Spring Boot框架对接一个假定的马来西亚股票数据源API&#xff08;例如&#xff0c;StockTV API&#xff09;&#xff0c;以便开发者能够轻松访问和处理这些…...

MySQL 面经

1、什么是 MySQL&#xff1f; MySQL 是一个开源的关系型数据库&#xff0c;现在隶属于 Oracle 公司。是我们国内使用频率最高的一种数据库&#xff0c;我本地安装的是比较新的 8.0 版本。 1.1 怎么删除/创建一张表&#xff1f; 可以使用 DROP TABLE 来删除表&#xff0c;使用…...

【Flink运行时架构】作业提交流程

本文介绍在单作业模式下Flink提交作业的具体流程&#xff0c;如下图所示。 客户端将作业提交给YARN的RM&#xff1b;YARN的RM启动Flink JobManager&#xff0c;并将作业提交给JobMaster&#xff1b;JobMaster向Flink内置的RM请求slots&#xff1b;Flink内置的RM向YARN RM请求…...

【AutoTest】自动化测试工具大全(Java)

&#x1f60a; 如果您觉得这篇文章有用 ✔️ 的话&#xff0c;请给博主一个一键三连 &#x1f680;&#x1f680;&#x1f680; 吧 &#xff08;点赞 &#x1f9e1;、关注 &#x1f49b;、收藏 &#x1f49a;&#xff09;&#xff01;&#xff01;&#xff01;您的支持 &#x…...

当DRAM邂逅SSD:新型“DRAM+”存储技术来了!

在当今快速发展的科技领域&#xff0c;数据存储的需求日益增长&#xff0c;对存储设备的性能和可靠性提出了更高的要求。传统DRAM以其高速度著称&#xff0c;但其易失性限制了应用范围&#xff1b;而固态硬盘SSD虽然提供非易失性存储&#xff0c;但在速度上远不及DRAM。 为了解…...

【算法】快速排序

算法系列六&#xff1a;快速排序 一、快速排序的递归探寻 1.思路 2.书写 3.搭建 3.1设计过掉不符情况&#xff08;在最底层时&#xff09; 3.2查验能实现基础结果&#xff08;在最底层往上点时&#xff09; 3.3跳转结果继续往上回搭 4.实质 二、快速排序里的基准排序 …...

Python快速入门指南:从零开始掌握Python编程

文章目录 前言一、Python环境搭建&#x1f94f;1.1 安装Python1.2 验证安装1.3 选择开发工具 二、Python基础语法&#x1f4d6;2.1 第一个Python程序2.2 变量与数据类型2.3 基本运算 三、Python流程控制&#x1f308;3.1 条件语句3.2 循环结构 四、Python数据结构&#x1f38b;…...

机器学习中的数学(PartⅡ)——线性代数:2.1线性方程组

概述&#xff1a; 现实中很多问题都可被建模为线性方程组问题&#xff0c;而线性代数为我们提供了解决这类问题的工具。先看两个例子&#xff1a; 例子1&#xff1a; 一家公司有n个产品&#xff0c;分别是&#xff0c;生产上述产品需要m种原料&#xff0c;每个产品需要其中一…...

大模型上下文协议MCP详解(2)—核心功能

版权声明 本文原创作者:谷哥的小弟作者博客地址:http://blog.csdn.net/lfdfhl1. 标准化上下文交互技术 1.1 实时数据接入能力 MCP(Model Context Protocol)通过标准化的接口,为 AI 模型提供了强大的实时数据接入能力,使其能够快速获取和处理来自不同数据源的实时信息。…...

检测到目标URL存在http host头攻击漏洞

漏洞描述 修复措施 方法一: nginx 的 default_server 指令可以定义默认的 server 去处理一些没有匹配到 server_name 的请求,如果没有显式定义,则会选取第一个定义的 server 作为 default_server。 server {listen 80 default_server; …...

【 Beautiful Soup (bs4) 详解】

引言 Beautiful Soup 是 Python 最流行的 HTML/XML 解析库&#xff0c;能够从复杂的网页文档中高效提取数据。以下是其核心知识点及示例代码。 一、库简介 1. 核心模块 BeautifulSoup&#xff1a;主类&#xff0c;用于构建文档树结构Tag&#xff1a;表示 HTML/XML 标签的对象…...

Cuto壁纸 2.6.9 | 解锁所有高清精选壁纸,无广告干扰

Cuto壁纸 App 提供丰富多样的壁纸选择&#xff0c;涵盖动物、风景、创意及游戏动漫等类型。支持分类查找与下载&#xff0c;用户可轻松将心仪壁纸设为手机背景&#xff0c;并享受软件内置的编辑功能调整尺寸。每天更新&#xff0c;确保用户总能找到新鲜、满意的壁纸。 大小&am…...

人工智能之数学基础:复矩阵

本文重点 复矩阵是线性代数中以复数为元素的矩阵,是实矩阵在复数域上的自然推广。与实矩阵相比,复矩阵在数学性质、运算规则和应用场景上具有独特性,尤其在量子力学、信号处理、控制理论等领域发挥关键作用。 复矩阵的定义与表示 定义:复矩阵指的是元素含有复数的矩阵。…...

numpy初步掌握

文章目录 一、前言二、概述2.1 安装2.2 基础 三、数组3.1 数组创建3.1.1 从已有数据创建3.1.2 创建特殊值数组3.1.3 创建数值范围数组3.1.4 随机数组生成3.1.5 其他 3.2 数组属性 四、数组操作4.1 索引/切片4.2 数组遍历4.3 修改形状4.4 更多 五、数组运算5.1 常规运算5.2 广播…...

unity曲线射击

b站教程 using UnityEngine; using System.Collections;public class BallLauncher : MonoBehaviour {public float m_R;public NewBullet m_BulletPre;public Transform m_Target;private void Start(){StartCoroutine(Attack());}private void OnDestroy(){StopAllCoroutine…...

[特殊字符] 各领域 Dummy 开关实现方式大集合

涵盖硬件、软件、工业控制、游戏开发及网络虚拟化场景&#xff1a; &#x1f50c; 1. 电子 / 硬件工程 &#x1f6e0;️ (1) 物理替代方案 &#x1f9f2; 跳线帽&#xff08;Jumper&#xff09;或短路块 &#x1f449; 模拟开关“开/关”状态 ✅ 示例&#xff1a;开发板上的 B…...

深度解析基于 Web Search MCP的Deep Research 实现逻辑

写在前面 大型语言模型(LLM)已成为我们获取信息、生成内容的重要工具。但它们的知识大多截止于训练数据的时间点,对于需要实时信息、跨领域知识整合、多角度观点比较的深度研究 (Deep Research) 任务,它们往往力有不逮。如何让 LLM 突破自身知识的局限,像人类研究员一样,…...

set 的 contains

语法&#xff1a; set<int> num_set; st.contains(num); 在 C 中&#xff0c;!num_set.contains(num - 1) 这行代码通常用于检查一个集合&#xff08;num_set&#xff09;中是否不存在某个值&#xff08;num - 1&#xff09;。以下是对这行代码的详细解释&#xff1a;…...

深度学习总结(7)

用计算图进行自动微分 思考反向传播的一种有用方法是利用计算图(compu- tation graph)​。计算图是TensorFlow和深度学习革命的核心数据结构。它是一种由运算(比如我们用到的张量运算)构成的有向无环图。下图给出了一个模型的计算图表示。 计算图是计算机科学中一个非常…...

linux网络环境配置

今天我们来了解一下ip获取的两种方式,知道两者的特点,并且学会配置静态ip,那么话不多说,来看. linux网络环境配置. .第一种方式(自动获取): 说明:登录后,通过界面的来设置自动获取ip,特点;Linux启动后会自动获取ip,缺点是每次自动获取的ip地址可能不一样. 第二种方式(指定I…...

SSRF漏洞公开报告分析

文章目录 1. SSRF | 获取元数据 | 账户接管2. AppStore | 版本上传表单 | Blind SSRF3. HOST SSRF一、为什么HOST修改不会影响正常访问二、案例 4. Turbonomic 的 终端节点 | SSRF 获取元密钥一、介绍二、漏洞分析 5. POST | Blind SSRF6. CVE-2024-40898利用 | SSRF 泄露 NTL…...