|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Source Code</title>
</head>
<body>
<p>using System;<br>
using System.Security.Cryptography;<br>
using System.IO;<br>
using System.Text;</p>
<p>namespace FangHome_Crypto<br>
{<br>
/// <summary><br>
/// SymmCrypto is a wrapper of
System.Security.Cryptography.SymmetricAlgorithm classes<br>
/// and simplifies the interface. It supports customized
SymmetricAlgorithm as well.<br>
/// </summary><br>
public class SymmCrypto<br>
{<br>
/// <remarks><br>
/// Supported .Net intrinsic
SymmetricAlgorithm classes.<br>
/// </remarks><br>
public enum SymmProvEnum : int<br>
{<br>
DES, RC2,
Rijndael<br>
}</p>
<p> private SymmetricAlgorithm
mobjCryptoService;</p>
<p> /// <remarks><br>
/// Constructor for using an
intrinsic .Net SymmetricAlgorithm class.<br>
/// </remarks><br>
public SymmCrypto(SymmProvEnum
NetSelected)<br>
{<br>
switch
(NetSelected)<br>
{<br>
case SymmProvEnum.DES:<br>
mobjCryptoService = new DESCryptoServiceProvider();<br>
break;<br>
case SymmProvEnum.RC2:<br>
mobjCryptoService = new RC2CryptoServiceProvider();<br>
break;<br>
case SymmProvEnum.Rijndael:<br>
mobjCryptoService = new RijndaelManaged();<br>
break;<br>
}<br>
}</p>
<p> /// <remarks><br>
/// Constructor for using a
customized SymmetricAlgorithm class.<br>
/// </remarks><br>
public SymmCrypto(SymmetricAlgorithm
ServiceProvider)<br>
{<br>
mobjCryptoService = ServiceProvider;<br>
}</p>
<p> /// <remarks><br>
/// Depending on the legal key size
limitations of a specific CryptoService provider<br>
/// and length of the private key
provided, padding the secret key with space character<br>
/// to meet the legal size of the
algorithm.<br>
/// </remarks><br>
private byte[] GetLegalKey(string
Key)<br>
{<br>
string
sTemp;<br>
if
(mobjCryptoService.LegalKeySizes.Length > 0)<br>
{<br>
int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;<br>
// key sizes are in bits<br>
while (Key.Length * 8 > moreSize)<br>
{<br>
lessSize = moreSize;<br>
moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;<br>
}<br>
sTemp = Key.PadRight(moreSize / 8, ' ');<br>
}<br>
else<br>
sTemp = Key;</p>
<p> // convert
the secret key to byte array<br>
return
ASCIIEncoding.ASCII.GetBytes(sTemp);<br>
}</p>
<p> public string Encrypting(string
Source, string Key)<br>
{<br>
byte[] bytIn
= System.Text.ASCIIEncoding.ASCII.GetBytes(Source);<br>
// create a
MemoryStream so that the process can be done without I/O files<br>
System.IO.MemoryStream ms = new System.IO.MemoryStream();</p>
<p> byte[] bytKey
= GetLegalKey(Key);</p>
<p> // set the
private key<br>
mobjCryptoService.Key = bytKey;<br>
mobjCryptoService.IV = bytKey;</p>
<p> // create an
Encryptor from the Provider Service instance<br>
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();</p>
<p> // create
Crypto Stream that transforms a stream using the encryption<br>
CryptoStream
cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);</p>
<p> // write out
encrypted content into MemoryStream<br>
cs.Write(bytIn, 0, bytIn.Length);<br>
cs.FlushFinalBlock();</p>
<p> // get the
output and trim the '\0' bytes<br>
byte[]
bytOut = ms.GetBuffer();<br>
int i = 0;<br>
for (i = 0;
i < bytOut.Length; i++)<br>
if (bytOut[i] == 0)<br>
break;</p>
<p> // convert
into Base64 so that the result can be used in xml<br>
return
System.Convert.ToBase64String(bytOut, 0, i);<br>
}</p>
<p> public string Decrypting(string
Source, string Key)<br>
{<br>
// convert
from Base64 to binary<br>
byte[] bytIn
= System.Convert.FromBase64String(Source);<br>
// create a
MemoryStream with the input<br>
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);</p>
<p> byte[] bytKey
= GetLegalKey(Key);</p>
<p> // set the
private key<br>
mobjCryptoService.Key = bytKey;<br>
mobjCryptoService.IV = bytKey;</p>
<p> // create a
Decryptor from the Provider Service instance<br>
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();</p>
<p> // create
Crypto Stream that transforms a stream using the decryption<br>
CryptoStream
cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);<br>
</p>
<p> // read out
the result from the Crypto Stream<br>
System.IO.StreamReader sr = new System.IO.StreamReader( cs );<br>
return
sr.ReadToEnd();<br>
}<br>
}<br>
}</p>
</body>
</html>
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.