using System.Security.Cryptography;
using System.IO;
static byte[] keys = ASCIIEncoding.ASCII.GetBytes("friction");
public static string StringEncrypt(string originalString)
{
if (String.IsNullOrEmpty(originalString))
{
throw new ArgumentNullException
("The string which needs to be encrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateEncryptor(keys, keys), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
public static string StringDecrypt(string cryptedString)
{
if (String.IsNullOrEmpty(cryptedString))
{
throw new ArgumentNullException
("The string which needs to be decrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream
(Convert.FromBase64String(cryptedString));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(keys, keys), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}
using System.IO;
static byte[] keys = ASCIIEncoding.ASCII.GetBytes("friction");
public static string StringEncrypt(string originalString)
{
if (String.IsNullOrEmpty(originalString))
{
throw new ArgumentNullException
("The string which needs to be encrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateEncryptor(keys, keys), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
public static string StringDecrypt(string cryptedString)
{
if (String.IsNullOrEmpty(cryptedString))
{
throw new ArgumentNullException
("The string which needs to be decrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream
(Convert.FromBase64String(cryptedString));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(keys, keys), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}
No comments:
Post a Comment