Guest User

Untitled

a guest
Feb 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.18 KB | None | 0 0
  1. //.h file code:
  2.  
  3. #pragma once#pragma region IAsyncResult Members
  4.  
  5.             public:
  6.                 property Object ^AsyncState
  7.                 {
  8.                     Object ^get();
  9.                 }
  10.  
  11.                 property bool CompletedSynchronously
  12.                 {
  13.                     bool get();
  14.                 }
  15.  
  16.                 property System::Threading::WaitHandle ^AsyncWaitHandle
  17.                 {
  18.                     System::Threading::WaitHandle ^get();
  19.                 }
  20.  
  21.                 property bool IsCompleted
  22.                 {
  23.                     bool get();
  24.                 }
  25.  
  26.     #pragma endregion
  27.             };
  28.  
  29.         public:
  30.             enum class Terget: int
  31.             {
  32.                 Encrypted,
  33.                 Normal
  34.             };
  35.         private:
  36.             Stream ^stream;
  37.             Blowfish ^bf;
  38.             BlowfishStream(Stream ^stream, Blowfish ^bf);
  39.         public:
  40.             property bool CanRead
  41.             {
  42.                 virtual bool get() override;
  43.             }
  44.             property bool CanSeek
  45.             {
  46.                 virtual bool get() override;
  47.             }
  48.             property bool CanWrite
  49.             {
  50.                 virtual bool get() override;
  51.             }
  52.             property Int64 Length
  53.             {
  54.                 virtual Int64 get() override;
  55.             }
  56.             property Int64 Position
  57.             {
  58.                 virtual Int64 get() override;
  59.                 virtual void set(Int64 value) override;
  60.             }
  61.             virtual void Flush() override;
  62.             virtual int Read(array<Byte> ^buffer, int offset, int count) override;
  63.             virtual void Write(array<Byte> ^buffer, int offset, int count) override;
  64.             virtual IAsyncResult ^BeginRead(array<Byte> ^buffer, int offset, int count, AsyncCallback ^callback, Object ^state) override;
  65.             virtual int EndRead(IAsyncResult ^asyncResult) override;
  66.         private:
  67.             void ReadComplete(IAsyncResult ^result);
  68.         public:
  69.             virtual IAsyncResult ^BeginWrite(array<Byte> ^buffer, int offset, int count, AsyncCallback ^callback, Object ^state) override;
  70.             virtual Int64 Seek(Int64 offset, SeekOrigin origin) override;
  71.             virtual void SetLength(Int64 value) override;
  72.         };
  73.     }
  74. }
  75.  
  76.  
  77.        //.cpp file code:
  78.  
  79. #include "blow.h"
  80.  
  81. using namespace System;
  82. using namespace System::Text;
  83. using namespace System::IO;
  84. namespace Simias
  85. {
  86.     namespace Encryption
  87.     {
  88.  
  89.         Blowfish::Blowfish(array<Byte> ^key)
  90.         {
  91.             short i = 0;
  92.             short j = 0;
  93.             short k = 0;
  94.             UInt32 data = 0;
  95.             UInt32 datal = 0;
  96.             UInt32 datar = 0;
  97.  
  98.             P = dynamic_cast<array<int>^>(_P->Clone());
  99.             S = dynamic_cast<array<int, 2>^>(_S->Clone());
  100.  
  101.             j = 0;
  102.             for (i = 0; i <= N + 1; i++)
  103.             {
  104.                 data = 0X0;
  105.                 for (k = 0; k <= 3; k++)
  106.                 {
  107.                     data = (data << 8) | key[j];
  108.                     j += 1;
  109.                     if (j >= key->Length)
  110.                     {
  111.                         j = 0;
  112.                     }
  113.                 }
  114.                 P[i] = P[i] ^ data;
  115.             }
  116.  
  117.             datal = 0X0;
  118.             datar = 0X0;
  119.  
  120.             for (i = 0; i <= N + 1; i += 2)
  121.             {
  122.                 Encipher(datal, datar);
  123.                 P[i] = datal;
  124.                 P[i + 1] = datar;
  125.             }
  126.  
  127.             for (i = 0; i <= 3; i++)
  128.             {
  129.                 for (j = 0; j <= 255; j += 2)
  130.                 {
  131.                     Encipher(datal, datar);
  132.  
  133.                     S[i, j] = datal;
  134.                     S[i, j + 1] = datar;
  135.                 }
  136.             }
  137.         }
  138.  
  139.         UInt32 Blowfish::F(UInt32 x)
  140.         {
  141.             UInt16 a = 0;
  142.             UInt16 b = 0;
  143.             UInt16 c = 0;
  144.             UInt16 d = 0;
  145.             UInt32 y = 0;
  146.  
  147.             d = Convert::ToUInt16((x & 0XFF));
  148.             x >>= 8;
  149.             c = Convert::ToUInt16((x & 0XFF));
  150.             x >>= 8;
  151.             b = Convert::ToUInt16((x & 0XFF));
  152.             x >>= 8;
  153.             a = Convert::ToUInt16((x & 0XFF));
  154.             y = S[0, a] + S[1, b];
  155.             y = y ^ S[2, c];
  156.             y = y + S[3, d];
  157.  
  158.             return y;
  159.         }
  160.  
  161.         void Blowfish::Encipher(array<Byte> ^data, int length)
  162.         {
  163.             UInt32 xl = 0;
  164.             UInt32 xr = 0;
  165.             if ((length % 8) != 0)
  166.             {
  167.                 throw gcnew Exception("Invalid Length");
  168.             }
  169.             for (int i = 0; i < length; i += 8)
  170.             {
  171.                 xl = Convert::ToUInt32(((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]));
  172.                 xr = Convert::ToUInt32(((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]));
  173.                 Encipher(xl, xr);
  174.                 data[i] = Convert::ToByte((xl >> 24));
  175.                 data[i + 1] = Convert::ToByte((xl >> 16));
  176.                 data[i + 2] = Convert::ToByte((xl >> 8));
  177.                 data[i + 3] = Convert::ToByte((xl));
  178.                 data[i + 4] = Convert::ToByte((xr >> 24));
  179.                 data[i + 5] = Convert::ToByte((xr >> 16));
  180.                 data[i + 6] = Convert::ToByte((xr >> 8));
  181.                 data[i + 7] = Convert::ToByte((xr));
  182.             }
  183.         }
  184.  
  185.         void Blowfish::Encipher(UInt32 %xl__1, UInt32 %xr__2)
  186.         {
  187.             UInt32 Xl__3 = 0;
  188.             UInt32 Xr__4 = 0;
  189.             UInt32 temp = 0;
  190.             short i = 0;
  191.  
  192.             Xl__3 = xl__1;
  193.             Xr__4 = xr__2;
  194.  
  195.             for (i = 0; i < N; i++)
  196.             {
  197.                 Xl__3 = Xl__3 ^ P[i];
  198.                 Xr__4 = F(Xl__3) ^ Xr__4;
  199.  
  200.                 temp = Xl__3;
  201.                 Xl__3 = Xr__4;
  202.                 Xr__4 = temp;
  203.             }
  204.  
  205.             temp = Xl__3;
  206.             Xl__3 = Xr__4;
  207.             Xr__4 = temp;
  208.  
  209.             Xr__4 = Xr__4 ^ P[N];
  210.             Xl__3 = Xl__3 ^ P[N + 1];
  211.  
  212.             xl__1 = Xl__3;
  213.             xr__2 = Xr__4;
  214.         }
  215.  
  216.         void Blowfish::Decipher(array<Byte> ^data, int length)
  217.         {
  218.             UInt32 xl = 0;
  219.             UInt32 xr = 0;
  220.             if ((length % 8) != 0)
  221.             {
  222.                 throw gcnew Exception("Invalid Length");
  223.             }
  224.             for (int i = 0; i < length; i += 8)
  225.             {
  226.                 xl = Convert::ToUInt32(((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]));
  227.                 xr = Convert::ToUInt32(((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]));
  228.                 Decipher(xl, xr);
  229.                 data[i] = Convert::ToByte((xl >> 24));
  230.                 data[i + 1] = Convert::ToByte((xl >> 16));
  231.                 data[i + 2] = Convert::ToByte((xl >> 8));
  232.                 data[i + 3] = Convert::ToByte((xl));
  233.                 data[i + 4] = Convert::ToByte((xr >> 24));
  234.                 data[i + 5] = Convert::ToByte((xr >> 16));
  235.                 data[i + 6] = Convert::ToByte((xr >> 8));
  236.                 data[i + 7] = Convert::ToByte((xr));
  237.             }
  238.         }
  239.  
  240.         void Blowfish::Decipher(UInt32 %xl__1, UInt32 %xr__2)
  241.         {
  242.             UInt32 Xl__3 = 0;
  243.             UInt32 Xr__4 = 0;
  244.             UInt32 temp = 0;
  245.             short i = 0;
  246.  
  247.             Xl__3 = xl__1;
  248.             Xr__4 = xr__2;
  249.  
  250.             for (i = N + 1; i >= 2; i--)
  251.             {
  252.                 Xl__3 = Xl__3 ^ P[i];
  253.                 Xr__4 = F(Xl__3) ^ Xr__4;
  254.  
  255.  
  256.                 temp = Xl__3;
  257.                 Xl__3 = Xr__4;
  258.                 Xr__4 = temp;
  259.             }
  260.  
  261.  
  262.             temp = Xl__3;
  263.             Xl__3 = Xr__4;
  264.             Xr__4 = temp;
  265.  
  266.             Xr__4 = Xr__4 ^ P[1];
  267.             Xl__3 = Xl__3 ^ P[0];
  268.  
  269.             xl__1 = Xl__3;
  270.             xr__2 = Xr__4;
  271.         }
  272.  
  273.         BlowfishStream::CBState::CBState(AsyncCallback ^callback, Object ^state, array<Byte> ^buffer)
  274.         {
  275.             this->callback = callback;
  276.             this->state = state;
  277.             this->buffer = buffer;
  278.         }
  279.  
  280.         Object ^BlowfishStream::CBState::AsyncState::get()
  281.         {
  282.             return state;
  283.         }
  284.  
  285.         bool BlowfishStream::CBState::CompletedSynchronously::get()
  286.         {
  287.             return result->CompletedSynchronously;
  288.         }
  289.  
  290.         System::Threading::WaitHandle ^BlowfishStream::CBState::AsyncWaitHandle::get()
  291.         {
  292.             return result->AsyncWaitHandle;
  293.         }
  294.  
  295.         bool BlowfishStream::CBState::IsCompleted::get()
  296.         {
  297.             return result->IsCompleted;
  298.         }
  299.  
  300.         BlowfishStream::BlowfishStream(Stream ^stream, Blowfish ^bf)
  301.         {
  302.             this->stream = stream;
  303.             this->bf = bf;
  304.         }
  305.  
  306.         bool BlowfishStream::CanRead::get()
  307.         {
  308.             return stream->CanRead;
  309.         }
  310.  
  311.         bool BlowfishStream::CanSeek::get()
  312.         {
  313.             return stream->CanSeek;
  314.         }
  315.  
  316.         bool BlowfishStream::CanWrite::get()
  317.         {
  318.             return stream->CanWrite;
  319.         }
  320.  
  321.         Int64 BlowfishStream::Length::get()
  322.         {
  323.             return stream->Length;
  324.         }
  325.  
  326.         Int64 BlowfishStream::Position::get()
  327.         {
  328.             return stream->Position;
  329.         }
  330.  
  331.         void BlowfishStream::Position::set(Int64 value)
  332.         {
  333.             stream->Position = value;
  334.         }
  335.  
  336.         void BlowfishStream::Flush()
  337.         {
  338.             stream->Flush();
  339.         }
  340.  
  341.         int BlowfishStream::Read(array<Byte> ^buffer, int offset, int count)
  342.         {
  343.             int bytesRead = stream->Read(buffer, offset, count);
  344.             String ^Target = "";
  345.             if (Target == Terget::Normal)
  346.             {
  347.                 bf->Encipher(buffer, bytesRead);
  348.             }
  349.             else
  350.             {
  351.                 bf->Decipher(buffer, bytesRead);
  352.             }
  353.             return bytesRead;
  354.         }
  355.  
  356.         void BlowfishStream::Write(array<Byte> ^buffer, int offset, int count)
  357.         {
  358.             String ^Target = "";
  359.             if (Target == Terget::Normal)
  360.             {
  361.                 bf->Decipher(buffer, count);
  362.             }
  363.             else
  364.             {
  365.                 bf->Encipher(buffer, count);
  366.             }
  367.             stream->Write(buffer, offset, count);
  368.         }
  369.  
  370.         IAsyncResult ^BlowfishStream::BeginRead(array<Byte> ^buffer, int offset, int count, AsyncCallback ^callback, Object ^state)
  371.         {
  372.             CBState ^cbs = gcnew CBState(callback, state, buffer);
  373.             cbs->result = Stream::BeginRead(buffer, offset, count, gcnew AsyncCallback(ReadComplete), cbs);
  374.             return cbs;
  375.         }
  376.  
  377.         int BlowfishStream::EndRead(IAsyncResult ^asyncResult)
  378.         {
  379.             CBState ^cbs = safe_cast<CBState^>(asyncResult->AsyncState);
  380.             int bytesRead = Stream::EndRead(cbs->result);
  381.             String ^Target = "";
  382.             if (Target == Terget::Normal)
  383.             {
  384.                 bf->Encipher(cbs->buffer, bytesRead);
  385.             }
  386.             else
  387.             {
  388.                 bf->Decipher(cbs->buffer, bytesRead);
  389.             }
  390.             return bytesRead;
  391.         }
  392.  
  393.         void BlowfishStream::ReadComplete(IAsyncResult ^result)
  394.         {
  395.             CBState ^cbs = safe_cast<CBState^>(result->AsyncState);
  396.             cbs->callback(cbs);
  397.         }
  398.  
  399.         IAsyncResult ^BlowfishStream::BeginWrite(array<Byte> ^buffer, int offset, int count, AsyncCallback ^callback, Object ^state)
  400.         {
  401.             String ^Target = "";
  402.             if (Target == Terget::Normal)
  403.             {
  404.                 bf->Decipher(buffer, count);
  405.             }
  406.             else
  407.             {
  408.                 bf->Encipher(buffer, count);
  409.             }
  410.             return Stream::BeginWrite(buffer, offset, count, callback, state);
  411.         }
  412.  
  413.         Int64 BlowfishStream::Seek(Int64 offset, SeekOrigin origin)
  414.         {
  415.             return stream->Seek(offset, origin);
  416.         }
  417.  
  418.         void BlowfishStream::SetLength(Int64 value)
  419.         {
  420.             stream->SetLength(value);
  421.         }
  422.     }
  423. }
Add Comment
Please, Sign In to add comment