add encrypt

This commit is contained in:
notrynohigh 2020-03-22 02:00:27 +08:00
parent 167501c8dd
commit 2973086731
14 changed files with 119 additions and 4 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.0, 2020-03-15T20:02:22. -->
<!-- Written by QtCreator 4.11.0, 2020-03-22T01:58:08. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@ -67,7 +67,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.1 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.1 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5141.win64_mingw73_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
@ -299,7 +299,7 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">F:/babyos/BabyOS_Protocol/build-BabyOS_Protocol-Desktop_Qt_5_14_1_MinGW_64_bit-Release</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">F:/babyos/BabyOS_Protocol/build-BabyOS_Protocol-Desktop_Qt_5_14_1_MinGW_64_bit-Debug</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>

View File

@ -57,7 +57,14 @@
* \defgroup PROTOCOL_Private_Defines
* \{
*/
//<o> Secret Key 1
#define _SECRET_KEY1 1
//<o> Secret Key 2
#define _SECRET_KEY2 22
//<o> Secret Key 3
#define _SECRET_KEY3 333
//<o> Secret Key 4
#define _SECRET_KEY4 4444
/**
* \}
*/
@ -78,6 +85,10 @@
static bProtocolInfo_t bProtocolInfo[1];
static uint8_t bProtocolInfoIndex = 0;
#define _PROTO_TEA_DELTA 0x9e3779b9
const static uint32_t Keys[4] = {_SECRET_KEY1, _SECRET_KEY2, _SECRET_KEY3, _SECRET_KEY4};
/**
* \}
*/
@ -107,6 +118,39 @@ static uint8_t _bProtocolCalCheck(uint8_t *pbuf, bProtoLen_t len)
return tmp;
}
static void _bProtocolEncryptGroup(uint32_t *text, uint32_t *key)
{
uint32_t sum = 0, v0 = text[0], v1 = text[1];
uint32_t k0 = key[0], k1 = key[1], k2 = key[2], k3 = key[3];
int i = 0;
for(i = 0;i < 16;i++)
{
sum += _PROTO_TEA_DELTA;
v0 += (v1 << 4) + k0 ^ v1 + sum ^ (v1 >> 5) + k1;
v1 += (v0 << 4) + k2 ^ v0 + sum ^ (v0 >> 5) + k3;
}
text[0] = v0;
text[1] = v1;
}
static void _bProtocolDecryptGroup(uint32_t *text, uint32_t *key)
{
uint32_t sum = _PROTO_TEA_DELTA * 16, v0 = text[0], v1 = text[1];
uint32_t k0 = key[0], k1 = key[1], k2 = key[2], k3 = key[3];
int i = 0;
for(i = 0;i < 16;i++)
{
v1 -= (v0 << 4) + k2 ^ v0 + sum ^ (v0 >> 5) + k3;
v0 -= (v1 << 4) + k0 ^ v1 + sum ^ (v1 >> 5) + k1;
sum -= _PROTO_TEA_DELTA;
}
text[0] = v0;
text[1] = v1;
}
/**
* \}
*/
@ -229,6 +273,40 @@ int bProtocolPack(int no, bProtoID_t id, uint8_t cmd, uint8_t *param, bProtoLen_
return length;
}
void _bProtocolEncrypt(uint8_t *text, uint32_t size)
{
uint32_t number = size >> 3;
int i = 0;
if(size < 8)
{
return;
}
for(i = 0;i < number;i++)
{
_bProtocolEncryptGroup(&(((uint32_t *)text)[i * 2]), (uint32_t *)Keys);
}
}
void _bProtocolDecrypt(uint8_t *text, uint32_t size)
{
uint32_t number = size >> 3;
int i = 0;
if(size < 8)
{
return;
}
for(i = 0;i < number;i++)
{
_bProtocolDecryptGroup(&(((uint32_t *)text)[i * 2]), (uint32_t *)Keys);
}
}
/**
* \}
*/

View File

