1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Collections::Generic;
using namespace System::Text;
namespace myapplication {
/// <summary>
/// Summary for Class1
/// </summary>
public ref class Class1 : public System::ComponentModel::Component
{
public:
Class1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
Class1(System::ComponentModel::IContainer ^container)
{
/// <summary>
/// Required for Windows.Forms Class Composition Designer support
/// </summary>
container->Add(this);
InitializeComponent();
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Class1()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
components = gcnew System::ComponentModel::Container();
}
#pragma endregion
public: void PolyAES()
{
this->algo = gcnew System::Security::Cryptography::RijndaelManaged();
this->algo->Mode = System::Security::Cryptography::CipherMode::CBC;
this->rngAlgo = gcnew System::Security::Cryptography::RNGCryptoServiceProvider();
}
private:
literal int saltSize = 32;
System::Security::Cryptography::SymmetricAlgorithm ^algo;
System::Security::Cryptography::RNGCryptoServiceProvider ^rngAlgo;
array<Byte> ^salt;
private:
void InitializeSecureParameters(array<Byte> ^key)
{
// init rijndael IV
this->algo->GenerateIV();
salt = gcnew array<Byte>(saltSize);
rngAlgo->GetBytes(salt);
System::Security::Cryptography::Rfc2898DeriveBytes ^pwDeriveAlg = gcnew System::Security::Cryptography::Rfc2898DeriveBytes(key, salt, 2000);
this->algo->Key = pwDeriveAlg->GetBytes(32);
}
void LoadSecureParameters(array<Byte> ^key, array<Byte> ^encIv, array<Byte> ^encSalt)
{
this->algo->IV = encIv;
this->salt = encSalt;
System::Security::Cryptography::Rfc2898DeriveBytes ^pwDeriveAlg = gcnew System::Security::Cryptography::Rfc2898DeriveBytes(key, salt, 2000);
this->algo->Key = pwDeriveAlg->GetBytes(32);
}
public:
String ^Encrypt(String ^plainText, String ^key)
{
return Convert::ToBase64String(this->Encrypt(UnicodeEncoding::UTF8->GetBytes(plainText), UnicodeEncoding::UTF8->GetBytes(key)));
}
String ^Decrypt(String ^cipherText, String ^key)
{
return UnicodeEncoding::UTF8->GetString(this->Decrypt(Convert::FromBase64String(cipherText), UnicodeEncoding::UTF8->GetBytes(key)));
}
array<Byte> ^Encrypt(array<Byte> ^plainText, array<Byte> ^key)
{
InitializeSecureParameters(key);
System::Security::Cryptography::ICryptoTransform ^encTransform = algo->CreateEncryptor();
return ConcatDataToCipherText(ConcatDataToCipherText(encTransform->TransformFinalBlock(plainText, 0, plainText->Length), salt), algo->IV);
}
array<Byte> ^Decrypt(array<Byte> ^cipherText, array<Byte> ^key)
{
array<Byte> ^cipherTextWithSalt = gcnew array<Byte>(1);
array<Byte> ^encSalt = gcnew array<Byte>(1);
array<Byte> ^origCipherText = gcnew array<Byte>(1);
array<Byte> ^encIv = gcnew array<Byte>(1);
SliceCipherTextIntoParts(cipherText, 16, cipherTextWithSalt, encIv);
SliceCipherTextIntoParts(cipherTextWithSalt, saltSize, origCipherText, encSalt);
LoadSecureParameters(key, encIv, encSalt);
System::Security::Cryptography::ICryptoTransform ^decTransform = algo->CreateDecryptor();
array<Byte> ^plainText = decTransform->TransformFinalBlock(origCipherText, 0, origCipherText->Length);
return plainText;
}
private:
array<Byte> ^ConcatDataToCipherText(array<Byte> ^cipherText, array<Byte> ^iv)
{
int origLength = cipherText->Length;
Array::Resize(cipherText, cipherText->Length + iv->Length);
Buffer::BlockCopy(iv, 0, cipherText, origLength, iv->Length);
return cipherText;
}
void SliceCipherTextIntoParts(array<Byte> ^cipherText, int secondPartLen, array<Byte> ^%origCipherText, array<Byte> ^%iv)
{
Array::Resize(iv, secondPartLen);
Buffer::BlockCopy(cipherText, Convert::ToInt32(cipherText->Length - secondPartLen), iv, 0, secondPartLen);
Array::Resize(origCipherText, Convert::ToInt32(cipherText->Length - secondPartLen));
Buffer::BlockCopy(cipherText, 0, origCipherText, 0, Convert::ToInt32(cipherText->Length - secondPartLen));
}
};
}
|