Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.h
- #pragma once
- #include <cmath>
- #include <fstream>
- #include <iostream>
- #include <chrono>
- #include <string>
- void encryptFile(std::ifstream * file_to_encrypt, std::ofstream * output_file);
- void decryptFile(std::ifstream * file_to_decrypt, std::ofstream * output_file);
- //main.cpp
- #include "main.h"
- #define BUFFER_TYPE int32_t
- #define BUFFER_SIZE 4 //in bytes
- using namespace std;
- namespace little_endian_io {
- template <typename Word>
- std::ostream& write_word(std::ostream& outs, Word value, unsigned size = sizeof(Word)) {
- for (; size; --size, value >>= 8)
- outs.put(static_cast <char> (value & 0xFF));
- return outs;
- }
- }
- using namespace little_endian_io;
- void encryptFile(ifstream& file_to_encrypt, ofstream& output_file) {
- size_t ifsize = file_to_encrypt.tellg();
- BUFFER_TYPE byte;
- for (int i = 0; i < ifsize / BUFFER_SIZE; i++) {
- file_to_encrypt.seekg(i * BUFFER_SIZE);
- file_to_encrypt.read((char*)&byte, BUFFER_SIZE);
- write_word(output_file, byte, BUFFER_SIZE);
- }
- }
- void decryptFile(ifstream * file_to_decrypt, ofstream * output_file) {
- }
- long getCurrentTime() {
- return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
- }
- struct WavInfo {
- char s1[16]; // "RIFF----WAVEfmt "
- int32_t extensionData = 16;
- int16_t type = 1;
- int16_t channelCount;
- int32_t hz;
- int32_t bitrate;
- int16_t dataBlockSize;
- int16_t bps;
- char s2[8] ; //"data----"
- };
- WavInfo createWav(int HZ, int CHANNEL_COUNT, int BPS) {
- WavInfo wavInfo;
- wavInfo.hz = HZ;
- wavInfo.channelCount = CHANNEL_COUNT;
- wavInfo.bps = BPS;
- wavInfo.bitrate = wavInfo.hz * wavInfo.bps * wavInfo.channelCount / 8;
- wavInfo.dataBlockSize = wavInfo.channelCount * wavInfo.bps / 8;
- return wavInfo;
- }
- void writeHeader(WavInfo& wavInfo, ofstream& ofs) {
- ofs.write((char*)&wavInfo, sizeof(wavInfo));
- ofs.seekp(0);
- ofs << "RIFF----WAVEfmt ";
- ofs.seekp(36);
- ofs << "data----";
- ofs.seekp(44);
- }
- void writeAdditionalData(ofstream& ofs, size_t fileLength) {
- ofs.seekp(40);
- write_word(ofs, fileLength - 28);
- ofs.seekp(4);
- write_word(ofs, fileLength - 8, 4);
- }
- int main() {
- ofstream ofs("encrypted.wav", ios::binary);
- WavInfo wavInfo = createWav(44100, 2, 16);
- writeHeader(wavInfo, ofs);
- ifstream ifs("file_to_encrypt", ios::binary | ios::ate);
- if (!ifs.good()) {
- printf("file_to_encrypt doesn't exists. Create it first\n");
- exit(0);
- }
- long startTime = getCurrentTime();
- encryptFile(ifs, ofs);
- printf("File encrypted. This took %i ms\n", (getCurrentTime() - startTime));
- size_t file_length = ofs.tellp();
- writeAdditionalData(ofs, file_length);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement