Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Migbar
More...


Resources » Code Snippets » Encryption

How Create text file with encryption text.


Posted Date:     Category: Encryption    
Author: Member Level: Gold    Points: 30


This resource about how to create a text file with encrypted text in that file. And Retrive that text in Decrepted format using Rijndael Algorithm using System.Security.Cryptography And how to change extension at runtime.



 


This resource gives us knowledge of how to create text file with particular path which is static in this resource but we can make it dynamic. And write string in that text file with

encrypted formate. And when we need at that time we will decrypt it and then read it. here is the code of Encryption and Decryption using Rijndael Algorithm.

First of all we have to add Namespace like

using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.IO;
using System.Configuration;
using System.Net;





Now we will see how to create text with encrypted string file in specific folder.


string localComputerName = Dns.GetHostName();
IPHostEntry ip = Dns.GetHostEntry(localComputerName);
string localIPAddress = ip.AddressList[1].ToString();

string filepath = Application.StartupPath + "\\BinUsers.dll";
FileStream fs = null;
if (!File.Exists(filepath))
{
string extension = Path.GetExtension(filepath);
filepath.Replace(extension, ".txt");
filepath = Application.StartupPath + "\\BinUsers.txt";
using (fs = File.Create(filepath))
{

}
using (StreamWriter sw = new StreamWriter(filepath))
{

string finalstr = EncryptString(localIPAddress, "password");
sw.Write(finalstr);
}
FileInfo f1 = new FileInfo(filepath);
f1.MoveTo(Path.ChangeExtension(filepath, ".dll"));
}




here i am writing ip address of the localcomputer so in Above Code first line means Localcomputername gets the Computer name and through IPHostEntry we can get the IP address of the local computer. And the variable filepath stores the path of the file created or will be create. Here file's extension is .dll so we have to change it into .txt.
and then through FileStream we will create text file and then Through StreamWriter we will write IP address in that file but before writing anything we will encrypt the OP Address and then write the file. For Getting IPAddress below code is used

string localComputerName = Dns.GetHostName();
IPHostEntry ip = Dns.GetHostEntry(localComputerName);
string localIPAddress = ip.AddressList[1].ToString();


And this code used to create text file


string filepath = Application.StartupPath + "\\BinUsers.dll";
FileStream fs = null;
if (!File.Exists(filepath))
{
string extension = Path.GetExtension(filepath);
filepath.Replace(extension, ".txt");
filepath = Application.StartupPath + "\\BinUsers.txt";
using (fs = File.Create(filepath))
{

}


And this code used to write the string in text file with encryption


using (StreamWriter sw = new StreamWriter(filepath))
{

string finalstr = EncryptString(localIPAddress, "password");
sw.Write(finalstr);
}





Decrypt string


To decrept the string of the text file below code is used



fileLoc = Application.StartupPath + "\\BinUsers.txt";
using (TextReader tr = new StreamReader(fileLoc))
{
TextOfFile = DecryptString(tr.ReadLine(),"password")+",";
}



Private Functions




public string EncryptString(string Text, string Password)
{
byte[] Bytes = System.Text.Encoding.Unicode.GetBytes(Text);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
byte[] encryptedbyte = Encrypt(Bytes, pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedbyte);
}




In Above code we pass two string
1) Text : This text means which we want to encrypt.
2) Password : this means password to decrypt.




public static byte[] Encrypt(byte[] Data, byte[] Keybyte, byte[] IVByte)
{
MemoryStream mstrm = new MemoryStream();
Rijndael algo = Rijndael.Create();
algo.Key = Keybyte;
algo.IV = IVByte;
CryptoStream cs = new CryptoStream(mstrm, algo.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(Data, 0, Data.Length);
cs.Close();
byte[] encryptedByte = mstrm.ToArray();
return encryptedByte;
}


In Above function we use Rijndael Algorithm for encryption. And this function returns EncryptedBytes.


Through Above two function we can Encrypt the string which is passed. Now will see How to Decrypt that Encrypted data.


public string DecryptString(string EncText, string Password)
{
byte[] EncBytes = Convert.FromBase64String(EncText);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
byte[] decryptedbutes = Decrypt(EncBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return System.Text.Encoding.Unicode.GetString(decryptedbutes);
}




In Above Code we can see that we passed the encryption string and password which is we had given while Encryption.


public static byte[] Decrypt(byte[] cipherByte, byte[] Keybyte, byte[] IVbyte)
{
MemoryStream mstrm = new MemoryStream();
Rijndael algo = Rijndael.Create();
algo.Key = Keybyte;
algo.IV = IVbyte;
CryptoStream cstrm = new CryptoStream(mstrm, algo.CreateDecryptor(), CryptoStreamMode.Write);
cstrm.Write(cipherByte, 0, cipherByte.Length);
cstrm.Close();
byte[] decryptedbytes = mstrm.ToArray();
return decryptedbytes;
}


Above Two functions gives the out put of Decrepted string. When Decreption is over we can use that string in anywhere in our project.





Did you like this resource? Share it with your friends and show your love!


Responses to "How Create text file with encryption text."

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Encrypting Particular Section in a Web.config
    Previous Resource: Encryption with the help of IDEA algorithm
    Return to Resources
    Post New Resource
    Category: Encryption


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Create encrypted text file  .  Get decrypted text from file  .  Encryption and decryption of string  .  String encryption  .  String decryption  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.