My Blog List

Tuesday, July 15, 2014

RSA Encryption and Decryption using Public and Private Key


RSA Key generation logic:


const int RSA_FULL = 1;
const string strSaltKey = "ABCDEFGHIJKLMNOP";   //16 Digit Key means 16*8*8 = 1024 bit
CspParameters cspParams;
cspParams = new CspParameters(RSA_FULL);
cspParams.KeyContainerName = strSaltKey ;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
var rsa = new RSACryptoServiceProvider(cspParams);

 string PublicPrivateKey = rsa.ToXmlString(true);
 string PublicKey = rsa.ToXmlString(false);

NameSpace :  using System.Security.Cryptography;


Encryption Methods:


private static bool _optimalAsymmetricEncryptionPadding = false;

 public static string EncryptText(string text, int keySize, string publicKeyXml)
 {
        var encrypted = Encrypt(Encoding.UTF8.GetBytes(text), publicKeyXml, keySize);
        return Convert.ToBase64String(encrypted);
 }
   
private static byte[] Encrypt(byte[] data, string publicKeyXml, int keySize)
{
      if (data == null || data.Length == 0)
      {
            throw new ArgumentException("Data are empty", "data");
      }
       int maxLength = GetMaxDataLength(keySize);
        if (data.Length > maxLength)
        {
            throw new ArgumentException(String.Format("Maximum data length is {0}", maxLength), "data");
        }

        if (!IsKeySizeValid(keySize))
        {
            throw new ArgumentException("Key size is not valid", "keySize");
        }

        if (String.IsNullOrEmpty(publicKeyXml))
        {
            throw new ArgumentException("Key is null or empty", "publicKeyXml");
        }
        using (var provider = new RSACryptoServiceProvider(keySize))
        {
            provider.FromXmlString(publicKeyXml);
            return provider.Encrypt(data, _optimalAsymmetricEncryptionPadding);
        }
  }
   
 public static int GetMaxDataLength(int keySize)
   {
        if (_optimalAsymmetricEncryptionPadding)
        {
            return ((keySize - 384) / 8) + 7;
        }
        return ((keySize - 384) / 8) + 37;
   }

  public static bool IsKeySizeValid(int keySize)
  {
        return keySize >= 384 &&
                keySize <= 16384 &&
                keySize % 8 == 0;
  }


Decryption Methods:


private static bool _optimalAsymmetricEncryptionPadding = false;

public static string DecryptText(string text, int keySize, string privateXml)
{
    byte[] decrypted = Decrypt(Convert.FromBase64String(text), keySize, privateXml);
    return Encoding.UTF8.GetString(decrypted);
}
public static byte[] Decrypt(byte[] data, int keySize, string publicAndPrivateKeyXml)
{
    if (data == null || data.Length == 0) throw new ArgumentException("Data are empty", "data");
    if (!IsKeySizeValid(keySize)) throw new ArgumentException("Key size is not valid", "keySize");
    if (String.IsNullOrEmpty(publicAndPrivateKeyXml)) throw new ArgumentException("Key is null or empty", "publicAndPr

    using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider(keySize))
    {
        provider.FromXmlString(publicAndPrivateKeyXml);
        return provider.Decrypt(data, _optimalAsymmetricEncryptionPadding);
    }
}
public static string EncryptText(string text, int keySize, string publicKeyXml)
{
    var encrypted = Encrypt(Encoding.UTF8.GetBytes(text), publicKeyXml, keySize);
    return Convert.ToBase64String(encrypted);
}
private static byte[] Encrypt(byte[] data, string publicKeyXml, int keySize)
{
    if (data == null || data.Length == 0)
    {
        throw new ArgumentException("Data are empty", "data");
    }
    int maxLength = GetMaxDataLength(keySize);
    if (data.Length > maxLength)
    {
        throw new ArgumentException(String.Format("Maximum data length is {0}", maxLength), "data");
    }

    if (!IsKeySizeValid(keySize))
    {
        throw new ArgumentException("Key size is not valid", "keySize");
    }

    if (String.IsNullOrEmpty(publicKeyXml))
    {
        throw new ArgumentException("Key is null or empty", "publicKeyXml");
    }
    using (var provider = new RSACryptoServiceProvider(keySize))
    {
        provider.FromXmlString(publicKeyXml);
        return provider.Encrypt(data, _optimalAsymmetricEncryptionPadding);
    }
}
public static int GetMaxDataLength(int keySize)
{
    if (_optimalAsymmetricEncryptionPadding)
    {
        return ((keySize - 384) / 8) + 7;
    }
    return ((keySize - 384) / 8) + 37;
}
public static bool IsKeySizeValid(int keySize)
{
    return keySize >= 384 &&
            keySize <= 16384 &&
            keySize % 8 == 0;
}


Sample Code to call Encrypt and Decrypt methods:


To Encrypt : 
     
     string strEncrypt = EncryptText("TEST", 1024, strPublicKey);


To Decrypt:

    string strDecrypt = DecryptText(strEncrypt , 1024, strPrivateKey);


Wednesday, July 9, 2014

Friday, July 4, 2014

Auto VPN (Connect CISCO VPN from C#.Net)

To connect the CISCO VPN, we need to enter the User Name and Password (Every Time).





To avoid the manual process.  We need to launch the VPN from C#.Net.


Step #1:   Create a new console application.

Step #2:   Place the below code in the Page_Load

try

{
                System.Diagnostics.Process[] remoteByName = System.Diagnostics.Process.GetProcessesByName("vpngui");
                if (remoteByName.Length == 0)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Cisco Systems\VPN Client\vpngui.exe", "-c -sd -user " + System.Configuration.ConfigurationManager.AppSettings["VPN_UserName"].ToString() + " -pwd " + System.Configuration.ConfigurationManager.AppSettings["VPN_Password"].ToString());
                    }
                    catch
                    {
                        System.Diagnostics.Process.Start(@"C:\Program Files\Cisco Systems\VPN Client\vpngui.exe", "-c -sd -user " + System.Configuration.ConfigurationManager.AppSettings["VPN_UserName"].ToString() + " -pwd " + System.Configuration.ConfigurationManager.AppSettings["VPN_Password"].ToString());
                    }
                }
                else
                {
                    try
                    {
                        System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Cisco Systems\VPN Client\vpnclient.exe","disconnect" );
                    }
                    catch
                    {
                        System.Diagnostics.Process.Start(@"C:\Program Files\Cisco Systems\VPN Client\vpnclient.exe","disconnect");
                    } 
                    remoteByName[0].Kill();
                }
            }
            catch
            {
                Console.WriteLine("Error:  While processing the request");
                Console.ReadLine();

            }

Step #3:  Open the App.Config file and place the below code.


 <appSettings>

    <add key="VPN_UserName" value="USERNAME"/>
    <add key="VPN_Password" value="PASSWORD"/>

  </appSettings>

Note:  Here you can set the USERNAME is your VPN user name and PASSWORD is your VPN Password.


Step #4:  Run the AutoVPN.exe file.


Functionality:  If the VPN is running it will disconnect, otherwise it will launch the VPN (No need to enter the user name and password).