日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Qt4--加密日记本(子例化QMainWindow文本加密解密)

發布時間:2023/12/20 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt4--加密日记本(子例化QMainWindow文本加密解密) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • 近來剛學習Qt4編程,想找個實例練習練習,于是產生了一個想法,就是怎么樣做一個文本加密,這樣,自己保存的一些文檔可以通過軟件

生成加密文本,到時候要看的時候,通過自己的軟件讀取就可以。既然有想法了,那就行動起來吧!

  • 加密解密采用RC4方法,目前只能處理英文文檔。

  • 首先介紹一下軟件的框架


菜單欄:包括【file】、【edit】、【option】、【help】
  • 【file】下拉菜單包括【new】、【open】、【close】、【save】、【save as】、【exit】
  • 【edit】下拉菜單包括【copy】、【cut】、【paste】、【undo】、【redo】
  • 【option】下拉菜單包括【encrypt】、【decipher】
  • 【help】下拉菜單包括【about】
? ? ?? ??? ?

工具欄:包括【file】、【edit】、【option】、【font】、【list】
  • 【file】欄包括【new】、【open】、【save】、【save as】
  • 【edit】欄包括【copy】、【cut】、【paste】、【undo】、【redo】
  • 【option】欄包括【encrypt】、【decipher】
  • 【font】欄包括【fonttype】、【fontsize】、【bold】、【italic】、【under】、【color】
  • 【list】欄包括【list】、【left】、【center】、【justify】、【right】


編輯區:文本編輯框
狀態欄:顯示行列



  • 菜單欄的實現舉例
菜單欄其實只是對功能函數的一個集合而已,拿【file】菜單舉例,它包括【open】【new】【close】等操作,像【open】這樣的操作,可以看做是一個函數的動作, 當我們點擊這個選項的時候,執行相應的功能。
首先、在頭文件中定義一個動作,然后為這個動作添加一個需要執行的槽 private slots:void open(); private:QAction* openAction;
其次、就是在**.cpp中創建這個QAction 在這里為了各個QAction的創建和維護的便利性,我定義了一個creatAction()函數,如下是創建openAction的代碼: void SecretMainWindow::creatAction() {//=========創建一個【file】->【Open】的動作響應=========//openAction = new QAction(tr("&Open"), this);openAction->setIcon(QIcon(":/images/open.png"));openAction->setShortcut(QKeySequence::Open);openAction->setStatusTip(tr("Open a file"));connect(openAction, SIGNAL(triggered()), this, SLOT(open())); }
openAction = new QAction(tr("&Open"), this);創建一個新的QAction,標簽是【Open】
openAction->setIcon(QIcon(":/images/open.png"));給【Open】設置一個圖標 詳見:Qt--創建自定義圖標
openAction->setShortcut(QKeySequence::Open);給【Open】創建一個快捷鍵【Ctrl + O】 openAction->setStatusTip(tr("Open a file"));給【Open】設置狀態提示
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));連接信號與槽


