Thứ Hai, 13 tháng 2, 2017

Some useful powershell commands

get-command
ex: get-command *msmq*

get-verb
ex: get-verb

get-help
ex: get-help get-service

help
ex: help get-service

select
ex: get-command *msmq* | select Name, Version

get-member
ex: get-command *msmq* | get-member

out-file
ex: get-command *msmq* | out-file c:\abc.txt | notepad c:\abc.txt

Thứ Bảy, 14 tháng 1, 2017

Create a new user in Azure Active Directory

Step 1: Azure Active Directory -> Domain names -> copy the domain name

Step 2: Azure Active Directory -> Users and groups -> All users -> Add

Step 3: Create the user name with the domain name in step 1

Thứ Năm, 12 tháng 1, 2017

Configure Excel Page Layout using EPPlus

worksheet.PrinterSettings.FitToPage = true;
worksheet.PrinterSettings.FitToWidth = 1;
worksheet.PrinterSettings.FitToHeight = 0;

Add Excel Tooltip (Data Validation) using EPPlus

public static void AddToolTip(ExcelRange excelRange, string title, string text)
{
    var dataValidation = excelRange.DataValidation.AddAnyDataValidation();
    dataValidation.ShowInputMessage = true;
    dataValidation.PromptTitle = title;
    dataValidation.Prompt = text;
}

Thứ Hai, 9 tháng 1, 2017

Asymmetric Algorithm examples in .Net

RSA using RSAParameter Key
private RSAParameters _publicKey;
private RSAParameters _privateKey;

public void AssignNewKey()
{
    using (var rsa = new RSACryptoServiceProvider(2048))
    {
        rsa.PersistKeyInCsp = false;
        _publicKey = rsa.ExportParameters(false);
        _privateKey = rsa.ExportParameters(true);
    }
}

public byte[] EncryptData(byte[] dataToEncrypt)
{
    using (var rsa = new RSACryptoServiceProvider(2048))
    {
        rsa.PersistKeyInCsp = false;
        rsa.ImportParameters(_publicKey);

        return rsa.Encrypt(dataToEncrypt, true);
    }
}

public byte[] DecryptData(byte[] dataToDecrypt)
{
    using (var rsa = new RSACryptoServiceProvider(2048))
    {
        rsa.PersistKeyInCsp = false;

        rsa.ImportParameters(_privateKey);
        return rsa.Decrypt(dataToDecrypt, true);
    }
}

RSA using Xml String Key
public void AssignNewKey(string publicKeyPath, string privateKeyPath)
{
    using (var rsa = new RSACryptoServiceProvider(2048))
    {
        rsa.PersistKeyInCsp = false;

        if (File.Exists(privateKeyPath))
        {
            File.Delete(privateKeyPath);
        }

        if (File.Exists(publicKeyPath))
        {
            File.Delete(publicKeyPath);
        }

        var publicKeyfolder = Path.GetDirectoryName(publicKeyPath);
        var privateKeyfolder = Path.GetDirectoryName(privateKeyPath);

        if (!Directory.Exists(publicKeyfolder))
        {
            Directory.CreateDirectory(publicKeyfolder);
        }

        if (!Directory.Exists(privateKeyfolder))
        {
            Directory.CreateDirectory(privateKeyfolder);
        }

        File.WriteAllText(publicKeyPath, rsa.ToXmlString(false));
        File.WriteAllText(privateKeyPath, rsa.ToXmlString(true));
    }
}

public byte[] EncryptData(string publicKeyPath, byte[] dataToEncrypt)
{
    using (var rsa = new RSACryptoServiceProvider(2048))
    {
        rsa.PersistKeyInCsp = false;
        rsa.FromXmlString(File.ReadAllText(publicKeyPath));

        return rsa.Encrypt(dataToEncrypt, false);
    }
}

public byte[] DecryptData(string privateKeyPath, byte[] dataToDecrypt)
{
    using (var rsa = new RSACryptoServiceProvider(2048))
    {
        rsa.PersistKeyInCsp = false;
        rsa.FromXmlString(File.ReadAllText(privateKeyPath));
        return rsa.Decrypt(dataToDecrypt, false);
    }
}

RSA using KeyContainerName
const string ContainerName = "MyContainer";

public void AssignNewKey()
{
    CspParameters cspParams = new CspParameters(1);
    cspParams.KeyContainerName = ContainerName;
    cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
    cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";

    var rsa = new RSACryptoServiceProvider(cspParams) { PersistKeyInCsp = true };
}

public void DeleteKeyInCsp()
{
    var cspParams = new CspParameters { KeyContainerName = ContainerName };
    var rsa = new RSACryptoServiceProvider(cspParams) { PersistKeyInCsp = false };

    rsa.Clear();                         
}

public byte[] EncryptData(byte[] dataToEncrypt)
{
    var cspParams = new CspParameters { KeyContainerName = ContainerName };

    using (var rsa = new RSACryptoServiceProvider(2048, cspParams))
    {
        return rsa.Encrypt(dataToEncrypt, false);
    }
}

public byte[] DecryptData(byte[] dataToDecrypt)
{
    var cspParams = new CspParameters { KeyContainerName = ContainerName };

    using (var rsa = new RSACryptoServiceProvider(2048, cspParams))
    {                               
        return rsa.Decrypt(dataToDecrypt, false);
    }
}