mauronofrio | 6d3bf89 | 2019-10-26 19:47:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 Mauronofrio |
| 3 | |
| 4 | This file is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This file is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | GNU General Public License <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include <string.h> |
| 19 | #include <openssl/evp.h> |
| 20 | #include <openssl/ssl.h> |
| 21 | #define _FILE_OFFSET_BITS 64 |
| 22 | //extern "C" __int64 __cdecl _ftelli64(FILE*); |
| 23 | |
| 24 | using namespace std; |
| 25 | typedef std::basic_string<unsigned char> u_string; |
| 26 | |
| 27 | int decrypt(unsigned char* ciphertext, int ciphertext_len, unsigned char* key, |
| 28 | unsigned char* iv, unsigned char* plaintext) |
| 29 | { |
| 30 | EVP_CIPHER_CTX* ctx; |
| 31 | int len; |
| 32 | int plaintext_len; |
| 33 | ctx = EVP_CIPHER_CTX_new(); |
| 34 | EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv); |
| 35 | EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len); |
| 36 | plaintext_len = len; |
| 37 | EVP_DecryptFinal_ex(ctx, plaintext + len, &len); |
| 38 | plaintext_len += len; |
| 39 | EVP_CIPHER_CTX_free(ctx); |
| 40 | return plaintext_len; |
| 41 | } |
| 42 | |
| 43 | std::string hexToASCII(string hex) |
| 44 | { |
| 45 | int len = hex.length(); |
| 46 | std::string newString; |
| 47 | for (int i = 0; i < len; i += 2) |
| 48 | { |
| 49 | string byte = hex.substr(i, 2); |
| 50 | char chr = (char)(int)strtol(byte.c_str(), nullptr, 16); |
| 51 | newString.push_back(chr); |
| 52 | } |
| 53 | return newString; |
| 54 | } |
| 55 | |
| 56 | bool testkey(const char* keyf, const char* path) { |
| 57 | u_string key = (unsigned char*)(hexToASCII(keyf)).c_str(); |
| 58 | int data[17]; |
| 59 | FILE* fps = fopen(path, "rb"); |
| 60 | fseek(fps, 4176, SEEK_SET); |
| 61 | fread(data, sizeof(char), 16, fps); |
| 62 | fclose(fps); |
| 63 | u_string udata = (unsigned char*)data; |
| 64 | EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); |
| 65 | EVP_CIPHER_CTX_init(ctx); |
| 66 | EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key.c_str(), NULL); |
| 67 | EVP_CIPHER_CTX_set_padding(ctx, false); |
| 68 | unsigned char buffer[1024], * pointer = buffer; |
| 69 | int outlen; |
| 70 | EVP_DecryptUpdate(ctx, pointer, &outlen, udata.c_str(), udata.length()); |
| 71 | pointer += outlen; |
| 72 | EVP_DecryptFinal_ex(ctx, pointer, &outlen); |
| 73 | pointer += outlen; |
| 74 | EVP_CIPHER_CTX_free(ctx); |
| 75 | u_string test= u_string(buffer, pointer - buffer); |
| 76 | u_string checktest = test.substr(0, 4); |
| 77 | if (checktest == ((unsigned char*) "\x50\x4B\x03\x04") || checktest == ((unsigned char*) "\x41\x4E\x44\x52")) { |
| 78 | return true; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | int main(int argc, char* argv[]) |
| 84 | { |
| 85 | |
| 86 | if (argc != 3) |
| 87 | { |
| 88 | printf("Usage: ozipdecrypt key [*.ozip]\n"); |
| 89 | return 0; |
| 90 | } |
| 91 | const char* key = argv[1]; |
| 92 | const char* path = argv[2]; |
| 93 | FILE* fp = fopen(path, "rb"); |
| 94 | char magic[13]; |
| 95 | fgets(magic, sizeof(magic), fp); |
| 96 | string temp(path); |
| 97 | temp = (temp.substr(0, temp.size() - 5)).append(".zip"); |
| 98 | const char* destpath= temp.c_str(); |
| 99 | if (strcmp(magic, "OPPOENCRYPT!") != 0) |
| 100 | { |
| 101 | printf("This is not an .ozip file!\n"); |
| 102 | fclose(fp); |
| 103 | int rencheck = rename(path, destpath); |
| 104 | if (rencheck == 0) { |
| 105 | printf("Renamed .ozip file in .zip file\n"); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | printf("Unable to rename .ozip file in .zip file\n"); |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | if (testkey(key, path) == false) |
| 114 | { |
| 115 | printf("Key is not good!\n"); |
| 116 | fclose(fp); |
| 117 | return 0; |
| 118 | } |
| 119 | else { |
| 120 | printf("Key is good!\n"); |
| 121 | } |
| 122 | FILE* fp2 = fopen(destpath, "wb"); |
| 123 | fseek(fp, 0L, SEEK_END); |
| 124 | unsigned long int sizetot = ftello(fp); |
| 125 | fseek(fp, 4176, SEEK_SET); |
| 126 | int bdata[16384]; |
| 127 | unsigned long int sizeseek; |
| 128 | printf("Decrypting...\n"); |
| 129 | while (true) |
| 130 | { |
| 131 | unsigned char data[17]; |
| 132 | fread(data, sizeof(char), 16, fp); |
| 133 | decrypt(data, sizeof(data), (unsigned char*)(hexToASCII(key)).c_str(), NULL, data); |
| 134 | fwrite(data, sizeof(char), 16, fp2); |
| 135 | sizeseek = ftello(fp); |
| 136 | if ((sizetot - sizeseek) <= 16384) { |
| 137 | fread(bdata, sizeof(char), (sizetot - sizeseek), fp); |
| 138 | fwrite(bdata, sizeof(char), (sizetot - sizeseek), fp2); |
| 139 | break; |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | fread(bdata, sizeof(char), 16384, fp); |
| 144 | fwrite(bdata, sizeof(char), 16384, fp2); |
| 145 | } |
| 146 | } |
| 147 | printf("File succesfully decrypted, saved in %s\n", destpath); |
| 148 | fclose(fp2); |
| 149 | fclose(fp); |
| 150 | return 0; |
| 151 | } |
| 152 | |