生成加密文本,到时候要看的时候,通过自己的软件读取就可以。既然有想法了,那就行动起来吧!
private slots: void open(); private: QAction* 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】设置一个图标
openAction->setStatusTip(tr("Open a file"));给【Open】设置状态提示
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));连接信号与槽
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();添加一个分隔符
void SecretMainWindow::open() { //========== 打开一个文件 ==========// toSave(); //如果内容发生变更,执行保存 fileName = QFileDialog::getOpenFileName(this); //获得要打开的文件的名字 if(!fileName.isEmpty()) //如果文件名不为空,加载文件 { loadFile(fileName); } textEdit->setVisible(true); //文本编辑器可见 }
void SecretMainWindow::creatToolBars() { //=========创建一个【file】工具栏=========// fileToolBar = addToolBar(tr("&File")); fileToolBar->addAction(newAction); fileToolBar->addAction(openAction); fileToolBar->addSeparator(); fileToolBar->addAction(saveAction); fileToolBar->addAction(saveAsAction); }
QToolBar* fileToolBar;其余创建过程与【file】菜单是一致的。
//=========创建一个【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 Sbox char 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 Cipher i=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(); }
#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
void PassWord::slotOk() { password = passwordLineEdit->text(); //emit getPassword(password); this->accept(); } void PassWord::slotCancel() { password = QString::null; this->reject(); }