C++/Qt中一个简单的加密解密程序

本例采用查表法实现加密解密,采用一个长度为256的字节数组,每一个对应一个assic码,将顺序打乱,最终是一个全排列,加密的种类有256!=8.5781777534284265411908227168123e+506种,直接上代码吧。

生成密码表的函数,应用之前,先随机生成一个密码表:
QVector tmp;
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
while(tmp.count()<256)
{
  char t=qrand();
  if(!tmp.contains(t))tmp.append(t);
}


WriteLog_(" const char data[256] = {");
for(int i=0;i= 10000000)file.remove();
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << message ;
    file.flush();
    file.close();
}
*/


在头文件中加入生成的密码表和相关函数的声明:
 const char data[256]= {-12,57,-104,-66,-29,-5,121,124,-85,106,123,-53,2,-37,4,-35,113,-71,-106,-65,-10,
                        -83,34,-27,101,-8,-4,-48,-22,-113,-75,51,90,-79,60,22,-123,-13,99,25,87,42,-47,-26,-100,
                        -122,-46,64,13,102,75,93,69,19,92,-45,39,-16,-103,-80,9,82,88,71,52,-70,7,-14,108,-17,73,
                        65,-57,-90,-60,-23,-20,-67,40,-1,-43,-63,96,86,78,10,97,-84,61,62,-112,14,37,11,-124,-117,
                        85,72,21,127,-39,-118,45,80,53,38,-89,84,-81,-88,17,41,-119,70,18,-36,126,83,-56,76,0,114,
                        -78,48,-44,-54,79,104,112,59,8,-99,35,-114,68,115,31,-28,-24,-49,116,6,103,-51,111,-128,-7,
                        95,-50,77,-33,-97,-93,81,125,-98,-2,43,-31,15,-11,-69,-52,12,-76,28,-3,67,74,-82,-61,-102,
                        -38,109,-101,-116,-121,98,47,-109,-125,-18,36,-62,44,33,-30,29,-9,-110,-34,100,-72,50,3,
                        -68,-42,55,-92,23,-105,-32,56,110,24,-25,16,-96,-58,30,49,-87,-120,-55,-94,-15,119,20,58,32,
                        -41,-91,118,-127,-19,94,107,46,5,117,27,-77,105,120,54,-108,-126,-6,63,26,-64,122,-107,66,
                        -86,-111,-40,91,-95,89,-74,-115,-73,1,-59,-21};

    QMap tableEncrypt;
    QMap tableDecrypt;

    void encryptFile(QString fileName, QString dest);
    void decryptFile(QString fileName, QString dest);

在源文件中加入:

首先初始化密码表:
    for(int i=0;i<256;i++){
        tableEncrypt.insert(i,data[i]);
        tableDecrypt.insert(data[i],i);
    }


加密解密函数:
void MainWindow::encryptFile(QString fileName,QString dest)
{
    QFile *file = new QFile(fileName);
    file->open(QIODevice::ReadOnly);
    QByteArray bytes=file->readAll();

    QByteArray newBytes;
    int cnt=bytes.count();
    newBytes.resize(cnt);
    for(int i=0;iopen(QIODevice::WriteOnly);
    newFile->write(newBytes);
    newFile->flush();
    newFile->close();

    delete file;
    delete newFile;
}

void MainWindow::decryptFile(QString fileName,QString dest)
{
    QFile *file = new QFile(fileName);
    file->open(QIODevice::ReadOnly);
    QByteArray bytes=file->readAll();

    QByteArray newBytes;
    int cnt=bytes.count();
    newBytes.resize(cnt);
    for(int i=0;iopen(QIODevice::WriteOnly);
    newFile->write(newBytes);
    newFile->flush();
    newFile->close();

    delete file;
    delete newFile;
}

具体应用:
    ui->pushButton->setText("加密");
    connect(ui->pushButton,&QPushButton::clicked,this,[this](){
        QStringList fileNames=QFileDialog::getOpenFileNames();
        QString dest=QFileDialog::getExistingDirectory(this,"选择目标文件夹");
        for(int i=0;ipushButton1->setText("解密");
    connect(ui->pushButton1,&QPushButton::clicked,this,[this](){
        QStringList fileNames=QFileDialog::getOpenFileNames();
        QString dest=QFileDialog::getExistingDirectory(this,"选择目标文件夹");
        for(int i=0;i


你可能感兴趣的:(Qt,C++,STL)