MQTT HMACSHA256加密
时间: 2023-10-16 14:05:47 浏览: 66
MQTT HMACSHA256加密是一种用于保护MQTT消息传输安全的加密方式。它基于HMAC(Hash-based Message Authentication Code)算法和SHA256(Secure Hash Algorithm 256)散列函数,使用共享密钥对消息进行加密和验证,以防止未经授权的访问和篡改。
具体实现流程如下:
1. 生成一个对称密钥,这个密钥将用于加密和验证消息。
2. 在MQTT连接时,客户端和服务端交换此密钥。
3. 在发送MQTT消息时,客户端通过将消息和密钥输入到HMACSHA256算法中,生成一个哈希值。客户端将此哈希值和消息一起发送给服务端。
4. 服务端使用相同的密钥和HMACSHA256算法对接收到的消息进行哈希计算,然后将结果与客户端发送的哈希值进行比较,以验证消息的完整性和真实性。
5. 如果哈希值不匹配,则服务端会拒绝消息,并通知客户端。
通过使用MQTT HMACSHA256加密,可以有效地保护MQTT通信的安全性,并防止未经授权的访问和篡改。
相关问题
MQTT 加密的话 端口变么
MQTT 加密通常使用的是 SSL/TLS 协议,因此需要使用另一个端口号来提供加密服务。默认情况下,非加密的 MQTT 服务使用的是 1883 端口,而加密的 MQTT 服务使用的是 8883 端口。这个端口号可以在 MQTT 服务器的配置文件中进行修改。
如果你使用的是 Mosquitto MQTT 服务器,你可以在配置文件中找到以下行:
```
# Port to use for the encrypted MQTT protocol.
#port 8883
```
将 `port` 行的注释符号 `#` 去掉,并将端口号改为你想要的加密端口号即可。修改完成后,需要重新启动 Mosquitto 服务器才能生效。
windows c语言使用mqtt不加密向mosquitto服务器推送json数据
在 Windows C 语言程序中使用 MQTT 协议向 Mosquitto 服务器发送 JSON 数据,可以使用 Paho MQTT C 客户端库。下面是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <MQTTClient.h>
#include <cJSON.h>
#define ADDRESS "tcp://10.10.10.97:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "test"
#define QOS 1
#define TIMEOUT 10000L
int main(int argc, char* argv[]) {
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
cJSON *root = NULL;
char *payload = NULL;
int rc;
MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "name", "John");
cJSON_AddStringToObject(root, "surname", "Doe");
cJSON_AddNumberToObject(root, "age", 30);
payload = cJSON_Print(root);
MQTTClient_message pubmsg = MQTTClient_message_initializer;
pubmsg.payload = payload;
pubmsg.payloadlen = strlen(payload);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_deliveryToken token;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("Waiting for up to %d seconds for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
(int)(TIMEOUT / 1000), payload, TOPIC, CLIENTID);
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered\n", token);
cJSON_Delete(root);
free(payload);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}
```
这个示例代码中,首先定义了一些常量,包括 MQTT 代理服务器的地址 `ADDRESS`、客户端 ID `CLIENTID`、MQTT 主题 `TOPIC`、消息 QoS 级别 `QOS`、和超时时间 `TIMEOUT`。
然后,在 `main` 函数中,创建了一个 MQTT 客户端实例 `client`。使用 `MQTTClient_connect` 函数连接 MQTT 代理服务器。连接成功后,使用 cJSON 库创建了一个 JSON 对象 `root`,并向其中添加了三个属性:`name`、`surname` 和 `age`。使用 `cJSON_Print` 函数将 JSON 对象转换成字符串格式,保存在变量 `payload` 中。
接着,定义了一个 MQTT 消息结构体 `pubmsg`,并设置了消息内容为 `payload`、消息长度为字符串长度、QoS 级别为 1,保留标志为 0。使用 `MQTTClient_publishMessage` 函数将消息发布到指定的 MQTT 主题上,并获取了发布令牌 `token`。
等待消息发布完成,使用 `MQTTClient_waitForCompletion` 函数等待消息传递完成,超时时间为 `TIMEOUT`。如果消息成功发送,则打印消息传递令牌 `token`。
最后,释放 JSON 对象内存、释放字符串内存、断开 MQTT 连接并销毁 MQTT 客户端实例,然后返回程序执行结果。
请注意,这个示例代码中的 MQTT 连接是不加密的,如果需要加密,请使用 SSL/TLS 协议连接 Mosquitto 服务器。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)