开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 1092|回复: 2
收起左侧

[C/C++] 求翻译一段C代码为易代码

[复制链接]
结帖率:81% (34/42)
发表于 2019-4-27 12:56:30 | 显示全部楼层 |阅读模式   吉林省长春市
100精币
  
#include "stdafx.h"
#include
#include
#include
#include
#include

// #define ANDROID_WECHAT

#define SQLITE_FILE_HEADER "SQLite format 3" //length == 16
#define IV_SIZE 16
#define HMAC_SHA1_SIZE 20
#define KEY_SIZE 32

#ifndef ANDROID_WECHAT
#define DEFAULT_PAGESIZE 4096
#define DEFAULT_ITER 64000
#else
#define NO_USE_HMAC_SHA1
#define DEFAULT_PAGESIZE 1024
#define DEFAULT_ITER 4000
#endif
unsigned char pass[] = { 0x5C, 0xF8, 0x6A, 0x13, 0x61, 0xA1, 0x46, 0x14, 0x98, 0x6C, 0x2D, 0x6F, 0x5E, 0x6C, 0x16, 0x81, 0xB8, 0xCF, 0x5D, 0x3F, 0xD2, 0xEB, 0x49, 0xCE, 0xAF, 0xFB, 0x88, 0xE3, 0xD8, 0x28, 0xC7, 0xBD };

int _tmain (int argc, _TCHAR* argv[])
{
FILE *fpdb = fopen ("MicroMsg.db", "rb+");
if (!fpdb)
{
return 0;
}
fseek (fpdb, 0, SEEK_END);
long nFileSize = ftell (fpdb);
fseek (fpdb, 0, SEEK_SET);
unsigned char *pDbBuffer = new unsigned char[nFileSize];
fread (pDbBuffer, 1, nFileSize, fpdb);
fclose (fpdb);

unsigned char salt[16] = { 0 };
memcpy (salt, pDbBuffer, 16);

#ifndef NO_USE_HMAC_SHA1
unsigned char mac_salt[16] = { 0 };
memcpy (mac_salt, salt, 16);
for (int i = 0; i < sizeof (salt); i++)
{
mac_salt[i] ^= 0x3a;
}
#endif

int reserve = IV_SIZE;
#ifndef NO_USE_HMAC_SHA1
reserve += HMAC_SHA1_SIZE;
#endif
reserve = ( (reserve % AES_BLOCK_SIZE) == 0) ? reserve : ( (reserve / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;

unsigned char key[KEY_SIZE] = { 0 };
unsigned char mac_key[KEY_SIZE] = { 0 };

OpenSSL_add_all_algorithms ();
PKCS5_PBKDF2_HMAC_SHA1 ( (const char *)pass, sizeof (pass), salt, sizeof (salt), DEFAULT_ITER, sizeof (key), key);
#ifndef NO_USE_HMAC_SHA1
PKCS5_PBKDF2_HMAC_SHA1 ( (const char *)key, sizeof (key), mac_salt, sizeof (mac_salt), 2, sizeof (mac_key), mac_key);
#endif

unsigned char *pTemp = pDbBuffer;
unsigned char pDecryptPerPageBuffer[DEFAULT_PAGESIZE];
int nPage = 1;
int offset = 16;
while (pTemp < pDbBuffer + nFileSize)
{
printf ("decrypt page:%d/%d \n", nPage, nFileSize / DEFAULT_PAGESIZE);

#ifndef NO_USE_HMAC_SHA1
//check hmac
unsigned char hash_mac[HMAC_SHA1_SIZE] = { 0 };
unsigned int hash_len = 0;
HMAC_CTX hctx;
HMAC_CTX_init (&hctx);
HMAC_Init_ex (&hctx, mac_key, sizeof (mac_key), EVP_sha1 (), NULL);
HMAC_Update (&hctx, pTemp + offset, DEFAULT_PAGESIZE - reserve - offset + IV_SIZE);
HMAC_Update (&hctx, (const unsigned char *)&nPage, sizeof (nPage));
HMAC_Final (&hctx, hash_mac, &hash_len);
HMAC_CTX_cleanup (&hctx);
if (0 != memcmp (hash_mac, pTemp + DEFAULT_PAGESIZE - reserve + IV_SIZE, sizeof (hash_mac)))
{
//hash check err
return 0;
}
#endif
//
if (nPage == 1)
{
memcpy (pDecryptPerPageBuffer, SQLITE_FILE_HEADER, offset);
}

//aes decrypt
EVP_CIPHER_CTX* ectx = EVP_CIPHER_CTX_new ();
EVP_CipherInit_ex (ectx, EVP_get_cipherbyname ("aes-256-cbc"), NULL, NULL, NULL, 0);
EVP_CIPHER_CTX_set_padding (ectx, 0);
EVP_CipherInit_ex (ectx, NULL, NULL, key, pTemp + (DEFAULT_PAGESIZE - reserve), 0);

int nDecryptLen = 0;
int nTotal = 0;
EVP_CipherUpdate (ectx, pDecryptPerPageBuffer + offset, &nDecryptLen, pTemp + offset, DEFAULT_PAGESIZE - reserve - offset);
nTotal = nDecryptLen;
EVP_CipherFinal_ex (ectx, pDecryptPerPageBuffer + offset + nDecryptLen, &nDecryptLen);
nTotal += nDecryptLen;
EVP_CIPHER_CTX_free (ectx);

//assert (nTotal == DEFAULT_PAGESIZE - reserve - offset);

//no necessary ,just like sqlcipher
memcpy (pDecryptPerPageBuffer + DEFAULT_PAGESIZE - reserve, pTemp + DEFAULT_PAGESIZE - reserve, reserve);

FILE *fp = fopen ("MicroMsg_Decrypt.db", "ab+");
{
fwrite (pDecryptPerPageBuffer, 1, DEFAULT_PAGESIZE, fp);
fclose (fp);
}

nPage++;
offset = 0;
pTemp += DEFAULT_PAGESIZE;
}
return 0;





可能粘贴的有点乱  原文地址“https://bbs.pediy.com/thread-222652.htm”


回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值无答案申请取消悬赏投诉有答案未采纳为最佳
结帖率:94% (78/83)

签到天数: 13 天

发表于 2019-4-27 21:55:38 | 显示全部楼层   江苏省徐州市
太长了。。。。。
回复

使用道具 举报

结帖率:0% (0/1)
发表于 2019-5-7 20:08:28 | 显示全部楼层   广东省肇庆市
............
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表