Encrypting & decrypta txt file
-
Hi all... I'm asking of how i can encrypt and decript a txt file using C#, i think u don't mined to help me ... Thank u............... :-D
-
Hi all... I'm asking of how i can encrypt and decript a txt file using C#, i think u don't mined to help me ... Thank u............... :-D
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
-
Hi all... I'm asking of how i can encrypt and decript a txt file using C#, i think u don't mined to help me ... Thank u............... :-D
because you want both Encrypting and Decrypting I would offer you gunzipstream
public string Decode() { FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Open, FileAccess.ReadWrite); byte\[\] array = new byte\[400\]; GZipStream gstream = new GZipStream(fs, CompressionMode.Decompress); gstream.Read(array, 0, array.Length); gstream.Close(); string Mytext = Encoding.UTF8.GetString(array); return Mytext; } public void Encode(string text) { byte\[\] array = Encoding.UTF8.GetBytes(text); FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Create, FileAccess.ReadWrite); GZipStream gstream = new GZipStream(fs, CompressionMode.Compress); gstream.Write(array, 0, array.Length); gstream.Flush(); gstream.Close(); }
note: this solution is not completely secure!
-
because you want both Encrypting and Decrypting I would offer you gunzipstream
public string Decode() { FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Open, FileAccess.ReadWrite); byte\[\] array = new byte\[400\]; GZipStream gstream = new GZipStream(fs, CompressionMode.Decompress); gstream.Read(array, 0, array.Length); gstream.Close(); string Mytext = Encoding.UTF8.GetString(array); return Mytext; } public void Encode(string text) { byte\[\] array = Encoding.UTF8.GetBytes(text); FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Create, FileAccess.ReadWrite); GZipStream gstream = new GZipStream(fs, CompressionMode.Compress); gstream.Write(array, 0, array.Length); gstream.Flush(); gstream.Close(); }
note: this solution is not completely secure!
Hi, How could you make this happen on the click of a button? I've tried adding it to the event but the Public void Encode(string text) line obviously stops this happening. edit: Some of my terminology may not be correct, so if your unsure, just ask.
modified on Monday, December 21, 2009 10:41 AM
-
Hi, How could you make this happen on the click of a button? I've tried adding it to the event but the Public void Encode(string text) line obviously stops this happening. edit: Some of my terminology may not be correct, so if your unsure, just ask.
modified on Monday, December 21, 2009 10:41 AM
just insert button on the form and double click it, Then write button1_Click event code like shown. In this sample I encoded text of TextBox1 to 'Myfile.dat' in My Document note: must copy Encode function to Form class
private void button1\_Click(object sender, EventArgs e) { Encode(TextBox1.Text); } public void Encode(string text) { byte\[\] array = Encoding.UTF8.GetBytes(text); FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Create, FileAccess.ReadWrite); GZipStream gstream = new GZipStream(fs, CompressionMode.Compress); gstream.Write(array, 0, array.Length); gstream.Flush(); gstream.Close(); }
modified on Thursday, December 24, 2009 10:23 AM
-
just insert button on the form and double click it, Then write button1_Click event code like shown. In this sample I encoded text of TextBox1 to 'Myfile.dat' in My Document note: must copy Encode function to Form class
private void button1\_Click(object sender, EventArgs e) { Encode(TextBox1.Text); } public void Encode(string text) { byte\[\] array = Encoding.UTF8.GetBytes(text); FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MyFile.dat", FileMode.Create, FileAccess.ReadWrite); GZipStream gstream = new GZipStream(fs, CompressionMode.Compress); gstream.Write(array, 0, array.Length); gstream.Flush(); gstream.Close(); }
modified on Thursday, December 24, 2009 10:23 AM
I get the following error when my code is run: "The given path's format is not supported." The error is in this line:
FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "MYFILE.txt", FileMode.Create, FileAccess.ReadWrite);
It cant be a problem with my txt file i dont think, because i have used them before in my programs and they have worked fine. Any help would be appreciated. Extra information: - Using VS 2008 - .NET Framework 3.5 SP1
-
I get the following error when my code is run: "The given path's format is not supported." The error is in this line:
FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "MYFILE.txt", FileMode.Create, FileAccess.ReadWrite);
It cant be a problem with my txt file i dont think, because i have used them before in my programs and they have worked fine. Any help would be appreciated. Extra information: - Using VS 2008 - .NET Framework 3.5 SP1
thanks you forget the "\\" before "MYFILE.txt" corrct that to this:
FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MYFILE.txt", FileMode.Create, FileAccess.ReadWrite);
-
thanks you forget the "\\" before "MYFILE.txt" corrct that to this:
FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\MYFILE.txt", FileMode.Create, FileAccess.ReadWrite);
Thanks for your help so far. Just 1 more question. Ive managed to succesfully encrypt the data that is inputted into textbox1, but decoding it fails.
public Form1()
{
InitializeComponent();
Decode();
}
private void button2_Click(object sender, EventArgs e){ Decode(); StreamReader rb = new StreamReader("C:\\\\file.txt"); MessageBox.Show(rb.ReadToEnd()); rb.Close(); } public string Decode() { FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\file.txt", FileMode.Open, FileAccess.ReadWrite); byte\[\] array = new byte\[400\]; GZipStream gstream = new GZipStream(fs, CompressionMode.Decompress); gstream.Read(array, 0, array.Length); gstream.Close(); string Mytext = Encoding.UTF8.GetString(array); return Mytext; }
It doesnt return any errors, it just clears the file. Any help would be appreciated
-
Thanks for your help so far. Just 1 more question. Ive managed to succesfully encrypt the data that is inputted into textbox1, but decoding it fails.
public Form1()
{
InitializeComponent();
Decode();
}
private void button2_Click(object sender, EventArgs e){ Decode(); StreamReader rb = new StreamReader("C:\\\\file.txt"); MessageBox.Show(rb.ReadToEnd()); rb.Close(); } public string Decode() { FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\\\file.txt", FileMode.Open, FileAccess.ReadWrite); byte\[\] array = new byte\[400\]; GZipStream gstream = new GZipStream(fs, CompressionMode.Decompress); gstream.Read(array, 0, array.Length); gstream.Close(); string Mytext = Encoding.UTF8.GetString(array); return Mytext; }
It doesnt return any errors, it just clears the file. Any help would be appreciated