Introduction
Encryption and Decryption
The System.Security.Cryptographic
namespace within the
Microsoft .NET Framework provides a variety of tools to aid in encryption and
decryption. The CryptoStream
class is used here to demonstrate the
encryption and decryption with
System.Security.Cryptographic.SymmetricAlgorithm
,
such as DESCryptoServiceProvider
, RC2CryptoServiceProvider
, and
RijndaelManaged
classes.
I have searched the Internet for some samples and all I found
were based on the Microsoft sample code in KB Article
Q307010
which basically uses input/output files as source and destination. I
would like to have the encryption and decryption done in memory without having
to specify source and destination files, so that I could use the code on a web
server or so.
If you have any questions, please email to:
fangfrank@hotmail.com
Frank Fang
Source Code
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace FangHome_Crypto
{
public class SymmCrypto
{
public enum SymmProvEnum : int
{
DES, RC2, Rijndael
}
private SymmetricAlgorithm mobjCryptoService;
public SymmCrypto(SymmProvEnum NetSelected)
{
switch (NetSelected)
{
case SymmProvEnum.DES:
mobjCryptoService = new DESCryptoServiceProvider();
break;
case SymmProvEnum.RC2:
mobjCryptoService = new RC2CryptoServiceProvider();
break;
case SymmProvEnum.Rijndael:
mobjCryptoService = new RijndaelManaged();
break;
}
}
public SymmCrypto(SymmetricAlgorithm ServiceProvider)
{
mobjCryptoService = ServiceProvider;
}
private byte[] GetLegalKey(string Key)
{
string sTemp;
if (mobjCryptoService.LegalKeySizes.Length > 0)
{
int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
while (Key.Length * 8 > moreSize)
{
lessSize = moreSize;
moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
}
sTemp = Key.PadRight(moreSize / 8, ' ');
}
else
sTemp = Key;
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
public string Encrypting(string Source, string Key)
{
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] bytKey = GetLegalKey(Key);
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.GetBuffer();
int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut[i] == 0)
break;
return System.Convert.ToBase64String(bytOut, 0, i);
}
public string Decrypting(string Source, string Key)
{
byte[] bytIn = System.Convert.FromBase64String(Source);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
byte[] bytKey = GetLegalKey(Key);
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
System.IO.StreamReader sr = new System.IO.StreamReader( cs );
return sr.ReadToEnd();
}
}
}