| 43 | } |
| 44 | |
| 45 | int Decryptdb() |
| 46 | { |
| 47 | FILE* fpdb; |
| 48 | fopen_s(&fpdb, dbfilename, "rb+"); |
| 49 | if (!fpdb) |
| 50 | { |
| 51 | printf("打开文件错!"); |
| 52 | getchar(); |
| 53 | return 0; |
| 54 | } |
| 55 | fseek(fpdb, 0, SEEK_END); |
| 56 | long nFileSize = ftell(fpdb); |
| 57 | fseek(fpdb, 0, SEEK_SET); |
| 58 | unsigned char* pDbBuffer = new unsigned char[nFileSize]; |
| 59 | fread(pDbBuffer, 1, nFileSize, fpdb); |
| 60 | fclose(fpdb); |
| 61 | |
| 62 | unsigned char salt[16] = { 0 }; |
| 63 | memcpy(salt, pDbBuffer, 16); |
| 64 | |
| 65 | #ifndef NO_USE_HMAC_SHA1 |
| 66 | unsigned char mac_salt[16] = { 0 }; |
| 67 | memcpy(mac_salt, salt, 16); |
| 68 | for (int i = 0; i < sizeof(salt); i++) |
| 69 | { |
| 70 | mac_salt[i] ^= 0x3a; |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | int reserve = IV_SIZE; //校验码长度,PC端每4096字节有48字节 |
| 75 | #ifndef NO_USE_HMAC_SHA1 |
| 76 | reserve += HMAC_SHA1_SIZE; |
| 77 | #endif |
| 78 | reserve = ((reserve % AES_BLOCK_SIZE) == 0) ? reserve : ((reserve / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE; |
| 79 | |
| 80 | unsigned char key[KEY_SIZE] = { 0 }; |
| 81 | unsigned char mac_key[KEY_SIZE] = { 0 }; |
| 82 | |
| 83 | OpenSSL_add_all_algorithms(); |
| 84 | PKCS5_PBKDF2_HMAC_SHA1((const char*)pass, sizeof(pass), salt, sizeof(salt), DEFAULT_ITER, sizeof(key), key); |
| 85 | #ifndef NO_USE_HMAC_SHA1 |
| 86 | PKCS5_PBKDF2_HMAC_SHA1((const char*)key, sizeof(key), mac_salt, sizeof(mac_salt), 2, sizeof(mac_key), mac_key); |
| 87 | #endif |
| 88 | |
| 89 | unsigned char* pTemp = pDbBuffer; |
| 90 | unsigned char pDecryptPerPageBuffer[DEFAULT_PAGESIZE]; |
| 91 | int nPage = 1; |
| 92 | int offset = 16; |
| 93 | while (pTemp < pDbBuffer + nFileSize) |
| 94 | { |
| 95 | printf("解密数据页:%d/%d \n", nPage, nFileSize / DEFAULT_PAGESIZE); |
| 96 | |
| 97 | #ifndef NO_USE_HMAC_SHA1 |
| 98 | unsigned char hash_mac[HMAC_SHA1_SIZE] = { 0 }; |
| 99 | unsigned int hash_len = 0; |
| 100 | HMAC_CTX hctx; |
| 101 | HMAC_CTX_init(&hctx); |
| 102 | HMAC_Init_ex(&hctx, mac_key, sizeof(mac_key), EVP_sha1(), NULL); |