public string md5(string strInput)
{
MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider();
byte[] bArray = Encoding.UTF8.GetBytes(strInput);
bArray = md5CryptoServiceProvider.ComputeHash(bArray);
StringBuilder stringBuilder = new StringBuilder();
foreach (byte b in bArray)
stringBuilder.Append(b.ToString("x2").ToLower());
return stringBuilder.ToString();
}
string strStackTrace = "";
StackTrace stackTrace = new StackTrace(ex, true);
StackFrame[] stackFrames = stackTrace.GetFrames();
if (stackFrames != null)
foreach (StackFrame stackFrame in stackFrames)
strStackTrace +=
String.Format(
"FileName : {0}, Method : {1}, LineNumber : {2}, ColumnNumber : {3}, ILOffset : {4}, NativeOffset : {5}{6}",
!string.IsNullOrEmpty(stackFrame.GetFileName())
? Path.GetFileName(stackFrame.GetFileName())
: "",
stackFrame.GetMethod().Name,
stackFrame.GetFileLineNumber(),
stackFrame.GetFileColumnNumber(),
stackFrame.GetILOffset(),
stackFrame.GetNativeOffset(),
Environment.NewLine);
List<ConversionBase> selectedConversions = new List<ConversionBase>();
foreach (ListViewItem listViewItem in lstConversionExists.SelectedItems)
selectedConversions.Add(listViewItem.Tag as ConversionBase);
new Thread(
delegate()
{
NDAConversion ndaConversion = new NDAConversion();
ndaConversion.log += log;
ndaConversion.updateUserReferences(selectedConversions);
})
.Start();
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Runtime.Remoting.Messaging;
namespace CommonUtils
{
#region SmtpServer Class
/// <summary>
/// General purpose class for handling email operations
/// </summary>
public class SmtpServer
{
/// <summary>
/// The smtp client that will send emails
/// </summary>
private readonly SmtpClient smtpClient;
/// <summary>
/// The logger
/// </summary>
public Log Log{get; set;}
/// <summary>
/// The last exception
/// </summary>
public Exception TheLastException;
/// <summary>
/// The default constructor
/// </summary>
/// <param name="host">SMTP host</param>
/// <param name="userName">SMTP user name</param>
/// <param name="password">SMTP password</param>
public SmtpServer(string host, string userName, string password)
{
smtpClient =
new SmtpClient
{
Host = host,
Credentials = new NetworkCredential(userName, password)
};
}
/// <summary>
/// Constructs a mail message from supplied parameters
/// </summary>
/// <param name="from">The from address</param>
/// <param name="tos">The to addresses</param>
/// <param name="ccs">The cc addresses</param>
/// <param name="bccs">The bcc addresses</param>
/// <param name="subject">The subject of the message</param>
/// <param name="body">The body of the message</param>
/// <param name="mailStatus">The out parameter for the mail status, in case of error</param>
/// <returns>If success, returns the mail message, else returns null. In case of error, please check mailStatus</returns>
private static MailMessage constructMailMessage(
string from,
ICollection<string> tos,
ICollection<string> ccs,
ICollection<string> bccs,
string subject,
string body,
out MailStatus mailStatus)
{
mailStatus = null;
// We need a from address
if (string.IsNullOrEmpty(from))
{
mailStatus = MailStatus.EMPTY_FROM;
return null;
}
// We need at least 1 to address
if (tos == null || tos.Count <= 0)
{
mailStatus = MailStatus.EMPTY_TO;
return null;
}
bool hasTo = false;
foreach (string to in tos)
{
if (!string.IsNullOrEmpty(to))
{
hasTo = true;
break;
}
}
if (!hasTo)
{
mailStatus = MailStatus.EMPTY_TO;
return null;
}
// We need a subject
if (string.IsNullOrEmpty(subject))
{
mailStatus = MailStatus.EMPTY_SUBJECT;
return null;
}
// We need a body
if (string.IsNullOrEmpty(body))
{
mailStatus = MailStatus.EMPTY_BODY;
return null;
}
// Construct the mail message
MailMessage mailMessage =
new MailMessage
{
From = new MailAddress(from),
Subject = subject,
Body = body,
IsBodyHtml = true
};
// Add tos
foreach (string to in tos)
{
if (!string.IsNullOrEmpty(to))
{
mailMessage.To.Add(new MailAddress(to));
}
}
// Add ccs
if (ccs != null && ccs.Count > 0)
{
foreach (string cc in ccs)
{
if (!string.IsNullOrEmpty(cc))
{
mailMessage.CC.Add(new MailAddress(cc));
}
}
}
// Add bccs
if (bccs != null && bccs.Count > 0)
{
foreach (string bcc in bccs)
{
if (!string.IsNullOrEmpty(bcc))
{
mailMessage.Bcc.Add(new MailAddress(bcc));
}
}
}
return mailMessage;
}
/// <summary>
/// Sends a mail message
/// </summary>
/// <param name="from">The from address</param>
/// <param name="tos">The to addresses</param>
/// <param name="ccs">The cc addresses</param>
/// <param name="bccs">The bcc addresses</param>
/// <param name="subject">The subject of the message</param>
/// <param name="body">The body of the message</param>
/// <returns>Returns information as MailStatus</returns>
public MailStatus send(
string from,
ICollection<string> tos,
ICollection<string> ccs,
ICollection<string> bccs,
string subject,
string body)
{
MailStatus mailStatus;
MailMessage mailMessage =
constructMailMessage(
from,
tos,
ccs,
bccs,
subject,
body,
out mailStatus);
if (mailMessage == null)
return mailStatus ?? MailStatus.ERROR;
// Send the mail
try
{
if(Log != null)
Log.log("Send message to : " + mailMessage.To[0]);
smtpClient.Send(mailMessage);
if (Log != null)
Log.log("OK : " + mailMessage.To[0]);
}
catch (Exception ex)
{
TheLastException = ex;
return MailStatus.ERROR;
}
return MailStatus.SUCCESS;
}
/// <summary>
/// Send mail message delegate
/// </summary>
/// <param name="from">The from address</param>
/// <param name="tos">The to addresses</param>
/// <param name="ccs">The cc addresses</param>
/// <param name="bccs">The bcc addresses</param>
/// <param name="subject">The subject of the message</param>
/// <param name="body">The body of the message</param>
/// <returns>Returns information as MailStatus</returns>
public delegate MailStatus SendMailAsyncDelegate(
string from,
ICollection<string> tos,
ICollection<string> ccs,
ICollection<string> bccs,
string subject,
string body);
/// <summary>
/// The async callback event to be fired, when email send operation is finished
/// </summary>
/// <param name="ar">The async result</param>
private void SendMailAsyncCallBack(IAsyncResult ar)
{
SendMailAsyncDelegate del = (SendMailAsyncDelegate)((AsyncResult)ar).AsyncDelegate;
try
{
MailStatus result = del.EndInvoke(ar);
if (Log != null)
Log.log("Async result : " + result);
}
catch (Exception)
{
}
}
/// <summary>
/// Sends a mail message, in async fashion
/// </summary>
/// <param name="from">The from address</param>
/// <param name="tos">The to addresses</param>
/// <param name="ccs">The cc addresses</param>
/// <param name="bccs">The bcc addresses</param>
/// <param name="subject">The subject of the message</param>
/// <param name="body">The body of the message</param>
/// <returns>Returns information as MailStatus</returns>
public MailStatus sendAsync(
string from,
ICollection<string> tos,
ICollection<string> ccs,
ICollection<string> bccs,
string subject,
string body)
{
SendMailAsyncDelegate del = send;
AsyncCallback callback = SendMailAsyncCallBack;
del.BeginInvoke(
from,
tos,
ccs,
bccs,
subject,
body,
callback,
null);
return MailStatus.ASYNC_SENT;
}
}
#endregion
#region MailStatus Class
/// <summary>
/// General purpose class for checking status of mail sends
/// </summary>
public class MailStatus
{
/// <summary>
/// The string representation of the status
/// </summary>
private readonly string status;
/// <summary>
/// The prefix for string conversion
/// </summary>
private const string prefix = "SMTP_SERVER_SEND_MAIL_";
/// <summary>
/// STATUS_EMPTY_FROM Constant
/// </summary>
private const string STATUS_EMPTY_FROM = "STATUS_EMPTY_FROM";
/// <summary>
/// EMPTY_FROM MailStatus
/// </summary>
public static MailStatus EMPTY_FROM = new MailStatus(STATUS_EMPTY_FROM);
/// <summary>
/// STATUS_EMPTY_TO Constant
/// </summary>
private const string STATUS_EMPTY_TO = "STATUS_EMPTY_TO";
/// <summary>
/// EMPTY_TO MailStatus
/// </summary>
public static MailStatus EMPTY_TO = new MailStatus(STATUS_EMPTY_TO);
/// <summary>
/// STATUS_SUCCESS Constant
/// </summary>
private const string STATUS_SUCCESS = "STATUS_SUCCESS";
/// <summary>
/// SUCCESS MailStatus
/// </summary>
public static MailStatus SUCCESS = new MailStatus(STATUS_SUCCESS);
/// <summary>
/// STATUS_ERROR Constant
/// </summary>
private const string STATUS_ERROR = "STATUS_ERROR";
/// <summary>
/// ERROR MailStatus
/// </summary>
public static MailStatus ERROR = new MailStatus(STATUS_ERROR);
/// <summary>
/// STATUS_EMPTY_SUBJECT Constant
/// </summary>
private const string STATUS_EMPTY_SUBJECT = "STATUS_EMPTY_SUBJECT";
/// <summary>
/// EMPTY_SUBJECT MailStatus
/// </summary>
public static MailStatus EMPTY_SUBJECT = new MailStatus(STATUS_EMPTY_SUBJECT);
/// <summary>
/// STATUS_EMPTY_BODY Constant
/// </summary>
private const string STATUS_EMPTY_BODY = "STATUS_EMPTY_BODY";
/// <summary>
/// EMPTY_BODY MailStatus
/// </summary>
public static MailStatus EMPTY_BODY = new MailStatus(STATUS_EMPTY_BODY);
/// <summary>
/// STATUS_ASYNC_SENT Constant
/// </summary>
private const string STATUS_ASYNC_SENT = "STATUS_ASYNC_SENT";
/// <summary>
/// ASYNC_SENT MailStatus
/// </summary>
public static MailStatus ASYNC_SENT = new MailStatus(STATUS_ASYNC_SENT);
/// <summary>
/// Private constructor
/// </summary>
/// <param name="status">The string representation of the status</param>
private MailStatus(string status)
{
this.status = status;
}
/// <summary>
/// Implicit operator for string conversion
/// </summary>
/// <param name="mailStatus">The MailStatus instance to convert to string</param>
/// <returns>The string representation of the mail status</returns>
public static implicit operator string(MailStatus mailStatus)
{
return prefix + mailStatus.status;
}
}
#endregion
}
using System;
using System.Security.Cryptography;
using System.Text;
namespace CommonUtils
{
/// <summary>
/// General purpose security functions
/// </summary>
public class Security
{
#region Constructor
/// <summary>
/// Disable object instantiation
/// </summary>
private Security()
{
}
#endregion
#region Private Properties
/// <summary>
/// The key for encryption and decryption
/// </summary>
private static string key = "printf( @TRIODOR @BV - @OZER @SENTURK - @23.@06.@2009 @18:@10 )";
#endregion
#region encrypt
/// <summary>
/// Encrypts a string
/// </summary>
/// <param name="str">The string to encrypt</param>
/// <returns>The encrypted string</returns>
public static string encrypt(string str)
{
MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider();
byte[] arrKey = md5CryptoServiceProvider.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
byte[] arrStr = UTF8Encoding.UTF8.GetBytes(str);
TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();
tripleDESCryptoServiceProvider.Key = arrKey;
tripleDESCryptoServiceProvider.Mode = CipherMode.ECB;
tripleDESCryptoServiceProvider.Padding = PaddingMode.PKCS7;
ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor();
byte[] arrResult = cryptoTransform.TransformFinalBlock(arrStr, 0, arrStr.Length);
// Both MD5CryptoServiceProvider and TripleDESCryptoServiceProvider
// are managed wrappers around unmanaged resources. You need to use
// Clear (which in turn calls Dispose) or Dispose directly
md5CryptoServiceProvider.Clear();
tripleDESCryptoServiceProvider.Clear();
return Convert.ToBase64String(arrResult, 0, arrResult.Length);
}
#endregion
#region decrypt
/// <summary>
/// Decrypts a string
/// </summary>
/// <param name="str">The string to decrypt</param>
/// <returns>The decrypted string</returns>
public static string decrypt(string str)
{
MD5CryptoServiceProvider md5CryptoServiceProvider = new MD5CryptoServiceProvider();
byte[] arrKey = md5CryptoServiceProvider.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
byte[] arrStr = Convert.FromBase64String(str);
TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();
tripleDESCryptoServiceProvider.Key = arrKey;
tripleDESCryptoServiceProvider.Mode = CipherMode.ECB;
tripleDESCryptoServiceProvider.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tripleDESCryptoServiceProvider.CreateDecryptor();
byte[] arrResult = cTransform.TransformFinalBlock(arrStr, 0, arrStr.Length);
// Both MD5CryptoServiceProvider and TripleDESCryptoServiceProvider
// are managed wrappers around unmanaged resources. You need to use
// Clear (which in turn calls Dispose) or Dispose directly
md5CryptoServiceProvider.Clear();
tripleDESCryptoServiceProvider.Clear();
return UTF8Encoding.UTF8.GetString(arrResult);
}
#endregion
}
}