Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Cryptography C# » Encrypt a File using DES (Data Encryption standard) Algorithm in ASP.NET

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

In this article we will learn how we can encrypt a file using DES (Data Encryption standard) algorithm in ASP.NET.

Author Rank :
Page Views : 5638
Downloads : 130
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
DES Encrypt a text file in ASP.net.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Brijesh Jalan
Brijesh Jalan has been working as a software engineer since 2010. He has written articles for C# Corner . He specializes in the implementation of client/server, database, graphics and/or Internet-based systems using Visual Studio .NET suite. His area of expertise include: C#, ADO.NET, GDI+, Windows Forms, Web Services, Silverlight application,WPF and WCF and ASP.NET also woking on some tools like Expression blend,Expression Web,Sql server 2005/2008,Oracle 10g,My sql.Brijesh background includes Bachelor in Infomation Technology.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
it is good but what about decryption by Koteswararao On August 17, 2010
it work fine but where is the decryption code i need
Reply | Email | Modify 
Re: it is good but what about decryption by Brijesh On August 17, 2010
hello koti,
I hope this code will help you

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");

             FileStream DecryptedFile = new FileStream("testDes.txt", FileMode.Open, FileAccess.Read);

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

            CryptoStream myCryptoStream = new CryptoStream(DecryptedFile, myICryptoTransform, CryptoStreamMode.Read);

             StreamReader myDecStreamReader = new StreamReader(myCryptoStream);

            StreamWriter myDecStreamWriter = new StreamWriter("p1.txt");

             myDecStreamWriter.Write(myDecStreamReader.ReadToEnd());

             myCryptoStream.Close();

            myDecStreamReader.Close();

            myDecStreamWriter.Close();

         }

        catch (Exception ex)

        {

            Console.WriteLine(ex.ToString());

        }

    }

}


Reply | Email | Modify 
thanks by Koteswararao On August 17, 2010
ok i am already test it ,it will also work fine good what about public key algorithms if have any code send me 
Reply | Email | Modify 
Re: thanks by Brijesh On August 18, 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

Reply | Email | Modify 
.exe's by Andy On August 17, 2010
Can this method also be used to encrypt .exe
For example if I wanted to crypt a malicious file program of some sort and make it where it can still run, but it is not detected by antivirus. 
Reply | Email | Modify 
Re: .exe's by Brijesh On August 18, 2010
Yes you can but first you convert .exe to text and than encrypt the text file
Reply | Email | Modify 
Re: .exe's by Brijesh On January 6, 2011
ya ofcourse it will work
Reply | Email | Modify 
DES is insecure by Nikolay On August 24, 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
Reply | Email | Modify 
Re: DES is insecure by Brijesh On August 26, 2010
it is mostly time secure bt sometime nt....
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.