Qt4--加密日记本(子例化QMainWindow文本加密解密)
生活随笔
收集整理的這篇文章主要介紹了
Qt4--加密日记本(子例化QMainWindow文本加密解密)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
- 近來剛學(xué)習(xí)Qt4編程,想找個實例練習(xí)練習(xí),于是產(chǎn)生了一個想法,就是怎么樣做一個文本加密,這樣,自己保存的一些文檔可以通過軟件
生成加密文本,到時候要看的時候,通過自己的軟件讀取就可以。既然有想法了,那就行動起來吧!
- 加密解密采用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】
編輯區(qū):文本編輯框
狀態(tài)欄:顯示行列
- 菜單欄的實現(xiàn)舉例
首先、在頭文件中定義一個動作,然后為這個動作添加一個需要執(zhí)行的槽 private slots:void open(); private:QAction* openAction;
其次、就是在**.cpp中創(chuàng)建這個QAction 在這里為了各個QAction的創(chuàng)建和維護的便利性,我定義了一個creatAction()函數(shù),如下是創(chuàng)建openAction的代碼: void SecretMainWindow::creatAction() {//=========創(chuàng)建一個【file】->【Open】的動作響應(yīng)=========//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);創(chuàng)建一個新的QAction,標(biāo)簽是【Open】
openAction->setIcon(QIcon(":/images/open.png"));給【Open】設(shè)置一個圖標(biāo) 詳見:Qt--創(chuàng)建自定義圖標(biāo)
openAction->setShortcut(QKeySequence::Open);給【Open】創(chuàng)建一個快捷鍵【Ctrl + O】 openAction->setStatusTip(tr("Open a file"));給【Open】設(shè)置狀態(tài)提示
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));連接信號與槽
然后、creatMenu()函數(shù)實現(xiàn)創(chuàng)建
void SecretMainWindow::creatMenus() {//=========創(chuàng)建一個【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"));創(chuàng)建一個【file】菜單,并且命名為【File】
fileMenu->addAction(newAction);添加【New】到菜單【File】 fileMenu->addSeparator();添加一個分隔符
最后、實現(xiàn)槽函數(shù)open() void SecretMainWindow::open() {//========== 打開一個文件 ==========//toSave(); //如果內(nèi)容發(fā)生變更,執(zhí)行保存fileName = QFileDialog::getOpenFileName(this); //獲得要打開的文件的名字if(!fileName.isEmpty()) //如果文件名不為空,加載文件{loadFile(fileName);}textEdit->setVisible(true); //文本編輯器可見 }
- 工具欄的實現(xiàn)舉例
情況一:把菜單欄里功能添加到工具欄,如【open】 void SecretMainWindow::creatToolBars() {//=========創(chuàng)建一個【file】工具欄=========//fileToolBar = addToolBar(tr("&File"));fileToolBar->addAction(newAction);fileToolBar->addAction(openAction);fileToolBar->addSeparator();fileToolBar->addAction(saveAction);fileToolBar->addAction(saveAsAction); }
方法與建立【file】菜單類似。需要在頭文件里建立一個工具欄的變量 QToolBar* fileToolBar;其余創(chuàng)建過程與【file】菜單是一致的。
情況二:創(chuàng)建工具欄特有的功能,如字體Font //=========創(chuàng)建一個【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"));在工具欄上添加一個設(shè)置字體的工具欄 fontstyleLabel = new QLabel(tr("FontType:"));新建一個標(biāo)簽:【FontType】
fontBox = new QFontComboBox;新建一個字體下拉框 fontBox->setFontFilters(QFontComboBox::ScalableFonts);把字體添加到下拉框 fontToolBar->addWidget(fontstyleLabel);fontToolBar->addWidget(fontBox);把標(biāo)簽和下拉框添加到工具欄上
- 更新狀態(tài)欄
statusBar()->addWidget(locationLabel);把狀態(tài)欄標(biāo)簽添加到狀態(tài)欄上 connect(textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(updateStatusBar()));當(dāng)光標(biāo)位置發(fā)生變化的時候,觸發(fā)更新狀態(tài)欄 locationLabel->setText(tr("%1 row %2 col").arg(textEdit->document()->blockCount()).arg(textEdit->textCursor().columnNumber()));顯示出光標(biāo)的位置,其中1,2分別對應(yīng)后面的兩個參數(shù)
- 加密解密
text.append(textEdit->toPlainText());獲取編輯區(qū)的文檔內(nèi)容 passwordDlg = new PassWord(this);新建一個彈出窗口,輸入加密密鑰
passwordDlg->exec()運行彈出窗口 if(!passwordDlg->password.isNull())RC4(text, passwordDlg->password);確定獲取到密鑰后,進行RC4加密,參考網(wǎng)上的程序。
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; }
其中,解密就是加密的逆過程,因此可以用同一個函數(shù)解密。 void SecretMainWindow::decipher() {encrypt(); }
- 彈出密鑰輸入窗口
#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編譯,發(fā)布release版本
總結(jié)
以上是生活随笔為你收集整理的Qt4--加密日记本(子例化QMainWindow文本加密解密)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 名词解释 算法的有限性_数据结构与算法期
- 下一篇: html5 怎么入门,初学HTML5 从