Saturday, October 30, 2021

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

 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.

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...