Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. //main.h
  2.  
  3. #pragma once
  4. #include <cmath>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <chrono>
  8. #include <string>
  9. void encryptFile(std::ifstream * file_to_encrypt, std::ofstream * output_file);
  10. void decryptFile(std::ifstream * file_to_decrypt, std::ofstream * output_file);
  11.  
  12. //main.cpp
  13.  
  14. #include "main.h"
  15.  
  16. #define BUFFER_TYPE int32_t
  17. #define BUFFER_SIZE 4 //in bytes
  18.  
  19. using namespace std;
  20.  
  21. namespace little_endian_io {
  22. template <typename Word>
  23. std::ostream& write_word(std::ostream& outs, Word value, unsigned size = sizeof(Word)) {
  24. for (; size; --size, value >>= 8)
  25. outs.put(static_cast <char> (value & 0xFF));
  26. return outs;
  27. }
  28. }
  29.  
  30. using namespace little_endian_io;
  31.  
  32. void encryptFile(ifstream& file_to_encrypt, ofstream& output_file) {
  33. size_t ifsize = file_to_encrypt.tellg();
  34. BUFFER_TYPE byte;
  35. for (int i = 0; i < ifsize / BUFFER_SIZE; i++) {
  36. file_to_encrypt.seekg(i * BUFFER_SIZE);
  37. file_to_encrypt.read((char*)&byte, BUFFER_SIZE);
  38. write_word(output_file, byte, BUFFER_SIZE);
  39. }
  40. }
  41.  
  42. void decryptFile(ifstream * file_to_decrypt, ofstream * output_file) {
  43.  
  44. }
  45.  
  46. long getCurrentTime() {
  47. return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
  48. }
  49.  
  50. struct WavInfo {
  51. char s1[16]; // "RIFF----WAVEfmt "
  52. int32_t extensionData = 16;
  53. int16_t type = 1;
  54. int16_t channelCount;
  55. int32_t hz;
  56. int32_t bitrate;
  57. int16_t dataBlockSize;
  58. int16_t bps;
  59. char s2[8] ; //"data----"
  60. };
  61.  
  62. WavInfo createWav(int HZ, int CHANNEL_COUNT, int BPS) {
  63. WavInfo wavInfo;
  64. wavInfo.hz = HZ;
  65. wavInfo.channelCount = CHANNEL_COUNT;
  66. wavInfo.bps = BPS;
  67. wavInfo.bitrate = wavInfo.hz * wavInfo.bps * wavInfo.channelCount / 8;
  68. wavInfo.dataBlockSize = wavInfo.channelCount * wavInfo.bps / 8;
  69. return wavInfo;
  70. }
  71.  
  72. void writeHeader(WavInfo& wavInfo, ofstream& ofs) {
  73. ofs.write((char*)&wavInfo, sizeof(wavInfo));
  74. ofs.seekp(0);
  75. ofs << "RIFF----WAVEfmt ";
  76. ofs.seekp(36);
  77. ofs << "data----";
  78. ofs.seekp(44);
  79. }
  80.  
  81. void writeAdditionalData(ofstream& ofs, size_t fileLength) {
  82. ofs.seekp(40);
  83. write_word(ofs, fileLength - 28);
  84. ofs.seekp(4);
  85. write_word(ofs, fileLength - 8, 4);
  86. }
  87.  
  88. int main() {
  89. ofstream ofs("encrypted.wav", ios::binary);
  90. WavInfo wavInfo = createWav(44100, 2, 16);
  91. writeHeader(wavInfo, ofs);
  92. ifstream ifs("file_to_encrypt", ios::binary | ios::ate);
  93. if (!ifs.good()) {
  94. printf("file_to_encrypt doesn't exists. Create it first\n");
  95. exit(0);
  96. }
  97.  
  98. long startTime = getCurrentTime();
  99. encryptFile(ifs, ofs);
  100. printf("File encrypted. This took %i ms\n", (getCurrentTime() - startTime));
  101.  
  102. size_t file_length = ofs.tellp();
  103. writeAdditionalData(ofs, file_length);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement