GxDlms编译

GxDlms编译

  1. C++版本如果要编译动态库,项目>属性需要在GXDLMSIp4Setup.h加入

    #pragma comment(lib, "ws2_32.lib")//这是链接API相关连的Ws2_32.lib静态库
    

    mark

  2. 刚开始QT使用静态库的时候,提示一堆找不到,要注意默认qt这里使用的是x64,然后我们将vs编译的改成x64,再加入静态库就好了

  3. qt加入库

    mark

  4. qt编译的时候如果提示少inet..,加入步骤1中的头文件

  5. 打包 windeployqt 工具命令:windeployqt hello.exe

  6. 一个小demo测试

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "./development/include/GXDLMSSecureClient.h"
    #include <iostream>
    using namespace std;
    #include <QApplication>
    #pragma comment(lib, "ws2_32.lib")
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
    
        CGXCipher cipher("4D4D4D0000BC614E");
    
    
        CGXByteBuffer SYS_TITLE;
        SYS_TITLE.SetHexString("4D4D4D0000BC614E");
        cipher.SetSystemTitle(SYS_TITLE);
    
    
        cipher.SetSecurity(DLMS_SECURITY_AUTHENTICATION_ENCRYPTION);
        CGXByteBuffer EK;
        EK.SetHexString("11111111111111111111111111111111");
        cipher.SetBlockCipherKey(EK);
        CGXByteBuffer AK;
        AK.SetHexString("33333333333333333333333333333333");
        cipher.SetAuthenticationKey(AK);
    
        cipher.SetFrameCounter(0);
    
        CGXByteBuffer plainText;
        plainText.SetHexString("01000000065f1f040000181dffff");
    
        CGXByteBuffer cryptedText;
    
        int cmd = 0x21;//DLMS_COMMAND_GENERAL_GLO_CIPHERING;
        cryptedText.Clear();
        cipher.Encrypt(cipher.GetSecurity(),
                        DLMS_COUNT_TYPE_TAG,
                        cipher.GetFrameCounter(),
                        cmd,
                        cipher.GetSystemTitle(),
                        cipher.GetBlockCipherKey(),
                        plainText,
                        cryptedText);
        std::cout << "DLMS_COUNT_TYPE_TAG" << std::endl;
        std::cout << cryptedText.ToHexString() << std::endl;
    
        cryptedText.Clear();
        cipher.Encrypt(cipher.GetSecurity(),
                        DLMS_COUNT_TYPE_DATA,
                        cipher.GetFrameCounter(),
                        cmd,
                        cipher.GetSystemTitle(),
                        cipher.GetBlockCipherKey(),
                        plainText,
                        cryptedText);
        cout << "DLMS_COUNT_TYPE_DATA" << endl;
        cout << cryptedText.ToHexString() << endl;
    
        cryptedText.Clear();
        cipher.Encrypt(cipher.GetSecurity(),
                        DLMS_COUNT_TYPE_PACKET,
                        cipher.GetFrameCounter(),
                        cmd,
                        cipher.GetSystemTitle(),
                        cipher.GetBlockCipherKey(),
                        plainText,
                        cryptedText);
        cout << "DLMS_COUNT_TYPE_PACKET" << endl;
        cout << cryptedText.ToHexString() << endl;
    
    
        ui->textEdit->setText(QString::fromStdString(cryptedText.ToHexString()));
    }
    
    1. qt程序
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "./development/include/GXDLMSSecureClient.h"
    #include <iostream>
    using namespace std;
    #include <QApplication>
    #include <QDebug>
    #include <QDataStream>
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm> // 大写转换
    #include <regex>
    #pragma comment(lib, "ws2_32.lib")
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    class string_help
    {
    public:
        string_help();
    
    
        static bool IsHexNum(char c);
        static void deleteAllMark(string &s, const string &mark);
    
    
        // 假定完美 hex形式的字符串形如 010203040506...FF 全是大写的并且
        // 中间无任何分隔符
        const static string demo_srting;
        // 替换一次
        static string replace_all(string&  str, const string& old_value, const string& new_value);
        // 迭代替换
        static string replace_all_distinct(string& str, const string& old_value, const string& new_value);
    
    
        // hex数组转换为string
        static string HexArrayToString(const vector<unsigned char >& data);
        //转换一个string到完美string
         static string HexStringFormat(const std::string& data=demo_srting);
        // string转换为一个数组
        static vector<unsigned char> StringToHexArray(const std::string& data=demo_srting);
    };
    
    const string string_help::demo_srting="500010203040506073031323334ff";
    
    string string_help:: replace_all_distinct(string& str, const string& old_value, const string& new_value)
    {
        string::size_type pos=0;
        while((pos=str.find(old_value,pos))!= string::npos)
        {
            str=str.replace(pos,old_value.length(),new_value);
            if(new_value.length()>0)
            {
                pos+=new_value.length();
            }
        }
        return str;
    
    }
    
    //替换2  循环替换,替换后的值也检查并替换 12212 替换12为21----->22211
    string string_help::replace_all(string& str, const string& old_value, const string& new_value)
    {
            string::size_type pos=0;
            while((pos=str.find(old_value))!= string::npos)
            {
                str=str.replace(str.find(old_value),old_value.length(),new_value);
            }
            return str;
    }
    
    void string_help:: deleteAllMark(string &s, const string &mark)
    {
        size_t nSize = mark.size();
        while(1)
        {
            size_t pos = s.find(mark);    //  尤其是这里
            if(pos == string::npos)
            {
                return;
            }
    
            s.erase(pos, nSize);
        }
    }
    
    bool string_help::IsHexNum(char c)
    {
        if(c>='0' && c<='9') return true;
        if(c>='a' && c<='f') return true;
        if(c>='A' && c<='F') return true;
        return false;
    
    }
    
    string string_help::HexStringFormat(const std::string& data)
    {
       vector<unsigned char >hex_array;
       //1. 转换为大写
       string format_string=data;
       transform(data.begin(),data.end(),format_string.begin(),::toupper);
    
       //2. 去除0X
       replace_all_distinct(format_string,"0X"," ");
       replace(format_string.begin(),format_string.end(),',',' ');
       replace(format_string.begin(),format_string.end(),',',' ');
    
       regex e("\\s+");
       regex_token_iterator<string::iterator> i(format_string.begin(), format_string.end(), e, -1);
       regex_token_iterator<string::iterator> end;
       while (i != end)
       {
           //字串处理
           string tmp_get=*i;
           for(size_t i = 0; i < tmp_get.length(); )
           {
                if(IsHexNum(tmp_get[i]))
                {
                    if(i+1<tmp_get.length() && IsHexNum(tmp_get[i+1]) )
                    {
                        string one=tmp_get.substr(i,2);
                        unsigned char value = static_cast<unsigned char>(std::stoi(one,0,16));
                        hex_array.push_back(value);
                        i++;
                    }
                    else
                    {
                        string one=tmp_get.substr(i,1);
                        unsigned char value = static_cast<unsigned char>(std::stoi(one,0,16));
                        hex_array.push_back(value);
                    }
                    //break;
                }
                i++;
           }
           i++;
       }
       return HexArrayToString(hex_array);
    }
    
    
    
    
    
    vector<unsigned char>  string_help::StringToHexArray(const std::string& src)
    {
        vector<unsigned char> ret;
        if(src.size()<1)
        {
            ret.push_back(0x00);
            return ret;
        }
        for (string::size_type i = 0; i < src.size()-1; i+=2)
        {
            string one=src.substr(i,2);
            unsigned char value = static_cast<unsigned char>(std::stoi(one,0,16));
            ret.push_back(value);
        }
        return ret;
    }
    
    string string_help::HexArrayToString(const vector<unsigned char >& data)
    {
        const string hexme = "0123456789ABCDEF";
        string ret="";
        for(auto it:data)
        {
           ret.push_back(hexme[(it&0xF0) >>4]);
           ret.push_back(hexme[it&0x0F]);
        }
        return ret;
    }
    
    
    string format(QTextEdit * me,QString default_txt )
    {
        QString tmp=me->toPlainText();
    
        if(tmp.size()<1)
            tmp=default_txt;
    
        string good_string=string_help::HexStringFormat(tmp.toStdString());
        QString good_qstring = QString::fromStdString(good_string);
        me->setText(good_qstring);
        return good_string;
    }
    
    
    
    void MainWindow::on_pushButton_clicked()
    {
    
        //ui->sysText->setText(QString::fromStdString("123"));
    
         string s_systitle   =format(ui->sysText,"4D4D4D0000BC614E");
         string s_Ak         =format(ui->AkText,"33333333333333333333333333333333");
         string s_Ek         =format(ui->EkText,"11111111111111111111111111111111");
         string s_Ic         =format(ui->IcEdit_8,"00");
         string s_text       =format(ui->plainText,"01000000065f1f040000181dffff");
         string cmdme        =format(ui->cmdme,"21");
    
    
    
    
    
    
    
    
        CGXCipher cipher(s_systitle.c_str());
        CGXByteBuffer SYS_TITLE;
        SYS_TITLE.SetHexString(s_systitle.c_str());
        CGXByteBuffer EK;
        EK.SetHexString(s_Ek.c_str());
        CGXByteBuffer AK;
        AK.SetHexString(s_Ak.c_str());
        CGXByteBuffer plainText;
        plainText.SetHexString(s_text.c_str());
    
    //    SYS_TITLE.Clear();
    //    EK.Clear();
    //    AK.Clear();
    //    plainText.Clear();
    //    SYS_TITLE.SetHexString("4D4D4D0000BC614E");
    //    EK.SetHexString("11111111111111111111111111111111");
    //    AK.SetHexString("33333333333333333333333333333333");
    //    plainText.SetHexString("01000000065f1f040000181dffff");
    
        int value_IC  = QString::fromStdString(s_Ic).toInt(NULL, 16);
        int value_cmd = QString::fromStdString(cmdme).toInt(NULL, 16);
    
    
    
        cipher.SetBlockCipherKey(EK);
        cipher.SetAuthenticationKey(AK);
        //cipher.SetFrameCounter(std::stol(s_Ic));
        cipher.SetSystemTitle(SYS_TITLE);
        cipher.SetFrameCounter(value_IC);
    
    
    
    
    
    
    
    
        std::cout << plainText.ToHexString() << std::endl;
        cout << cipher.GetFrameCounter() <<endl;
        std::cout << cipher.GetSystemTitle().ToHexString() << std::endl;
        std::cout << cipher.GetBlockCipherKey().ToHexString() << std::endl;
        std::cout << cipher.GetAuthenticationKey().ToHexString() << std::endl;
    
    
        CGXByteBuffer cryptedText;
    
        int cmd=value_cmd;
        cipher.SetSecurity(DLMS_SECURITY_AUTHENTICATION_ENCRYPTION);
        cryptedText.Clear();
        cipher.Encrypt(cipher.GetSecurity(),
                        DLMS_COUNT_TYPE_PACKET,
                        cipher.GetFrameCounter(),
                        cmd,
                        cipher.GetSystemTitle(),
                        cipher.GetBlockCipherKey(),
                        plainText,
                        cryptedText);
        std::cout << "DLMS_COUNT_TYPE_TAG" << std::endl;
        std::cout << cryptedText.ToHexString() << std::endl;
    
        ui->enTextAll->setText(QString::fromStdString(cryptedText.ToHexString()));
    
        cryptedText.Clear();
        cipher.SetSecurity(DLMS_SECURITY_ENCRYPTION);
        cipher.Encrypt(cipher.GetSecurity(),
                        DLMS_COUNT_TYPE_DATA,
                        cipher.GetFrameCounter(),
                        cmd,
                        cipher.GetSystemTitle(),
                        cipher.GetBlockCipherKey(),
                        plainText,
                        cryptedText);
        cout << "DLMS_COUNT_TYPE_DATA" << endl;
        cout << cryptedText.ToHexString() << endl;
    
        ui->enTextData->setText(QString::fromStdString(cryptedText.ToHexString()));
    
    
    }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>1080</width>
        <height>605</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QTextEdit" name="plainText">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>40</y>
          <width>631</width>
          <height>91</height>
         </rect>
        </property>
       </widget>
       <widget class="QPushButton" name="pushButton">
        <property name="geometry">
         <rect>
          <x>690</x>
          <y>370</y>
          <width>75</width>
          <height>23</height>
         </rect>
        </property>
        <property name="text">
         <string>加密</string>
        </property>
       </widget>
       <widget class="QLabel" name="label">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>20</y>
          <width>54</width>
          <height>12</height>
         </rect>
        </property>
        <property name="text">
         <string>原文</string>
        </property>
       </widget>
       <widget class="QLabel" name="label_2">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>140</y>
          <width>121</width>
          <height>16</height>
         </rect>
        </property>
        <property name="text">
         <string>密文(包含tag)</string>
        </property>
       </widget>
       <widget class="QTextEdit" name="enTextAll">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>160</y>
          <width>631</width>
          <height>91</height>
         </rect>
        </property>
       </widget>
       <widget class="QTextEdit" name="enTextData">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>280</y>
          <width>631</width>
          <height>91</height>
         </rect>
        </property>
       </widget>
       <widget class="QLabel" name="label_3">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>260</y>
          <width>121</width>
          <height>16</height>
         </rect>
        </property>
        <property name="text">
         <string>密文 数据域</string>
        </property>
       </widget>
       <widget class="QTextEdit" name="enTextDataTag">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>410</y>
          <width>631</width>
          <height>91</height>
         </rect>
        </property>
       </widget>
       <widget class="QLabel" name="label_5">
        <property name="geometry">
         <rect>
          <x>670</x>
          <y>10</y>
          <width>71</width>
          <height>16</height>
         </rect>
        </property>
        <property name="text">
         <string>sys-title</string>
        </property>
       </widget>
       <widget class="QTextEdit" name="sysText">
        <property name="geometry">
         <rect>
          <x>670</x>
          <y>40</y>
          <width>371</width>
          <height>31</height>
         </rect>
        </property>
       </widget>
       <widget class="QLabel" name="label_6">
        <property name="geometry">
         <rect>
          <x>690</x>
          <y>80</y>
          <width>71</width>
          <height>16</height>
         </rect>
        </property>
        <property name="text">
         <string>Ak</string>
        </property>
       </widget>
       <widget class="QTextEdit" name="AkText">
        <property name="geometry">
         <rect>
          <x>670</x>
          <y>100</y>
          <width>371</width>
          <height>31</height>
         </rect>
        </property>
       </widget>
       <widget class="QLabel" name="label_7">
        <property name="geometry">
         <rect>
          <x>690</x>
          <y>140</y>
          <width>71</width>
          <height>16</height>
         </rect>
        </property>
        <property name="text">
         <string>Ek</string>
        </property>
       </widget>
       <widget class="QTextEdit" name="EkText">
        <property name="geometry">
         <rect>
          <x>670</x>
          <y>160</y>
          <width>371</width>
          <height>31</height>
         </rect>
        </property>
       </widget>
       <widget class="QLabel" name="label_8">
        <property name="geometry">
         <rect>
          <x>680</x>
          <y>210</y>
          <width>71</width>
          <height>16</height>
         </rect>
        </property>
        <property name="text">
         <string>IC 帧序号</string>
        </property>
       </widget>
       <widget class="QTextEdit" name="IcEdit_8">
        <property name="geometry">
         <rect>
          <x>670</x>
          <y>230</y>
          <width>371</width>
          <height>31</height>
         </rect>
        </property>
       </widget>
       <widget class="QLabel" name="label_9">
        <property name="geometry">
         <rect>
          <x>670</x>
          <y>270</y>
          <width>71</width>
          <height>16</height>
         </rect>
        </property>
        <property name="text">
         <string>cmd</string>
        </property>
       </widget>
       <widget class="QTextEdit" name="cmdme">
        <property name="geometry">
         <rect>
          <x>670</x>
          <y>290</y>
          <width>371</width>
          <height>31</height>
         </rect>
        </property>
       </widget>
       <widget class="QPushButton" name="pushButton_2">
        <property name="geometry">
         <rect>
          <x>790</x>
          <y>370</y>
          <width>151</width>
          <height>23</height>
         </rect>
        </property>
        <property name="text">
         <string>解密包含tag</string>
        </property>
       </widget>
       <widget class="QPushButton" name="pushButton_3">
        <property name="geometry">
         <rect>
          <x>790</x>
          <y>410</y>
          <width>151</width>
          <height>23</height>
         </rect>
        </property>
        <property name="text">
         <string>解密单纯数据,需要填写IC</string>
        </property>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>1080</width>
         <height>23</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    
posted @ 2019-12-05 15:39  zongzi10010  阅读(377)  评论(0编辑  收藏  举报