using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Developpez.Dotnet.Security.Cryptography
{
///
/// DESCrypting class
///
public class DESCrypting : ICrypting
{
#region Properties
/// Secret key
public byte[] RgbKey { private get; set; }
/// Initialisation vector
public byte[] RgbIV { private get; set; }
#endregion
#region Constructor
///
/// DESCrypting constructor
///
/// Secret key
/// Initialisation vector
public DESCrypting(string secretKey, string initialisationVector)
{
if (string.IsNullOrEmpty(secretKey)) throw new ArgumentNullException("The secret key cannot be null.");
if (string.IsNullOrEmpty(initialisationVector)) throw new ArgumentNullException("The initialisation vector cannot be null.");
RgbKey = new UnicodeEncoding().GetBytes(secretKey);
if (RgbKey.Length != 8) throw new ArgumentException("For Single DES the key is fixed size of 8 bytes.");
RgbIV = new UnicodeEncoding().GetBytes(initialisationVector);
}
#endregion
#region Encrypting
/// Encrypt an object by using DES encrypting
/// Object to encrypt
/// Encrypted text
public string EncryptContent(object objectToEncrypt)
{
if (objectToEncrypt == null) return null;
var encryptedContent = "";
var serviceProvider = new DESCryptoServiceProvider();
using (var msEncrypt = new MemoryStream())
{
using (var encryptor = serviceProvider.CreateEncryptor(RgbKey, RgbIV))
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var swEncrypt = new StreamWriter(csEncrypt))
swEncrypt.Write(objectToEncrypt);
encryptedContent = Convert.ToBase64String(msEncrypt.ToArray());
}
serviceProvider.Clear();
return encryptedContent;
}
/// Encrypt a file and replace its content
/// Path of the file
public void Encrypt(string path)
{
Encrypt(path, path);
}
/// Encrypt an non-crypted file into a new one
/// Path of the input file
/// Path of the output file
public void Encrypt(string pathInput, string pathOutput)
{
if (!File.Exists(pathInput)) throw new FileNotFoundException("The file " + pathInput + " cannot be found.");
var encryptedContent = EncryptContent(File.ReadAllText(pathInput));
File.WriteAllText(pathOutput, encryptedContent);
}
#endregion
#region Decrypting
///
/// Decrypt a text.
/// Example:
///
/// var decrypter = new DESCrypting("abcd", "efgh");
/// var xdocument = XDocument.Parse(decrypter.DecryptContent(File.OpenText("FilePath").ReadToEnd));
///
///
/// Encrypted text
/// Decrypted text
public string DecryptContent(string encryptedtext)
{
if (string.IsNullOrEmpty(encryptedtext)) return null;
byte[] buffer;
try
{
buffer = Convert.FromBase64String(encryptedtext);
}
catch (FormatException)
{
throw new ArgumentException("This is not an encrypted content.");
}
var decryptedContent = "";
var serviceProvider = new DESCryptoServiceProvider();
using (var msEncrypt = new MemoryStream(buffer))
using (var decryptor = serviceProvider.CreateDecryptor(RgbKey, RgbIV))
using (var csDecrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
using (var swDecrypt = new StreamReader(csDecrypt))
{
try
{
decryptedContent = swDecrypt.ReadToEnd();
}
catch (CryptographicException)
{
throw new CryptographicException("Invalid key or vector.");
}
finally
{
serviceProvider.Clear();
}
}
return decryptedContent;
}
/// Decrypt a file and replace its content
/// Path of the file
public void Decrypt(string path)
{
Decrypt(path, path);
}
/// Decrypt an encrypted file into a new one
/// Path of the input file
/// Path of the output file
public void Decrypt(string pathInput, string pathOutput)
{
if (!File.Exists(pathInput)) throw new FileNotFoundException("The file " + pathInput + " cannot be found.");
var decryptedContent = DecryptContent(File.ReadAllText(pathInput));
File.WriteAllText(pathOutput, decryptedContent);
}
#endregion
}
}