Qt4--加密日记本(子例化QMainWindow文本加密解密)
- 近来刚学习Qt4编程,想找个实例练习练习,于是产生了一个想法,就是怎么样做一个文本加密,这样,自己保存的一些文档可以通过软件
生成加密文本,到时候要看的时候,通过自己的软件读取就可以。既然有想法了,那就行动起来吧!
- 加密解密采用RC4方法,目前只能处理英文文档。
- 首先介绍一下软件的框架
- 【file】下拉菜单包括【new】、【open】、【close】、【save】、【save as】、【exit】
- 【edit】下拉菜单包括【copy】、【cut】、【paste】、【undo】、【redo】
- 【option】下拉菜单包括【encrypt】、【decipher】
- 【help】下拉菜单包括【about】
- 【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】
- 菜单栏的实现举例
private slots:void open();
private:QAction* openAction;
其次、就是在**.cpp中创建这个QAction
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->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();
添加一个分隔符
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);
}
方法与建立【file】菜单类似。需要在头文件里建立一个工具栏的变量
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 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();
}
- 效果演示
加密前:
加密后:
- 程序下载
Qt4--加密日记本(子例化QMainWindow文本加密解密)相关推荐
- 蓝桥杯-9-2 文本加密
算法提高 9-2 文本加密 时间限制:1.0s 内存限制:256.0MB问题描述先编写函数EncryptChar,按照下述规则将给定的字符c转化(加密)为新的字符:"A"转化&qu ...
- java 蓝桥杯算法提高 9-2 文本加密
试题 算法提高 9-2 文本加密 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 先编写函数EncryptChar,按照下述规则将给定的字符c转化(加密)为新的字符:"A& ...
- Python函数式编程小案例:文本加密
问题描述:编写函数,接收一个文本和一个密钥字符串,实现文本加密和解密,要求使用异或算法,如果文本比密钥字符串长的话就循环使用指定密钥. 参考代码: 运行结果: ----------相关阅读------ ...
- 非结构化商业文本中隐私信息识别-第2名方案(含数据)
向AI转型的程序员都关注了这个号???????????? 人工智能大数据与深度学习 公众号:datayx 随着社交网络.移动通讯等技术的迅速发展,网络中存在大量包含隐私数据的文本信息,如何在非结构化 ...
- 蓝桥杯 算法提高 9-2 文本加密(c语言版详细注释)
试题 算法提高 9-2 文本加密 蓝桥杯试题解答汇总链接 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 先编写函数EncryptChar,按照下述规则将给定的字符c转化(加密)为新 ...
- 金仓数据库 KingbaseES PL/SQL 过程语言参考手册(16. A PL/SQL源文本加密)
16. A PL/SQL源文本加密 您可以为以下PL/SQL 单元加密 PL/SQL 源文本,从而防止任何人使用数据字典显示该文本: 包体 函数 存储过程 包含加密的 PL/SQL 源文本的文件称为加 ...
- 非结构化商业文本信息中隐私信息识别Baseline
简介 最近,CCF大数据与计算智能大赛出了好几个赛题. CCF BDCI比赛链接:https://www.datafountain.cn/special/BDCI2020 比赛baseline汇总:h ...
- 千寻的计算机字符,R语千寻 | 非结构化数据--文本数据的读入
原标题:R语千寻 | 非结构化数据--文本数据的读入 " 上一期(),我们介绍了普通数据读入的方法,一般来说,如果你的数据够规矩,够整齐,按照我们上期介绍的方法,注意注意路径.分隔符等常见问 ...
- 如何在Python中对字符串进行子字符串化
Python offers many ways to substring a string. It is often called 'slicing'. Python提供了许多对字符串进行子字符串化的 ...
- Vs code如何快速生成Verilog例化模板
问题描述 用过很多种编辑器,Vs code对与Verilog的语法支持,以及Vivado关联都是目前我最满意的. 也是我用过安装插件最方便的一款软件,最关键的是它不需要破解,直接在官网下载正版,安装即 ...
最新文章
- linux分区合并不损坏系统,一次Linux磁盘损坏导致系统不可用恢复实例
- 安卓:安卓工程构建及第一个安卓程序运行
- document引用图片的src属性能干嘛_如何实现图片懒加载
- Spring--总体架构
- uva 12545——Bits Equalizer
- 计算鞍点(信息学奥赛一本通-T1122)
- S2-MLPV2:目前最强的视觉MLP架构,空降榜一,达到83.6% Top-1准确率
- Python数据分析学习笔记:计算相关系数
- 备忘:gcc在linux下打包so库并调用
- Chrome调试vue项目时session异常,后端tp6 session使用异常
- 人在烟火间(外两章)
- 4.16 期货每日早盘操作建议
- js实现动画(移动方块)
- Unsupported operation: dart:isolate is not supported on dart4web
- Zepto中文API
- Windows Server 2012 如何实现多个用户远程桌面登陆?
- 禁用系统【快应用】,停止【快应用】自动弹出
- [31期]命运掌握在自己手中
- 如何在 Mac 上强制退出应用
- 电磁兼容入门篇之辐射发射(辐射骚扰)试验