@ -141,6 +141,10 @@ int bProtocolRegist(bProtoID_t id, pdispatch f);
int bProtocolSetID(int no, bProtoID_t id);
int bProtocolParse(int no, uint8_t *pbuf, bProtoLen_t len);
int bProtocolPack(int no, bProtoID_t id, uint8_t cmd, uint8_t *param, bProtoLen_t param_size, uint8_t *pbuf);
void _bProtocolEncrypt(uint8_t *text, uint32_t size);
void _bProtocolDecrypt(uint8_t *text, uint32_t size);
/**
* \}
*/

View File

@ -69,6 +69,10 @@ void MainWindow::TimerTimeout()
tmp_buf[len] = '\0';
if(len > 0)
{
if(ui->encryptBox->isChecked() == true)
{
_bProtocolDecrypt(tmp_buf, len);
}
if(0 > bProtocolParse(Protocol_n, tmp_buf, len))
{
QString str = QString::fromStdString((char *)tmp_buf);
@ -138,5 +142,9 @@ void MainWindow::on_pushButton_clicked()
int len;
uint8_t param[7] = "BabyOS";
len = bProtocolPack(Protocol_n, 0x520, 0x1, param, 7, table);
if(ui->encryptBox->isChecked() == true)
{
_bProtocolEncrypt(table, len);
}
uartModule.uartSendBuff(table, len);
}

View File

@ -144,6 +144,19 @@ https://github.com/notrynohigh/BabyOS</string>
参数为&quot;BabyOS&quot;</string>
</property>
</widget>
<widget class="QCheckBox" name="encryptBox">
<property name="geometry">
<rect>
<x>320</x>
<y>20</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>加密传输</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">

View File

@ -11,6 +11,7 @@
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
@ -35,6 +36,7 @@ public:
QPushButton *pushButton;
QLabel *label_2;
QLabel *label_3;
QCheckBox *encryptBox;
QMenuBar *menubar;
QStatusBar *statusbar;
@ -74,6 +76,9 @@ public:
label_3 = new QLabel(centralwidget);
label_3->setObjectName(QString::fromUtf8("label_3"));
label_3->setGeometry(QRect(540, 420, 201, 91));
encryptBox = new QCheckBox(centralwidget);
encryptBox->setObjectName(QString::fromUtf8("encryptBox"));
encryptBox->setGeometry(QRect(320, 20, 71, 21));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
@ -105,6 +110,7 @@ public:
"\346\265\213\350\257\225\346\214\211\351\222\256\345\217\221\351\200\201\346\225\260\346\215\256\347\232\204\347\233\256\346\240\207id\346\230\2570x520\n"
"\345\217\221\351\200\201\347\232\204cmd\344\270\2720x1\n"
"\345\217\202\346\225\260\344\270\272\"BabyOS\"", nullptr));
encryptBox->setText(QCoreApplication::translate("MainWindow", "\345\212\240\345\257\206\344\274\240\350\276\223", nullptr));
} // retranslateUi
};

View File

@ -11,6 +11,7 @@
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
@ -35,6 +36,7 @@ public:
QPushButton *pushButton;
QLabel *label_2;
QLabel *label_3;
QCheckBox *encryptBox;
QMenuBar *menubar;
QStatusBar *statusbar;
@ -74,6 +76,9 @@ public:
label_3 = new QLabel(centralwidget);
label_3->setObjectName(QString::fromUtf8("label_3"));
label_3->setGeometry(QRect(540, 420, 201, 91));
encryptBox = new QCheckBox(centralwidget);
encryptBox->setObjectName(QString::fromUtf8("encryptBox"));
encryptBox->setGeometry(QRect(320, 20, 71, 21));
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QString::fromUtf8("menubar"));
@ -105,6 +110,7 @@ public:
"\346\265\213\350\257\225\346\214\211\351\222\256\345\217\221\351\200\201\346\225\260\346\215\256\347\232\204\347\233\256\346\240\207id\346\230\2570x520\n"
"\345\217\221\351\200\201\347\232\204cmd\344\270\2720x1\n"
"\345\217\202\346\225\260\344\270\272\"BabyOS\"", nullptr));
encryptBox->setText(QCoreApplication::translate("MainWindow", "\345\212\240\345\257\206\344\274\240\350\276\223", nullptr));
} // retranslateUi
};

Binary file not shown.