SIGN UP MEMBER LOGIN:    
ARTICLE

Encrypt a File using DES (Data Encryption standard) Algorithm in ASP.NET

Posted by Brijesh Jalan Articles | Cryptography C# August 16, 2010
In this article we will learn how we can encrypt a file using DES (Data Encryption standard) algorithm in ASP.NET.
Reader Level:

Security in cryptography

Cryptography play important role in Security. Using Cryptography we can Encrypt and Decrypt the information in the Coded form.

How we Encrypt the File using DES (Data Encryption standard) algorithm.

1.Open visual studio and create a project

2.Add the following assembly on the page

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

3.Now write the Code in the Page

public class Tester
{
    public static void Main()
    {
         try
        {
            DESCryptoServiceProvider myDESProvider = new DESCryptoServiceProvider();
             myDESProvider.Key = ASCIIEncoding.ASCII.GetBytes("12345678");
            myDESProvider.IV = ASCIIEncoding.ASCII.GetBytes("12345678");

            ICryptoTransform myICryptoTransform = myDESProvider.CreateEncryptor(myDESProvider.Key, myDESProvider.IV);

            FileStream ProcessFileStream = new FileStream("test.txt", FileMode.Open, FileAccess.Read);
            FileStream ResultFileStream = new FileStream("testDes.txt", FileMode.Create, FileAccess.Write);
            CryptoStream myCryptoStream = new CryptoStream(ResultFileStream, myICryptoTransform, CryptoStreamMode.Write);
             byte[] bytearrayinput = new byte[ProcessFileStream.Length];
             ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length);
            myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length);
             myCryptoStream.Close();
            ProcessFileStream.Close();
            ResultFileStream.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

}

4.Now create a text file in the folder D:\New Folder (2)\ConsoleApplication4\ConsoleApplication4\bin\Debug (My text file path) with some text which you want to encrypt .

Now start debugging, you will see a testDes.txt folder automatically generated in the folder that is encrypted folder.

Login to add your contents and source code to this article
share this article :
post comment
 

ya ofcourse it will work

Posted by Brijesh Jalan Jan 06, 2011

it is mostly time secure bt sometime nt....

Posted by Brijesh Jalan Aug 26, 2010

Nice, but everybody should know that DES is now considered insecure. Using of not yet compromised algorithms is strongly recommended. See http://en.wikipedia.org/wiki/Data_Encryption_Standard

Posted by Nikolay Aug 24, 2010

hello koti,
For public key encryption you can use RSA method to encrypt and decrypy

This code will help you

Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO


Public Class Tester
    Public Shared Sub Main

        Dim myRSAProvide As New RSACryptoServiceProvider()

        Dim strCrypt As String
        Dim strResult As String
        Dim bteCrypt() As Byte
        Dim bteResult() As Byte
        Try
            strCrypt = "hello my name is X"
            bteCrypt = Encoding.ASCII.GetBytes(strCrypt)
            bteResult = myRSAProvide.Encrypt(bteCrypt, False)
            Console.WriteLine(Encoding.ASCII.GetString(bteResult))

        Catch ex As CryptographicException
            Console.WriteLine(ex.Message)
        End Try

        Dim strResault As String
        Dim bteDecrypt() As Byte
        Try
            bteDecrypt = myRSAProvide.Decrypt(bteResult, False)
            strResault = Encoding.ASCII.GetString(bteDecrypt)
            Console.WriteLine(strResault)
        Catch ex As CryptographicException
            Console.WriteLine(ex.Message)
        End Try

    End Sub
End Class

Posted by Brijesh Jalan Aug 18, 2010

Yes you can but first you convert .exe to text and than encrypt the text file

Posted by Brijesh Jalan Aug 18, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor