Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.43 KB | None | 0 0
  1. #pragma once
  2. #using <System.Security.dll>
  3.  
  4. namespace Project_2 {
  5.  
  6.     using namespace System;
  7.     using namespace System::ComponentModel;
  8.     using namespace System::Collections;
  9.     using namespace System::Windows::Forms;
  10.     using namespace System::Data;
  11.     using namespace System::Drawing;
  12.     using namespace System::IO;
  13.     using namespace System::Security::Cryptography;
  14.  
  15.     /// <summary>
  16.     /// Сводка для Form1
  17.     ///
  18.     /// Внимание! При изменении имени этого класса необходимо также изменить
  19.     ///          свойство имени файла ресурсов ("Resource File Name") для средства компиляции управляемого ресурса,
  20.     ///          связанного со всеми файлами с расширением .resx, от которых зависит данный класс. В противном случае,
  21.     ///          конструкторы не смогут правильно работать с локализованными
  22.     ///          ресурсами, сопоставленными данной форме.
  23.     /// </summary>
  24.     public ref class Form1 : public System::Windows::Forms::Form
  25.     {
  26.     public:
  27.         Form1(void)
  28.         {
  29.             InitializeComponent();
  30.             //
  31.             //TODO: добавьте код конструктора
  32.             //
  33.         }
  34.  
  35.     protected:
  36.         /// <summary>
  37.         /// Освободить все используемые ресурсы.
  38.         /// </summary>
  39.         ~Form1()
  40.         {
  41.             if (components)
  42.             {
  43.                 delete components;
  44.             }
  45.         }
  46.     private: System::Windows::Forms::TextBox^  textBox1;
  47.     private: System::Windows::Forms::Button^  overview;
  48.     private: System::Windows::Forms::TextBox^  hash;
  49.     private: System::Windows::Forms::Label^  md5_lbl;
  50.     private: System::Windows::Forms::Button^  encrypt;
  51.     private: System::Windows::Forms::ProgressBar^  progressBar1;
  52.     private: System::Windows::Forms::Button^  decrypt;
  53.     private: System::Windows::Forms::Label^  label1;
  54.     private: System::Windows::Forms::TextBox^  password;
  55.     protected:
  56.  
  57.  
  58.     private:
  59.         /// <summary>
  60.         /// Требуется переменная конструктора.
  61.         /// </summary>
  62.         System::ComponentModel::Container ^components;
  63.  
  64. #pragma region Windows Form Designer generated code
  65.         /// <summary>
  66.         /// Обязательный метод для поддержки конструктора - не изменяйте
  67.         /// содержимое данного метода при помощи редактора кода.
  68.         /// </summary>
  69.         void InitializeComponent(void)
  70.         {
  71.             this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  72.             this->overview = (gcnew System::Windows::Forms::Button());
  73.             this->hash = (gcnew System::Windows::Forms::TextBox());
  74.             this->md5_lbl = (gcnew System::Windows::Forms::Label());
  75.             this->encrypt = (gcnew System::Windows::Forms::Button());
  76.             this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
  77.             this->decrypt = (gcnew System::Windows::Forms::Button());
  78.             this->label1 = (gcnew System::Windows::Forms::Label());
  79.             this->password = (gcnew System::Windows::Forms::TextBox());
  80.             this->SuspendLayout();
  81.             //
  82.             // textBox1
  83.             //
  84.             this->textBox1->Location = System::Drawing::Point(12, 12);
  85.             this->textBox1->Name = L"textBox1";
  86.             this->textBox1->Size = System::Drawing::Size(216, 20);
  87.             this->textBox1->TabIndex = 0;
  88.             //
  89.             // overview
  90.             //
  91.             this->overview->Location = System::Drawing::Point(235, 10);
  92.             this->overview->Name = L"overview";
  93.             this->overview->Size = System::Drawing::Size(75, 23);
  94.             this->overview->TabIndex = 1;
  95.             this->overview->Text = L"overview";
  96.             this->overview->UseVisualStyleBackColor = true;
  97.             this->overview->Click += gcnew System::EventHandler(this, &Form1::overview_Click);
  98.             //
  99.             // hash
  100.             //
  101.             this->hash->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  102.                 static_cast<System::Byte>(204)));
  103.             this->hash->Location = System::Drawing::Point(13, 64);
  104.             this->hash->Name = L"hash";
  105.             this->hash->Size = System::Drawing::Size(297, 22);
  106.             this->hash->TabIndex = 2;
  107.             this->hash->TextChanged += gcnew System::EventHandler(this, &Form1::hash_TextChanged);
  108.             //
  109.             // md5_lbl
  110.             //
  111.             this->md5_lbl->AutoSize = true;
  112.             this->md5_lbl->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9.75F, static_cast<System::Drawing::FontStyle>((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)),
  113.                 System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
  114.             this->md5_lbl->Location = System::Drawing::Point(13, 39);
  115.             this->md5_lbl->Name = L"md5_lbl";
  116.             this->md5_lbl->Size = System::Drawing::Size(43, 19);
  117.             this->md5_lbl->TabIndex = 3;
  118.             this->md5_lbl->Text = L"md5:";
  119.             //
  120.             // encrypt
  121.             //
  122.             this->encrypt->Location = System::Drawing::Point(13, 144);
  123.             this->encrypt->Name = L"encrypt";
  124.             this->encrypt->Size = System::Drawing::Size(75, 23);
  125.             this->encrypt->TabIndex = 4;
  126.             this->encrypt->Text = L"Encrypt";
  127.             this->encrypt->UseVisualStyleBackColor = true;
  128.             this->encrypt->Click += gcnew System::EventHandler(this, &Form1::encrypt_Click);
  129.             //
  130.             // progressBar1
  131.             //
  132.             this->progressBar1->Location = System::Drawing::Point(104, 144);
  133.             this->progressBar1->Name = L"progressBar1";
  134.             this->progressBar1->Size = System::Drawing::Size(206, 23);
  135.             this->progressBar1->Style = System::Windows::Forms::ProgressBarStyle::Continuous;
  136.             this->progressBar1->TabIndex = 5;
  137.             //
  138.             // decrypt
  139.             //
  140.             this->decrypt->Location = System::Drawing::Point(13, 184);
  141.             this->decrypt->Name = L"decrypt";
  142.             this->decrypt->Size = System::Drawing::Size(75, 23);
  143.             this->decrypt->TabIndex = 6;
  144.             this->decrypt->Text = L"Decrypt";
  145.             this->decrypt->UseVisualStyleBackColor = true;
  146.             this->decrypt->Click += gcnew System::EventHandler(this, &Form1::decrypt_Click);
  147.             //
  148.             // label1
  149.             //
  150.             this->label1->AutoSize = true;
  151.             this->label1->Location = System::Drawing::Point(21, 115);
  152.             this->label1->Name = L"label1";
  153.             this->label1->Size = System::Drawing::Size(53, 13);
  154.             this->label1->TabIndex = 7;
  155.             this->label1->Text = L"Password";
  156.             //
  157.             // password
  158.             //
  159.             this->password->Location = System::Drawing::Point(104, 115);
  160.             this->password->Name = L"password";
  161.             this->password->Size = System::Drawing::Size(206, 20);
  162.             this->password->TabIndex = 8;
  163.             //
  164.             // Form1
  165.             //
  166.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  167.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  168.             this->ClientSize = System::Drawing::Size(325, 261);
  169.             this->Controls->Add(this->password);
  170.             this->Controls->Add(this->label1);
  171.             this->Controls->Add(this->decrypt);
  172.             this->Controls->Add(this->progressBar1);
  173.             this->Controls->Add(this->encrypt);
  174.             this->Controls->Add(this->md5_lbl);
  175.             this->Controls->Add(this->hash);
  176.             this->Controls->Add(this->overview);
  177.             this->Controls->Add(this->textBox1);
  178.             this->Name = L"Form1";
  179.             this->Text = L"Form1";
  180.             this->ResumeLayout(false);
  181.             this->PerformLayout();
  182.  
  183.         }
  184. #pragma endregion
  185.     private: System::Void overview_Click(System::Object^  sender, System::EventArgs^  e) {
  186.                  OpenFileDialog^ fileDlg = gcnew OpenFileDialog;
  187.                  fileDlg->InitialDirectory = Environment::CurrentDirectory;
  188.                  if (fileDlg->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
  189.                      this->textBox1->Text = fileDlg->FileName;
  190.                  }
  191.                  HashAlgorithm^ md5 = gcnew MD5CryptoServiceProvider;
  192.                  FileStream^ fptr = gcnew FileStream(this->textBox1->Text, FileMode::Open);
  193.                  array<Byte>^ result;
  194.                  result = md5->ComputeHash(fptr);
  195.                  fptr->Close();
  196.                  this->hash->Text = "";
  197.                  for (Int32 i = 0; i < result->Length; i++) {
  198.                      this->hash->Text += Convert::ToString(result[i]) + " ";
  199.                  }
  200.              }
  201.     private: System::Void hash_TextChanged(System::Object^  sender, System::EventArgs^  e) {
  202.              }
  203.     private: System::Void encrypt_Click(System::Object^  sender, System::EventArgs^  e) {
  204.                  OpenFileDialog^ fileDlg = gcnew OpenFileDialog;
  205.                  OpenFileDialog^ fileDlg_write = gcnew OpenFileDialog;
  206.                  fileDlg->InitialDirectory = Environment::CurrentDirectory;
  207.                  if (fileDlg->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
  208.                      FileStream^ fptr = gcnew FileStream(fileDlg->FileName, FileMode::Open);
  209.                      FileStream^ fptr_write = gcnew FileStream(fileDlg->FileName + ".enc", FileMode::Create);
  210.                      try {
  211.                          array<Byte>^ pwd = gcnew array<Byte>(8) {43,88,12,43,65,78,54,121}; // Key
  212.                          array<Byte>^ initVector = gcnew array<Byte>(8) {76,34,54,76,12,43,65,76}; // IV
  213.                          SymmetricAlgorithm^ Des = gcnew DESCryptoServiceProvider;
  214.  
  215.                          for (Int32 j = 0; j < Des->Key->Length; j++) {
  216.                              pwd[j] = Convert::ToByte(Convert::ToInt32(Covert::ToChar(this->password->Text[j % this->password->TextLength])) % 256);
  217.                          }
  218.                          Des->Key = pwd;
  219.                          Des->IV = initVector;
  220.                          CryptoStream^ enc_stream = gcnew CryptoStream (
  221.                              fptr_write,
  222.                              Des->CreateEncryptor(Des->Key, Des->IV),
  223.                              CryptoStreamMode::Write
  224.                              );
  225.  
  226.                          Int32 readlen = 0;
  227.                          Int32 totallen = fptr->Length;
  228.                          this->progressBar1->Value = 0;
  229.                          this->progressBar1->Maximum = fptr->Length;
  230.  
  231.                          Int32 len = 0;
  232.                          array<Byte>^ bin = gcnew array<Byte>(1024);
  233.  
  234.                          while (readlen < totallen) {
  235.                              len = fptr->Read(bin, 0, 1024);
  236.                              enc_stream->Write(bin, 0, len);
  237.                              readlen += len;
  238.                              this->progressBar1->Value = readlen;
  239.                              Application::DoEvents();
  240.                          }
  241.                              enc_stream->Close();
  242.                      }
  243.                      finally {
  244.                          fptr->Close();
  245.                          fptr_write->Close();
  246.                      }
  247.  
  248.                  }
  249.  
  250.              }
  251. private: System::Void decrypt_Click(System::Object^  sender, System::EventArgs^  e) {
  252.                  OpenFileDialog^ fileDlg = gcnew OpenFileDialog;
  253.                  OpenFileDialog^ fileDlg_write = gcnew OpenFileDialog;
  254.                  fileDlg->InitialDirectory = Environment::CurrentDirectory;
  255.                  if (fileDlg->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
  256.                      FileStream^ fptr = gcnew FileStream(fileDlg->FileName, FileMode::Open);
  257.                      FileStream^ fptr_write = gcnew FileStream(fileDlg->FileName + ".jpeg", FileMode::Create);
  258.                      try {
  259.                          array<Byte>^ pwd = gcnew array<Byte>(8) {43,88,12,43,65,78,54,121}; // Key
  260.                          array<Byte>^ initVector = gcnew array<Byte>(8) {76,34,54,76,12,43,65,76}; // IV
  261.                          SymmetricAlgorithm^ Des = gcnew DESCryptoServiceProvider;
  262.  
  263.                          for (Int32 j = 0; j < Des->Key->Length; j++) {
  264.                              pwd[j] = Convert::ToByte(Convert::ToInt32(Covert::ToChar(this->password->Text[j % this->password->TextLength])) % 256);
  265.                          }
  266.  
  267.                          Des->Key = pwd;
  268.                          Des->IV = initVector;
  269.                          CryptoStream^ enc_stream = gcnew CryptoStream (
  270.                              fptr_write,
  271.                              Des->CreateDecryptor(Des->Key, Des->IV),
  272.                              CryptoStreamMode::Write
  273.                              );
  274.  
  275.                          Int32 readlen = 0;
  276.                          Int32 totallen = fptr->Length;
  277.                          this->progressBar1->Value = 0;
  278.                          this->progressBar1->Maximum = fptr->Length;
  279.  
  280.                          Int32 len = 0;
  281.                          array<Byte>^ bin = gcnew array<Byte>(1024);
  282.  
  283.                          while (readlen < totallen) {
  284.                              len = fptr->Read(bin, 0, 1024);
  285.                              enc_stream->Write(bin, 0, len);
  286.                              readlen += len;
  287.                              this->progressBar1->Value = readlen;
  288.                              Application::DoEvents();
  289.                          }
  290.                              enc_stream->Close();
  291.                      }
  292.                      finally {
  293.                          fptr->Close();
  294.                          fptr_write->Close();
  295.                      }
  296.  
  297.                  }
  298.          }
  299. };
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement