Friday, September 27, 2013

How to get the datasource name from an Entity Framework in C#

If the entity framework model is in a different project, you have to add System.Data.Entity as a reference.
Assuming the name of the Entity Framework context is

"LIQBillsAndStatementsEntities"

then this is how you do it: Its hard.
 
textBoxStagingdataSource.Text = new LIQBillsAndStatementsEntities().Connection.DataSource;

Wednesday, September 25, 2013

Encryption Library - base 64

using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Security.Cryptography;
using System.Text;
 
namespace Applications.Common.Utilities
{
    public class Cryptography
    {
        private readonly byte[] _cryptVector = Encoding.ASCII.GetBytes("yolbutpddklczekf");
        private readonly byte[] _cryptKey = Encoding.ASCII.GetBytes("ivcsfemmnqejpxcnkbccebpmzzsuilxv");
 
        [SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
        public string EncryptString(string clearText)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                byte[] clearTextBytes = Encoding.UTF8.GetBytes(clearText);
                SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
 
                using (CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(_cryptKey, _cryptVector),
                                                       CryptoStreamMode.Write))
                {
                    cs.Write(clearTextBytes, 0, clearTextBytes.Length);
                }
 
                return Convert.ToBase64String(ms.ToArray());
            }
        }
 
        [SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
        public string DecryptString(string encryptedText)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                byte[] encryptedTextBytes = Convert.FromBase64String(encryptedText);
                SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
 
                using (CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(_cryptKey, _cryptVector),
                                                       CryptoStreamMode.Write))
                {
                    cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);
                }
 
                return Encoding.UTF8.GetString(ms.ToArray());
            }
        }
    }
}