RSS

Encryption/Decryption Function in .NET

22 Nov

 

C#

 

public string Encrypt(string str)
    {
        string EncrptKey = “2013;[pnuLIT)X”;
        byte[] byKey = { };
        byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
        byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0, 8));
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        return Convert.ToBase64String(ms.ToArray());
    }

 public string Decrypt(string str)
    {
        str = str.Replace(” “, “+”);
        string DecryptKey = “2013;[pnuLIT)Y”;
        byte[] byKey = { };
        byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
        byte[] inputByteArray = new byte[str.Length];
        byKey = System.Text.Encoding.UTF8.GetBytes(DecryptKey.Substring(0, 8));
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        inputByteArray = Convert.FromBase64String(str.Replace(” “, “+”));
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        System.Text.Encoding encoding = System.Text.Encoding.UTF8;
        return encoding.GetString(ms.ToArray());
    }

 

VB.Net

Public Function Encrypt(str As String) As String
Dim EncrptKey As String = “2013;[pnuLIT)X”
Dim byKey As Byte() = {}
Dim IV As Byte() = {18, 52, 86, 120, 144, 171, _
205, 239}
byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0, 8))
Dim des As New DESCryptoServiceProvider()
Dim inputByteArray As Byte() = Encoding.UTF8.GetBytes(str)
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
End Function

 
Public Function Decrypt(str As String) As String
str = str.Replace(” “, “+”)
Dim DecryptKey As String = “2013;[pnuLIT)Y”
Dim byKey As Byte() = {}
Dim IV As Byte() = {18, 52, 86, 120, 144, 171, _
205, 239}
Dim inputByteArray As Byte() = New Byte(str.Length – 1) {}

byKey = System.Text.Encoding.UTF8.GetBytes(DecryptKey.Substring(0, 8))
Dim des As New DESCryptoServiceProvider()
inputByteArray = Convert.FromBase64String(str.Replace(” “, “+”))
Dim ms As New MemoryStream()
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
End Function

 

Advertisements
 
1 Comment

Posted by on November 22, 2013 in ASP Dot Net C#, Vb.Net

 

Tags: , , ,

One response to “Encryption/Decryption Function in .NET

  1. joni inglis

    January 16, 2016 at 5:54 am

    i love u… ty so much….?$?-u?Sd?t?h?~cD

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: