Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 12-19-2005, 09:47 PM   #1 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default Encryption Troubles

[edit]

Actually I'm having problems with decryption...

Code:
		public static void DecryptRewrite( string FilePath )
		{
			string filedata;

			//create streams
			System.IO.FileStream decryptfile;
			System.IO.StreamReader readfile;
			System.Security.Cryptography.CryptoStream cstream;

			//create crypto
			DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

			//create key
			cryptic.Key = ASCIIEncoding.ASCII.GetBytes( "xxxxxxxx" );
			cryptic.IV = cryptic.IV = ASCIIEncoding.ASCII.GetBytes( "xxxxxxxx" );

			//open file to be decrypted
			decryptfile = new FileStream( FilePath, FileMode.Open, FileAccess.Read );

			//decrypt decryptfile
			cstream = new CryptoStream( decryptfile, cryptic.CreateDecryptor(), CryptoStreamMode.Read );

			//read file data and store in data string
			readfile = new StreamReader( FilePath );
			filedata = readfile.ReadToEnd();
			byte[] data = ASCIIEncoding.ASCII.GetBytes( filedata );

			//close streams
			readfile.Close();
			cstream.Close();

			//rewrite file with decrypted data
			System.IO.FileStream rewritefile = new FileStream( FilePath, FileMode.OpenOrCreate, FileAccess.Write );
			rewritefile.Write( data, 0, data.Length );
			rewritefile.Close();
		}
In the "rewrite file with decrypted data" section at the end of the method there's some kind of problem with the writing of the data into the file. When I run the program, it gives me the exception:
Quote:
An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll

Additional information: Stream does not support writing.
How am I supposed rewrite the decrypted data into the file?

Last edited by Cmonkey123; 12-19-2005 at 10:49 PM.
Cmonkey123 is offline   Reply With Quote
Old 12-22-2005, 11:54 AM   #2 (permalink)
Forum Expert
 
Join Date: Sep 2002
Age: 23
Posts: 1,472
Default

MSDN has a great example of DES in action:


Code:
// This sample demonstrates using a key based on the cryptographic service provider (CSP) version
// of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then 
// to decrypt the byte array back to a string.

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

class CryptoMemoryStream
{
    // Main method.
    public static void Main()
    {
        // Create a new DES key.
        DESCryptoServiceProvider key = new DESCryptoServiceProvider();

        // Encrypt a string to a byte array.
        byte[] buffer = Encrypt("This is some plaintext!", key);

        // Decrypt the byte array back to a string.
        string plaintext =  Decrypt(buffer, key);

        // Display the plaintext value to the console.
        Console.WriteLine(plaintext);
    }
    
    // Encrypt the string.
    public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
    {
        // Create a memory stream.
        MemoryStream ms = new MemoryStream();

        // Create a CryptoStream using the memory stream and the 
        // CSP DES key.  
        CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

        // Create a StreamWriter to write a string
        // to the stream.
        StreamWriter sw = new StreamWriter(encStream);

        // Write the plaintext to the stream.
        sw.WriteLine(PlainText);

        // Close the StreamWriter and CryptoStream.
        sw.Close();
        encStream.Close();

        // Get an array of bytes that represents
        // the memory stream.
        byte[] buffer = ms.ToArray();

        // Close the memory stream.
        ms.Close();

        // Return the encrypted byte array.
        return buffer;
    }

   // Decrypt the byte array.
    public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
    {
        // Create a memory stream to the passed buffer.
        MemoryStream ms = new MemoryStream(CypherText);

        // Create a CryptoStream using the memory stream and the 
        // CSP DES key. 
        CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);

        // Create a StreamReader for reading the stream.
        StreamReader sr = new StreamReader(encStream);

        // Read the stream as a string.
        string val = sr.ReadLine();

        // Close the streams.
        sr.Close();
        encStream.Close();
        ms.Close();
            
        return val;
    }
}
Ravatar is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5