|
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Simeon.Lobo.Security
{
public class SymmetricEncryption
{
public SymmetricEncryption()
{
this.Algorithm = SymmetricAlgorithm.Create();
for (int num1 = 0; num1 < this.Algorithm.LegalKeySizes.Length; num1++)
{
this.KeySize = this.Algorithm.LegalKeySizes[num1].MinSize;
if (this.Algorithm.ValidKeySize(this.KeySize))
{
break;
}
}
this.Algorithm.Mode = CipherMode.ECB;
}
public SymmetricEncryption(SymmetricAlgorithm Algorithm)
{
this.Algorithm = Algorithm;
for (int num1 = 0; num1 < this.Algorithm.LegalKeySizes.Length; num1++)
{
this.KeySize = this.Algorithm.LegalKeySizes[num1].MinSize;
if (this.Algorithm.ValidKeySize(this.KeySize))
{
return;
}
}
}
public SymmetricEncryption(SymmetricAlgorithm Algorithm, int KeySize)
{
this.Algorithm = Algorithm;
if (!this.Algorithm.ValidKeySize(KeySize))
{
for (int num1 = 0; num1 < this.Algorithm.LegalKeySizes.Length; num1++)
{
this.KeySize = this.Algorithm.LegalKeySizes[num1].MinSize;
if (this.Algorithm.ValidKeySize(this.KeySize))
{
return;
}
}
}
}
private string ByteArrayToString(byte[] source)
{
string text1 = string.Empty;
StringBuilder builder1 = new StringBuilder(source.Length);
foreach (byte num1 in source)
{
builder1.Append((char)num1);
}
return builder1.ToString();
}
public string Decode(string Input)
{
return this.ByteArrayToString(Convert.FromBase64String(Input));
}
private string Decrypt(string plaintext, byte[] key, byte[] initializationVector)
{
string text1 = string.Empty;
try
{
MemoryStream stream1 = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream1, this.Algorithm.CreateDecryptor(key, initializationVector), CryptoStreamMode.Write);
stream2.Write(this.StringToByteArray(plaintext), 0, plaintext.Length);
stream2.FlushFinalBlock();
stream2.Flush();
stream2.Close();
text1 = this.MemoryStreamToString(stream1);
stream1.Close();
return text1;
}
catch (Exception)
{
return “”;
}
}
public string DecryptString(string Input, string Key)
{
return this.DecryptString(Input, Key, Key);
}
public string DecryptString(string Input, string Key, string IV)
{
try
{
if (Key.Length != 0)
{
goto Label_0019;
}
return “”;
Label_0010:
Key = Key + Key;
Label_0019:
if (Key.Length >= this.KeySize)
{
Key = Key.Substring(0, this.KeySize);
if (IV.Length == 0)
{
IV = Key;
}
while (IV.Length < 0×10)
{
IV = IV + IV;
}
IV = IV.Substring(0, 0×10);
byte[] buffer1 = this.StringToByteArray(Key);
byte[] buffer2 = this.StringToByteArray(IV);
return this.Decrypt(this.Decode(Input), buffer1, buffer2);
}
goto Label_0010;
}
catch (Exception)
{
return “”;
}
}
public string Encode(string Input)
{
return Convert.ToBase64String(this.StringToByteArray(Input));
}
private string Encrypt(string plaintext, byte[] key, byte[] initializationVector)
{
string text1 = string.Empty;
try
{
MemoryStream stream1 = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream1, this.Algorithm.CreateEncryptor(key, initializationVector), CryptoStreamMode.Write);
stream2.Write(this.StringToByteArray(plaintext), 0, plaintext.Length);
stream2.FlushFinalBlock();
stream2.Flush();
stream2.Close();
text1 = this.MemoryStreamToString(stream1);
stream1.Close();
return text1;
}
catch (Exception)
{
return text1;
}
}
public string EncryptString(string Input, string Key)
{
return this.EncryptString(Input, Key, Key);
}
public string EncryptString(string Input, string Key, string IV)
{
try
{
if (Key.Length != 0)
{
goto Label_0019;
}
return “”;
Label_0010:
Key = Key + Key;
Label_0019:
if (Key.Length >= this.KeySize)
{
Key = Key.Substring(0, this.KeySize);
if (IV.Length == 0)
{
IV = Key;
}
while (IV.Length < 0×10)
{
IV = IV + IV;
}
IV = IV.Substring(0, 0×10);
byte[] buffer1 = this.StringToByteArray(Key);
byte[] buffer2 = this.StringToByteArray(IV);
return this.Encode(this.Encrypt(Input, buffer1, buffer2));
}
goto Label_0010;
}
catch (Exception)
{
return “”;
}
}
private string MemoryStreamToString(MemoryStream source)
{
string text1 = string.Empty;
return this.ByteArrayToString(source.ToArray());
}
private byte[] StringToByteArray(string s)
{
byte[] buffer1 = new byte[s.Length];
for (int num1 = 0; num1 < s.Length; num1++)
{
buffer1[num1] = (byte)s[num1];
}
return buffer1;
}
public SymmetricAlgorithm Algorithm
{
get
{
return this.moAlgorithm;
}
set
{
this.moAlgorithm = value;
}
}
public int KeySize
{
get
{
return this.mKeySize;
}
set
{
this.mKeySize = value / 8;
}
}
private int mKeySize;
private SymmetricAlgorithm moAlgorithm;
}
}
|