u32 sig_decrypto_package(u8 *package_buf, u32 package_len) { u32 err_id = ERROR_SUCCESS; FILE *fdout = NULL; u8 ivnum[PACKAGE_IV_LENGTH]; u8 *out_buf = NULL; u32 out_buf_len = 0; u32 out_len = 0; u32 temp_len = 0; memcpy(ivnum, &package_buf[PACKAGE_IV_POSITION], PACKAGE_IV_LENGTH); out_buf_len = package_len*2; out_buf = malloc(out_buf_len); if(NULL == out_buf) { err_id = ERROR_SYSTEM; goto decrypto_package_label; } memset(out_buf, 0, out_buf_len); package_len -= PACKAGE_SECRET_POSITION; err_id = (u32)cipher_file(ivnum, &package_buf[PACKAGE_RANDNUM_POSITION], PACKAGE_RANDNUM_LENGTH, package_buf+PACKAGE_SECRET_POSITION, (int)package_len, out_buf, (int *)(&out_len), DECRYPT); if(ERROR_SUCCESS != err_id) { goto decrypto_package_label; } fdout = fopen("/tmp/upgrade/cam_char_upgrade.tar.gz", "wb"); if(NULL == fdout) { err_id = ERROR_SYSTEM; goto decrypto_package_label; } temp_len = fwrite((char *)out_buf, 1, out_len, fdout); if(temp_len != out_len) { err_id = ERROR_SYSTEM; goto decrypto_package_label; } decrypto_package_label: if(NULL != out_buf) { free(out_buf); out_buf = NULL; } if(NULL != fdout) { fclose(fdout); fdout = NULL; } return err_id; }
int main() { std::ifstream cipher_file("assets/p059_cipher.txt"); std::string num; std::string cipher; while (std::getline(cipher_file, num, ',')) { cipher += (char)std::stoi(num); } std::string decrypted = find_key(cipher); int sum = std::accumulate(decrypted.begin(), decrypted.end(), 0); std::cout << "Sum of ASCII values of decrypted text: " << sum << "\n"; return 0; }
void* cipher_dir2(void* st) { struct thread_arg* th_pt = (struct thread_arg*)st; DIR* d; struct dirent* r; struct stat chk; char* name = NULL; unsigned char f_path[4096] = {0}; int i = 0; int j = 0; int b = 0; int rc1 = 0; pthread_t th1; pthread_attr_t attr1; pthread_attr_init(&attr1); pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_JOINABLE); while ((th_pt->in_path[i] != '\0') && (i < 4096)) { f_path[i] = th_pt->in_path[i]; ++i; } f_path[i++] = '/'; dbug_p("PATH is:%s:\n",th_pt->in_path); if ((d = opendir(th_pt->in_path)) == NULL) { perror("\n ERROR,Opendir_C_DIR::"); exit(1); } //Setting errno to 0 to diffrentiate between end of dir & ERROR errno = 0; while ((r = readdir(d)) != NULL) { j = 0; b = i; name = r->d_name; if ((name[0] == 46) && ((name[1] == 46) || (name[1] == '\0'))) continue; //handling '.' & '..' while ((name[j] != '\0') && (b < 4095) && (j < 256)) f_path[b++] = name[j++]; if(b < 4095) { f_path[b] = '\0'; dbug_p("F_PATH:%s:\n",f_path); } else { printf("\n ERROR,PATH TOO LONG_C_DIR\n"); exit(1); } if (stat(f_path, &chk) != 0) { perror("\n ERROR,stat_C_DIR::"); exit(1); } if (S_IFDIR == (chk.st_mode & S_IFMT)) { struct thread_arg chk; strncpy(chk.in_path, f_path, sizeof(f_path)); dbug_p("PATH copied to struct:%s:\n",chk.in_path); chk.di_tx = th_pt->di_tx; chk.ci_flag = th_pt->ci_flag; rc1 = pthread_create(&th1, &attr1, cipher_dir2, &chk); if (rc1) { printf("\n ERROR in thread_cre_c_d_2\n"); exit(1); } pthread_join(th1, NULL); pthread_attr_destroy(&attr1); } else if (cipher_file(f_path, th_pt->di_tx, th_pt->ci_flag)) { dbug_p("ERROR,CANT DO CRYPT_C_DIR\n"); exit(1); } } if (errno && (!r)) { perror("\n ERROR,readdir_C_DIR::"); exit(1); } closedir(d); pthread_exit(NULL); return (void *)1; }