#include "desecb.h"
DesEcb* DesEcb::instance = NULL;
DesEcb::DesEcb()
{
}
DesEcb *DesEcb::get_instance()
{
static QMutex mutex;
if (instance == NULL) {
mutex.lock();
if (instance == NULL) {
instance = new DesEcb();
}
mutex.unlock();
}
return instance;
}
// 加密函数
// 加密 ecb模式
QByteArray DesEcb::des_encrypt(const std::string &clearText, const std::string &key)
{
// DES加密分为两个阶段
// 第一阶段是设置密钥,从密钥生成DES_key_schedule
// 第二阶段是实际加密
std::string cipherText; // 密文
// 第一阶段,设置密钥
// DES的密钥类型是DES_cblock,8位
DES_cblock keyEncrypt;
memset(keyEncrypt, 0, 8);
// 构造补齐后的密钥
if(key.length() <= 8)
memcpy(keyEncrypt, key.c_str(), key.length());
else
memcpy(keyEncrypt, key.c_str(), 8);
// 密钥置换
DES_key_schedule keySchedule;
DES_set_key_unchecked(&keyEncrypt, &keySchedule);
//DES_set_key_checked(&keyEncrypt, &keySchedule);
// 第二阶段,实际加密
// 循环加密,每8字节一次
const_DES_cblock inputText; // 加密前的输入8字节
DES_cblock outputText; // 加密后的输出
std::vector<unsigned char> vecCiphertext; // 加密后的密文
unsigned char tmp[8]; // 记录加密过程中的数据块的数据
for(int i = 0; i < clearText.length() / 8; i++) {
memcpy(inputText, clearText.c_str() + i * 8, 8);
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_ENCRYPT);
memcpy(tmp, outputText, 8);
for(int j = 0; j < 8; j++)
vecCiphertext.push_back(tmp[j]);
}
// PkCs5padding填充模式
// 数据块大小固定为8数据长度
// 需要填充n(n>0)个字节才对齐,那么填充n个字节,每个字节都是n
// 如果数据本身就已经对齐了,则填充一块长度为块大小的数据,每个字节都是块大小8
if(clearText.length() % 8 == 0) {
//PkCs5padding
memset(inputText, 8, 8);
// 加密函数
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_ENCRYPT);
memcpy(tmp, outputText, 8);
for(int j = 0; j < 8; j++)
vecCiphertext.push_back(tmp[j]);
}
if(clearText.length() % 8 != 0) {
// PkCs5padding
int tmp1 = clearText.length() / 8 * 8;
int tmp2 = clearText.length() - tmp1;
int padNumber = 8 - tmp2;//最后一个需要补齐的数据块需要补齐的位数
memset(inputText, padNumber, 8);
memcpy(inputText, clearText.c_str() + tmp1, tmp2); //最后一个补齐的数据块
// 加密函数
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_ENCRYPT); //DES_ENCRYPT代表加密
memcpy(tmp, outputText, 8);
for(int j = 0; j < 8; j++)
vecCiphertext.push_back(tmp[j]);
}
cipherText.clear();
cipherText.assign(vecCiphertext.begin(), vecCiphertext.end());
QByteArray tempArray;
foreach (auto var, vecCiphertext) {
tempArray.append(var);
}
return tempArray;
}
// 解密
std::string DesEcb::des_decrypt(const std::string &cipherText, const std::string &key)
{
std::string clearText; // 明文
DES_cblock keyEncrypt;
memset(keyEncrypt, 0, 8);
if(key.length() <= 8)
memcpy(keyEncrypt, key.c_str(), key.length());
else
memcpy(keyEncrypt, key.c_str(), 8);
DES_key_schedule keySchedule;
DES_set_key_unchecked(&keyEncrypt, &keySchedule);
const_DES_cblock inputText;
DES_cblock outputText;
std::vector<unsigned char> vecCleartext;
unsigned char tmp[8];
for(int i = 0; i < cipherText.length() / 8; i++) {
memcpy(inputText, cipherText.c_str() + i * 8, 8);
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_DECRYPT); //DES_DECRYPT代表解密
memcpy(tmp, outputText, 8);
for (int j = 0; j < 8; j++) {
if(tmp[j] > 31) //31是什么意思???????????????????
vecCleartext.push_back(tmp[j]);
}
}
if(cipherText.length() % 8 != 0) {
int tmp1 = cipherText.length() / 8 * 8;
int tmp2 = cipherText.length() - tmp1;
memset(inputText, 0, 8);
memcpy(inputText, cipherText.c_str() + tmp1, tmp2);
// 解密函数
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_DECRYPT);
memcpy(tmp, outputText, 8);
for(int j = 0; j < 8; j++)
vecCleartext.push_back(tmp[j]);
}
clearText.clear();
clearText.assign(vecCleartext.begin(), vecCleartext.end());
return clearText;
}
QString DesEcb::get_encryption_str(QString data)
{
QByteArray ret = des_encrypt(data.toUtf8().data(), key.toStdString()).toHex();
return QString(ret);
}
QString DesEcb::get_decryption_str(QString data)
{
QByteArray tmp = QByteArray::fromHex(data.toUtf8());
std::string stdString(tmp.constData(), tmp.length());
QString ret = QString::fromStdString(des_decrypt(stdString, key.toStdString()));
return ret;
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt实现DES ECB加密解密
共117个文件
h:108个
cpp:3个
dll:2个
需积分: 0 21 下载量 165 浏览量
2023-05-19
16:50:57
上传
评论
收藏 2.03MB ZIP 举报
温馨提示
包含MinGW编译的OpenSSL库文件、调用库文件实现DES ECB模式的加密解密。
资源推荐
资源详情
资源评论
收起资源包目录
Qt实现DES ECB加密解密 (117个子文件)
desecb.cpp 5KB
mainwindow.cpp 556B
main.cpp 185B
libcrypto-1_1.dll 3.34MB
libssl-1_1.dll 873KB
obj_mac.h 212KB
ssl.h 109KB
evp.h 73KB
tls1.h 71KB
ec.h 62KB
sslerr.h 46KB
x509.h 42KB
bio.h 34KB
engine.h 34KB
asn1.h 33KB
x509v3.h 33KB
asn1t.h 32KB
x509_vfy.h 31KB
ts.h 22KB
rsa.h 22KB
bn.h 22KB
crypto.h 17KB
cms.h 16KB
ui.h 16KB
ct.h 16KB
ecerr.h 15KB
pem.h 15KB
ocsp.h 15KB
asn1err.h 14KB
ssl3.h 14KB
dh.h 13KB
pkcs7.h 11KB
evperr.h 11KB
err.h 11KB
store.h 11KB
cmserr.h 11KB
modes.h 10KB
pkcs12.h 10KB
dsa.h 9KB
lhash.h 9KB
rsaerr.h 9KB
e_os2.h 9KB
x509v3err.h 9KB
safestack.h 8KB
des.h 7KB
x509err.h 7KB
tserr.h 7KB
objects.h 6KB
bioerr.h 6KB
ossl_typ.h 6KB
conf.h 5KB
engineerr.h 5KB
pkcs7err.h 5KB
pemerr.h 5KB
bnerr.h 5KB
rand_drbg.h 5KB
opensslconf.h 5KB
randerr.h 4KB
storeerr.h 4KB
kdf.h 4KB
opensslv.h 4KB
dherr.h 4KB
sha.h 4KB
srp.h 4KB
pkcs12err.h 4KB
seed.h 3KB
cterr.h 3KB
conferr.h 3KB
ocsperr.h 3KB
aes.h 3KB
camellia.h 3KB
stack.h 3KB
dsaerr.h 3KB
uierr.h 3KB
async.h 2KB
cryptoerr.h 2KB
rand.h 2KB
kdferr.h 2KB
idea.h 2KB
rc5.h 2KB
blowfish.h 2KB
cast.h 2KB
txt_db.h 2KB
dtls1.h 2KB
buffer.h 2KB
hmac.h 2KB
rc2.h 1KB
whrlpool.h 1KB
comp.h 1KB
asyncerr.h 1KB
md4.h 1KB
md5.h 1KB
srtp.h 1KB
objectserr.h 1KB
symhacks.h 1KB
conf_api.h 1KB
ripemd.h 1KB
comperr.h 1KB
cmac.h 1KB
md2.h 1KB
共 117 条
- 1
- 2
资源评论
小小小将
- 粉丝: 7
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功