DES算法C++、QT实现(有图形界面)

本文介绍了如何使用C++和QT库实现DES加密算法,并创建了一个带有图形界面的应用程序。主要涉及文件包括DES_UI.ui和main.cpp。在Linux环境下运行此程序更为灵活,需要注意代码在不同编译器间的兼容性问题。
摘要由CSDN通过智能技术生成
总共 7 个文件。其中一个编译配置文件 (DES.pro) ;两个类: DES_UI 、 DesCrypt ;两个类共四个
文件,分别为 DES_UI.h 、 DesCrypt.h 、 DES_UI.cpp 、 DesCrypt.cpp ;一个 GUI 文件:

DES_UI.ui ;还有一个主函数 main.cpp 。

先上一张运行图:


一个一个的上:

/*************1 、 DesCrypt.h*************/

#ifndef DESCRYPT_H
#define DESCRYPT_H
#include <string>
#include <iostream>
#include <cstdlib>

using std::string;

class DesCrypt
{
public:
    DesCrypt();
    ~DesCrypt();
    void setKey(char *key);
    void encrypt(char *data);
    void decrypt(char *data);

private:
    static void inline hextobin(char h[16], char bit[64]); /* hex to binary */
    static void inline bintohex(char bit[64], char h[16]); /* binary to hex */
    static void inline _bytetobit(char ch, char bit[8]);   /* 1byte to 8bit */
    static void inline _bittobyte(char bit[8], char* ch); /* 8bit to char */
    static void inline _u8to64(char ch[8], char bit[64]); /* 8*8 to 64 bit */
    static void inline _64tou8(char bit[64], char ch[8]); /* 64 bit to 8*8 */
    static void inline IP_Trans(char data[64]);   /*IP transform*/
    static void inline IP_1_Trans(char data[64]); /*IP traverse transform*/
    static void inline E_Trans(char data[48]);    /* E transform */
    static void inline P_Trans(char data[32]);    /* P transform */
    static void inline S_Trans(char data[48]);    /* S box transform */
    static void inline PC_1_Trans(char key[64], char bit[56]);       /* PC_1 transform */
    static void inline PC_2_Trans(const char key[56], char bit[48]); /* PC_2 transform */
    static void inline ROL(char data[56], int time);                 /* cycle shift */
    static void inline XOR(char R[48], char L[48], int times);       /* XOR */
    static void inline SWAP(char L[32], char R[32]);  /* swap */
    void subKeys(char key[64], char subkeys[16][48]);  /* make the subkeys */
    void encrypt64bit(char data[8], char subkeys[16][48], char cipher[8]); /* encrypt a block */
    void decrypt64bit(char data[8], char subkeys[16][48], char cipher[8]); /* decrypt a block */

public:
    string endata;
    string dedata;
private:
    char *key = (char*)malloc(64);
    static const int IP[64];
    static const int IP_1[64];
    static const int E[64];
    static const int P[32];
    static const int S[8][4][16];
    static const int PC_1[56];
    static const int PC_2[56];
    static const int MOV[16];
};

#endif // DESCRYPT_H

/*************2 、 DesCrypt.cpp*************/

#include "DesCrypt.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cstdlib>
using namespace std;

DesCrypt::DesCrypt()
{
    this->endata = "";
    this->dedata = "";
}

void DesCrypt::setKey(char *key)
{
    if(strlen(key)==64){
        memcpy(this->key,key,64);
    }
    if(strlen(key)==8){
        _u8to64(key,this->key);
    }
    this->endata = "";
    this->dedata = "";
//    this->key = key;
}

DesCrypt::~DesCrypt()
{
    free(this->key);
}

const int DesCrypt::IP[64] = { 57,49,41,33,25,17,9,1,
                               59,51,43,35,27,19,11,3,
                               61,53,45,37,29,21,13,5,
                               63,55,47,39,31,23,15,7,
                               56,48,40,32,24,16,8,0,
                               58,50,42,34,26,18,10,2,
                               60,52,44,36,28,20,12,4,
                               62,54,46,38,30,22,14,6};
const int DesCrypt::IP_1[64] = {39,7,47,15,55,23,63,31,
                                38,6,46,14,54,22,62,30,
                                37,5,45,13,53,21,61,29,
                                36,4,44,12,52,20,60,28,
                                35,3,43,11,51,19,59,27,
                                34,2,42,10,50,18,58,26,
                                33,1,41,9,49,17,57,25,
                                32,0,40,8,48,16,56,24};
const int DesCrypt::E[64] = {31, 0, 1, 2, 3, 4,
                             3, 4, 5, 6, 7, 8,
                             7, 8,9,10,11,12,
                             11,12,13,14,15,16,
                             15,16,17,18,19,20,
                             19,20,21,22,23,24,
                             23,24
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值