Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //.h file code:
- #pragma once#pragma region IAsyncResult Members
- public:
- property Object ^AsyncState
- {
- Object ^get();
- }
- property bool CompletedSynchronously
- {
- bool get();
- }
- property System::Threading::WaitHandle ^AsyncWaitHandle
- {
- System::Threading::WaitHandle ^get();
- }
- property bool IsCompleted
- {
- bool get();
- }
- #pragma endregion
- };
- public:
- enum class Terget: int
- {
- Encrypted,
- Normal
- };
- private:
- Stream ^stream;
- Blowfish ^bf;
- BlowfishStream(Stream ^stream, Blowfish ^bf);
- public:
- property bool CanRead
- {
- virtual bool get() override;
- }
- property bool CanSeek
- {
- virtual bool get() override;
- }
- property bool CanWrite
- {
- virtual bool get() override;
- }
- property Int64 Length
- {
- virtual Int64 get() override;
- }
- property Int64 Position
- {
- virtual Int64 get() override;
- virtual void set(Int64 value) override;
- }
- virtual void Flush() override;
- virtual int Read(array<Byte> ^buffer, int offset, int count) override;
- virtual void Write(array<Byte> ^buffer, int offset, int count) override;
- virtual IAsyncResult ^BeginRead(array<Byte> ^buffer, int offset, int count, AsyncCallback ^callback, Object ^state) override;
- virtual int EndRead(IAsyncResult ^asyncResult) override;
- private:
- void ReadComplete(IAsyncResult ^result);
- public:
- virtual IAsyncResult ^BeginWrite(array<Byte> ^buffer, int offset, int count, AsyncCallback ^callback, Object ^state) override;
- virtual Int64 Seek(Int64 offset, SeekOrigin origin) override;
- virtual void SetLength(Int64 value) override;
- };
- }
- }
- //.cpp file code:
- #include "blow.h"
- using namespace System;
- using namespace System::Text;
- using namespace System::IO;
- namespace Simias
- {
- namespace Encryption
- {
- Blowfish::Blowfish(array<Byte> ^key)
- {
- short i = 0;
- short j = 0;
- short k = 0;
- UInt32 data = 0;
- UInt32 datal = 0;
- UInt32 datar = 0;
- P = dynamic_cast<array<int>^>(_P->Clone());
- S = dynamic_cast<array<int, 2>^>(_S->Clone());
- j = 0;
- for (i = 0; i <= N + 1; i++)
- {
- data = 0X0;
- for (k = 0; k <= 3; k++)
- {
- data = (data << 8) | key[j];
- j += 1;
- if (j >= key->Length)
- {
- j = 0;
- }
- }
- P[i] = P[i] ^ data;
- }
- datal = 0X0;
- datar = 0X0;
- for (i = 0; i <= N + 1; i += 2)
- {
- Encipher(datal, datar);
- P[i] = datal;
- P[i + 1] = datar;
- }
- for (i = 0; i <= 3; i++)
- {
- for (j = 0; j <= 255; j += 2)
- {
- Encipher(datal, datar);
- S[i, j] = datal;
- S[i, j + 1] = datar;
- }
- }
- }
- UInt32 Blowfish::F(UInt32 x)
- {
- UInt16 a = 0;
- UInt16 b = 0;
- UInt16 c = 0;
- UInt16 d = 0;
- UInt32 y = 0;
- d = Convert::ToUInt16((x & 0XFF));
- x >>= 8;
- c = Convert::ToUInt16((x & 0XFF));
- x >>= 8;
- b = Convert::ToUInt16((x & 0XFF));
- x >>= 8;
- a = Convert::ToUInt16((x & 0XFF));
- y = S[0, a] + S[1, b];
- y = y ^ S[2, c];
- y = y + S[3, d];
- return y;
- }
- void Blowfish::Encipher(array<Byte> ^data, int length)
- {
- UInt32 xl = 0;
- UInt32 xr = 0;
- if ((length % 8) != 0)
- {
- throw gcnew Exception("Invalid Length");
- }
- for (int i = 0; i < length; i += 8)
- {
- xl = Convert::ToUInt32(((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]));
- xr = Convert::ToUInt32(((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]));
- Encipher(xl, xr);
- data[i] = Convert::ToByte((xl >> 24));
- data[i + 1] = Convert::ToByte((xl >> 16));
- data[i + 2] = Convert::ToByte((xl >> 8));
- data[i + 3] = Convert::ToByte((xl));
- data[i + 4] = Convert::ToByte((xr >> 24));
- data[i + 5] = Convert::ToByte((xr >> 16));
- data[i + 6] = Convert::ToByte((xr >> 8));
- data[i + 7] = Convert::ToByte((xr));
- }
- }
- void Blowfish::Encipher(UInt32 %xl__1, UInt32 %xr__2)
- {
- UInt32 Xl__3 = 0;
- UInt32 Xr__4 = 0;
- UInt32 temp = 0;
- short i = 0;
- Xl__3 = xl__1;
- Xr__4 = xr__2;
- for (i = 0; i < N; i++)
- {
- Xl__3 = Xl__3 ^ P[i];
- Xr__4 = F(Xl__3) ^ Xr__4;
- temp = Xl__3;
- Xl__3 = Xr__4;
- Xr__4 = temp;
- }
- temp = Xl__3;
- Xl__3 = Xr__4;
- Xr__4 = temp;
- Xr__4 = Xr__4 ^ P[N];
- Xl__3 = Xl__3 ^ P[N + 1];
- xl__1 = Xl__3;
- xr__2 = Xr__4;
- }
- void Blowfish::Decipher(array<Byte> ^data, int length)
- {
- UInt32 xl = 0;
- UInt32 xr = 0;
- if ((length % 8) != 0)
- {
- throw gcnew Exception("Invalid Length");
- }
- for (int i = 0; i < length; i += 8)
- {
- xl = Convert::ToUInt32(((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]));
- xr = Convert::ToUInt32(((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]));
- Decipher(xl, xr);
- data[i] = Convert::ToByte((xl >> 24));
- data[i + 1] = Convert::ToByte((xl >> 16));
- data[i + 2] = Convert::ToByte((xl >> 8));
- data[i + 3] = Convert::ToByte((xl));
- data[i + 4] = Convert::ToByte((xr >> 24));
- data[i + 5] = Convert::ToByte((xr >> 16));
- data[i + 6] = Convert::ToByte((xr >> 8));
- data[i + 7] = Convert::ToByte((xr));
- }
- }
- void Blowfish::Decipher(UInt32 %xl__1, UInt32 %xr__2)
- {
- UInt32 Xl__3 = 0;
- UInt32 Xr__4 = 0;
- UInt32 temp = 0;
- short i = 0;
- Xl__3 = xl__1;
- Xr__4 = xr__2;
- for (i = N + 1; i >= 2; i--)
- {
- Xl__3 = Xl__3 ^ P[i];
- Xr__4 = F(Xl__3) ^ Xr__4;
- temp = Xl__3;
- Xl__3 = Xr__4;
- Xr__4 = temp;
- }
- temp = Xl__3;
- Xl__3 = Xr__4;
- Xr__4 = temp;
- Xr__4 = Xr__4 ^ P[1];
- Xl__3 = Xl__3 ^ P[0];
- xl__1 = Xl__3;
- xr__2 = Xr__4;
- }
- BlowfishStream::CBState::CBState(AsyncCallback ^callback, Object ^state, array<Byte> ^buffer)
- {
- this->callback = callback;
- this->state = state;
- this->buffer = buffer;
- }
- Object ^BlowfishStream::CBState::AsyncState::get()
- {
- return state;
- }
- bool BlowfishStream::CBState::CompletedSynchronously::get()
- {
- return result->CompletedSynchronously;
- }
- System::Threading::WaitHandle ^BlowfishStream::CBState::AsyncWaitHandle::get()
- {
- return result->AsyncWaitHandle;
- }
- bool BlowfishStream::CBState::IsCompleted::get()
- {
- return result->IsCompleted;
- }
- BlowfishStream::BlowfishStream(Stream ^stream, Blowfish ^bf)
- {
- this->stream = stream;
- this->bf = bf;
- }
- bool BlowfishStream::CanRead::get()
- {
- return stream->CanRead;
- }
- bool BlowfishStream::CanSeek::get()
- {
- return stream->CanSeek;
- }
- bool BlowfishStream::CanWrite::get()
- {
- return stream->CanWrite;
- }
- Int64 BlowfishStream::Length::get()
- {
- return stream->Length;
- }
- Int64 BlowfishStream::Position::get()
- {
- return stream->Position;
- }
- void BlowfishStream::Position::set(Int64 value)
- {
- stream->Position = value;
- }
- void BlowfishStream::Flush()
- {
- stream->Flush();
- }
- int BlowfishStream::Read(array<Byte> ^buffer, int offset, int count)
- {
- int bytesRead = stream->Read(buffer, offset, count);
- String ^Target = "";
- if (Target == Terget::Normal)
- {
- bf->Encipher(buffer, bytesRead);
- }
- else
- {
- bf->Decipher(buffer, bytesRead);
- }
- return bytesRead;
- }
- void BlowfishStream::Write(array<Byte> ^buffer, int offset, int count)
- {
- String ^Target = "";
- if (Target == Terget::Normal)
- {
- bf->Decipher(buffer, count);
- }
- else
- {
- bf->Encipher(buffer, count);
- }
- stream->Write(buffer, offset, count);
- }
- IAsyncResult ^BlowfishStream::BeginRead(array<Byte> ^buffer, int offset, int count, AsyncCallback ^callback, Object ^state)
- {
- CBState ^cbs = gcnew CBState(callback, state, buffer);
- cbs->result = Stream::BeginRead(buffer, offset, count, gcnew AsyncCallback(ReadComplete), cbs);
- return cbs;
- }
- int BlowfishStream::EndRead(IAsyncResult ^asyncResult)
- {
- CBState ^cbs = safe_cast<CBState^>(asyncResult->AsyncState);
- int bytesRead = Stream::EndRead(cbs->result);
- String ^Target = "";
- if (Target == Terget::Normal)
- {
- bf->Encipher(cbs->buffer, bytesRead);
- }
- else
- {
- bf->Decipher(cbs->buffer, bytesRead);
- }
- return bytesRead;
- }
- void BlowfishStream::ReadComplete(IAsyncResult ^result)
- {
- CBState ^cbs = safe_cast<CBState^>(result->AsyncState);
- cbs->callback(cbs);
- }
- IAsyncResult ^BlowfishStream::BeginWrite(array<Byte> ^buffer, int offset, int count, AsyncCallback ^callback, Object ^state)
- {
- String ^Target = "";
- if (Target == Terget::Normal)
- {
- bf->Decipher(buffer, count);
- }
- else
- {
- bf->Encipher(buffer, count);
- }
- return Stream::BeginWrite(buffer, offset, count, callback, state);
- }
- Int64 BlowfishStream::Seek(Int64 offset, SeekOrigin origin)
- {
- return stream->Seek(offset, origin);
- }
- void BlowfishStream::SetLength(Int64 value)
- {
- stream->SetLength(value);
- }
- }
- }
Add Comment
Please, Sign In to add comment