How to do encryption and decryption in c#

For VB.Net

Imports Microsoft.VisualBasic
Imports System.Security.Cryptography

Public Class Crypto
#Region "Method for Encryption"

    Private Shared key As Byte() = {}
    Private Shared IV As Byte() = {38, 55, 206, 48, 28, 64, _
     20, 16}
    Private Shared stringKey As String = "!5663a#KN"
    Public Shared Function Encrypt(ByVal text As String) As String

        Try
            key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8))
            Dim des As New DESCryptoServiceProvider()
            Dim byteArray As [Byte]() = Encoding.UTF8.GetBytes(text)
            Dim memoryStream As New IO.MemoryStream()
            Dim cryptoStream As New CryptoStream(memoryStream, des.CreateEncryptor(key, IV), CryptoStreamMode.Write)
            cryptoStream.Write(byteArray, 0, byteArray.Length)
            cryptoStream.FlushFinalBlock()

            Return Convert.ToBase64String(memoryStream.ToArray())


            ' Handle Exception Here

        Catch
        End Try
        Return String.Empty
    End Function
    Public Shared Function Decrypt(ByVal text As String) As String
        Try
            'Convert.FromBase64String(stringToDecrypt); to
            'Convert.FromBase64String(stringToDecrypt.Replace(" ", "+"));

            key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8))
            Dim des As New DESCryptoServiceProvider()
            'Byte[] byteArray = Convert.FromBase64String(text);
            Dim byteArray As [Byte]() = Convert.FromBase64String(text.Replace(" ", "+"))
            Dim memoryStream As New IO.MemoryStream()
            Dim cryptoStream As New CryptoStream(memoryStream, des.CreateDecryptor(key, IV), CryptoStreamMode.Write)
            cryptoStream.Write(byteArray, 0, byteArray.Length)
            cryptoStream.FlushFinalBlock()
            Return Encoding.UTF8.GetString(memoryStream.ToArray())
            ' Handle Exception Here

        Catch
        End Try

        Return String.Empty

    End Function

#End Region

End Class


////////////////////////////////////


for C#








using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Security.Cryptography;

public class Crypto
{
 #region "Method for Encryption"

 private static byte[] key = {
  
 };
 private static byte[] IV = {
  38,
  55,
  206,
  48,
  28,
  64,
  20,
  16
 };
 private static string stringKey = "!5663a#KN";
 public static string Encrypt(string text)
 {

  try {
   key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
   DESCryptoServiceProvider des = new DESCryptoServiceProvider();
   Byte[] byteArray = Encoding.UTF8.GetBytes(text);
   System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
   CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
   cryptoStream.Write(byteArray, 0, byteArray.Length);
   cryptoStream.FlushFinalBlock();

   return Convert.ToBase64String(memoryStream.ToArray());


  // Handle Exception Here

  } catch {
  }
  return string.Empty;
 }
 public static string Decrypt(string text)
 {
  try {
   //Convert.FromBase64String(stringToDecrypt); to
   //Convert.FromBase64String(stringToDecrypt.Replace(" ", "+"));

   key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
   DESCryptoServiceProvider des = new DESCryptoServiceProvider();
   //Byte[] byteArray = Convert.FromBase64String(text);
   Byte[] byteArray = Convert.FromBase64String(text.Replace(" ", "+"));
   System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
   CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
   cryptoStream.Write(byteArray, 0, byteArray.Length);
   cryptoStream.FlushFinalBlock();
   return Encoding.UTF8.GetString(memoryStream.ToArray());
  // Handle Exception Here

  } catch {
  }

  return string.Empty;

 }

 #endregion

}

Comments

Popular posts from this blog

Executing PowerShell scripts from C#

HOW TO Use the NumericUpDown Control

Exposing Agile Software Development Myths