然后、creatMenu()函數實現創建
void SecretMainWindow::creatMenus() {//=========創建一個【file】菜單欄=========//fileMenu = menuBar()->addMenu(tr("&File"));fileMenu->addAction(newAction);fileMenu->addAction(openAction);fileMenu->addAction(closeAction);fileMenu->addAction(saveAction);fileMenu->addAction(saveAsAction);fileMenu->addSeparator();fileMenu->addAction(exitAction); }
fileMenu = menuBar()->addMenu(tr("&File"));創建一個【file】菜單,并且命名為【File】
fileMenu->addAction(newAction);添加【New】到菜單【File】 fileMenu->addSeparator();添加一個分隔符
最后、實現槽函數open() void SecretMainWindow::open() {//========== 打開一個文件 ==========//toSave(); //如果內容發生變更,執行保存fileName = QFileDialog::getOpenFileName(this); //獲得要打開的文件的名字if(!fileName.isEmpty()) //如果文件名不為空,加載文件{loadFile(fileName);}textEdit->setVisible(true); //文本編輯器可見 }
  • 工具欄的實現舉例
工具欄的實現分兩種情況,一種是直接添加菜單欄里頭的功能,另一種是只有工具欄上才有的一些功能,實現略有差異,下面分別舉例說明。
情況一:把菜單欄里功能添加到工具欄,如【open】 void SecretMainWindow::creatToolBars() {//=========創建一個【file】工具欄=========//fileToolBar = addToolBar(tr("&File"));fileToolBar->addAction(newAction);fileToolBar->addAction(openAction);fileToolBar->addSeparator();fileToolBar->addAction(saveAction);fileToolBar->addAction(saveAsAction); }
方法與建立【file】菜單類似。需要在頭文件里建立一個工具欄的變量 QToolBar* fileToolBar;其余創建過程與【file】菜單是一致的。
情況二:創建工具欄特有的功能,如字體Font //=========創建一個【font】工具欄=========//fontToolBar = addToolBar(tr("&Font"));fontstyleLabel = new QLabel(tr("FontType:"));fontBox = new QFontComboBox;fontBox->setFontFilters(QFontComboBox::ScalableFonts);fontToolBar->addWidget(fontstyleLabel);fontToolBar->addWidget(fontBox);
fontToolBar = addToolBar(tr("&Font"));在工具欄上添加一個設置字體的工具欄 fontstyleLabel = new QLabel(tr("FontType:"));新建一個標簽:【FontType】
fontBox = new QFontComboBox;新建一個字體下拉框 fontBox->setFontFilters(QFontComboBox::ScalableFonts);把字體添加到下拉框 fontToolBar->addWidget(fontstyleLabel);fontToolBar->addWidget(fontBox);把標簽和下拉框添加到工具欄上

  • 更新狀態欄
void SecretMainWindow::creatStatusBar() {locationLabel = new QLabel;locationLabel->setAlignment(Qt::AlignHCenter);locationLabel->setMinimumSize(locationLabel->sizeHint());statusBar()->addWidget(locationLabel);connect(textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(updateStatusBar()));updateStatusBar(); }void SecretMainWindow::updateStatusBar() {locationLabel->setText(tr("%1 row %2 col").arg(textEdit->document()->blockCount()).arg(textEdit->textCursor().columnNumber())); }
statusBar()->addWidget(locationLabel);把狀態欄標簽添加到狀態欄上 connect(textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(updateStatusBar()));當光標位置發生變化的時候,觸發更新狀態欄 locationLabel->setText(tr("%1 row %2 col").arg(textEdit->document()->blockCount()).arg(textEdit->textCursor().columnNumber()));顯示出光標的位置,其中1,2分別對應后面的兩個參數
  • 加密解密
void SecretMainWindow::encrypt() {//save();//QFile file(currentFile);//QTextStream in(&file);QString text;text.append(textEdit->toPlainText());passwordDlg = new PassWord(this);if(passwordDlg->exec()){if(!passwordDlg->password.isNull())RC4(text, passwordDlg->password);} }
text.append(textEdit->toPlainText());獲取編輯區的文檔內容 passwordDlg = new PassWord(this);新建一個彈出窗口,輸入加密密鑰
passwordDlg->exec()運行彈出窗口 if(!passwordDlg->password.isNull())RC4(text, passwordDlg->password);確定獲取到密鑰后,進行RC4加密,參考網上的程序。
void SecretMainWindow::RC4(QString in, QString key) {int inLen = in.length();unsigned char* sequence;sequence = (unsigned char*)qstrdup(in.toAscii().constData());int keyLen = key.length();unsigned char* cKey;cKey = (unsigned char*)qstrdup(key.toAscii().constData());QString out = "";//Init Sboxchar SBox[128],Key[128];int i, k, j = 0, t;char temp, r;for(i = 0; i < 128; i++)SBox[i] = i;for(k = i = 0; i < 128; i++){Key[i] = cKey[k];k = (k + 1) % keyLen;}for (i = 0; i < 128; i++){j = (j + SBox[i] + Key[i]) % 128;temp = SBox[i];SBox[i] = SBox[j];SBox[j] = temp;}//RC4 Cipheri=0;j=0;for(k = 0; k < inLen; k++){r = sequence[k];i = (i + 1) % 128;j = (j + SBox[i]) % 128;temp = SBox[i];SBox[i] = SBox[j];SBox[j] = temp;t = (SBox[i] + SBox[j]) % 128;r = r ^ SBox[t];//out += QString::number(r, 16);out += QString(QChar(r));//char rr = char(r);//out += QString::fromUtf8(&rr);}textEdit->clear();textEdit->setText(out);delete[] sequence; }
其中,解密就是加密的逆過程,因此可以用同一個函數解密。 void SecretMainWindow::decipher() {encrypt(); }
  • 彈出密鑰輸入窗口
創建一個password類。

#ifndef PASSWORD_H #define PASSWORD_H#include<QDialog>class QLabel; class QPushButton; class QLineEdit;class PassWord:public QDialog {Q_OBJECT public:PassWord(QWidget* parent = 0);QString password; private:QLabel* passwordLabel;QLineEdit* passwordLineEdit;QPushButton* okButton;QPushButton* cancelButton; private slots:void slotOk();void slotCancel(); //signals://getPassword(const QString& password);}; #endif // PASSWORD_H
點擊【ok】,獲取密碼,點擊【Cancel】取消操作。 void PassWord::slotOk() {password = passwordLineEdit->text();//emit getPassword(password);this->accept(); }void PassWord::slotCancel() {password = QString::null;this->reject(); }
  • 效果演示
加密前:


加密后:

  • 程序下載
程序下載地址

用VS2008編譯,發布release版本

總結

以上是生活随笔為你收集整理的Qt4--加密日记本(子例化QMainWindow文本加密解密)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。