Encrypt Decrypt of a String in C# .NET

C# Encryption and Decryption of a String With a Simple Function Call

Cryptography is a big subject area and extremely important for modern software and programs. If you are writing any type of software you need an understanding of software security and methods to keep data, code and users secure. Encrypting data keeps it secure because it hides its meaning, converting the plaintext (or cleartext) to ciphertext. To see the data again you need to decrypt the ciphertext back to plaintext. A simple example is the encryption of passwords to protect them from use by others.

Encryption and decryption of a password or other strings containing data can be done in many ways. There are many character substitution or transposition methods that pre-date the computing era and can be traced back to classical times. Modern computer based methods use symmetric key and asymmetric key mathematical algorithms. There are lots of well established algorithms from which to choose. However not everyone wants to take a course in cryptography just to be able to encrypt a string to hide some data and decrypt it back again. That’s where this example C# encryption and decryption code comes in handy. This code was tested in Visual Studio 2013.

This C# code has been boiled down to an encryption function that takes a plaintext string and passphrase and returns an encrypted string. There is the reverse function that takes the ciphertext and the passphrase and returns the plaintext. This is a quick and easy method of adding some encryption to a C# project, or any .NET project. The encrypt decrypt C# string functions are in a simple class. An example project shows how simple it is to use them. Continue reading

Text File Edit Component for .NET WinForms Applications

A WinForms .NET Control to Add Text and Source Editing to Windows Applications

Here we show you how to add a lightweight .NET text editor component to a Windows Forms application, the control supports color coding when editing source files (based on the file extension). This component is compatible with version 2.0 of the .NET framework and higher, so it is suitable for use on older versions of Windows, such as Windows XP. The control is called Text File Edit (TFEdit for .NET) and comes as a single DLL (Dynamic Link Library), TextFileEdit.dll. It has several useful properties including turning the display of tabs and spaces on and off, EOL (end-of-line) marker support, showing line numbers and vertical and horizontal rulers. The component is from an Open Source project so all the code is available to change and improve. It comes with a small demonstration project to test it out, here is the component in action in the demo project:

.NET Text Editor

Background

A while back (several years ago) a C# Windows project required a simple text editor to allow users of a program to edit text based files and log files without needing to switch to another application (such as Notepad or Word). The editing capability was a small part of the requirements. At the time the use of the .NET text box did not provide enough functionality, the Rich Text Format control was not suitable, all commercial editors were two complex and added costs to the project. The answer was to extract the text editor portion of a Open Source IDE (Integrated Development Environement) project called SharpDevelop. The SharpDevelop component was called ICSharpCode.TextEditor.dll. This component was refactored to simplify the namespace to TextFileEdit, and a simple test project was written. This was then published on a previous web site where it sat for a few years before the web site was repurposed. With the release of a new version of Visual Studio Express (verison 2012) it was decided to see how well it handled older controls. So the Text File Edit DLL was dusted down and given a go.

Download and Run the Example C# Text Editor

You can download the Text File Edit DLL, source code and a demo test project using the TFEdit.zip file. Extract the contents to a folder on your computer. If you are using Visual Studio Express create a folder, e.g. call it TFEdit, in the Visual Studio Projects directory. Projects is normally in the Visual Studio directory (Visual Studio 2010 or Visual Studio 2012 folders) in My Documents. Open the solution (T_F_E_Solution.sln), the solution was created in Visual Studio 2008 so you will see a conversion report for later editions of Visual Studio. Set the TFETest project as the start-up (select Set as StartUp Project from the context menu). The project should run.

Use the Text File Edit Control in Your Projects

Text File Edit Added to ToolboxThe TextFileEdit.dll is in TextFileEdit\Project\bin\Release. With Visual Studio running start (or open) a Windows Forms Application project. Open the Toolbox (use the View menu if neccessary) and drag and drop the TextFileEdit.dll onto a tab, e.g. the General tab or create a new tab to hold the control. With a WinForm open in design mode drag the TextEditorControl from the Toolbox onto the form. It can then be positioned and properities set.

Text File Edit in Use

The ZIP file that contains the source code has more detailed instructions on adding TFEdit to a project, although it was written sometime ago for Visual Studio 2008.

Text File Edit .NET Control Properties

The TFEdit control supports the following properties:

  • AllowCaretBeyondEOL – When set to “true” the caret (typing cursor) can be moved beyond the end of the actual line. When it goes beyond the last character it is in a screen position and not in any text. Pressing the Return (Enter) key fills the line with spaces up to the point the caret has been moved and creates a new line.
  • BracketMatchingStyle – When set to “After” pairs of brackets, () or [] or {}, are highlighted when the caret moves past them. When set to “Before” the brackets are highlighted as the caret approaches them.
  • ConvertTabsToSpaces – When set to “true and a tab is typed, the tab character is not placed into the text. Instead spaces are placed into the text, the number of spaces being equal to the “TabIndent” property.
  • HideMouseCursor – When set to “true” the mouse cursor his hidden during typing.
  • IndentStyle – When set to “Auto” the text on the next line will start at the same postion as the current text. The same number of tabs and spaces are added to the new line that preceed the current line. When set to “None” no indentation occurs.
  • IsIconBarVisible – Shows a vertical bar on the lef hand side on to which icons can be programmatically placed on a line by line basis.
  • LineViewerStyle – When set to “FullRow” the line that has the focus for typing is highlighted. When set to “None” no highlight is present on the current line.
  • ShowEOLMarkers – When set to “true” a light marker (pilcrow character) is shown to indicate where the end of the line (carriage-return and line-feed character pair) will occur in the text file.
  • ShowHRuler – When set to “true” a “ruler” is drawn at the top of the text, within the control bounds, to give a visual indication of the columns.
  • ShowInvalidLines – When set to “true” the space below the last line will have a marker to show the additional lines that can fit into the area. This markers just indicate the position of additonal lines and are not lines present in the file to be saved.
  • ShowLineNumbers – When set to “true” line numbers appear in a margin at the left of the control within the control boundaries.
  • ShowSpaces – When set to “true” a light dot will indicate a “space” character.
  • ShowTabs – When set to “true” a tab character in the text is indicated by a light right pointing double angle character.
  • ShowVRuler – When set to “true” a vertical line is drawn down the control. It will be positioned at the “text column” indicated by the property “VRulerRow”.The vertical line can be used as a visual guide to determine the length of lines. Sometimes it is necessary to limit the length of lines in a text file for some systems, usually to 80 or 120 characters.
  • TabIndent – This property determines the number of character columns to indent when a tab is pressed or a tab is present in the text.
  • VRulerRow – This property determines the character column in which the vertical ruler line will be displayed.

Issues

When editing source code files the highlighting of paired brackets does not always occur.

Future Releases

There are no plans to release future versions of this control. The source code is available under the LGPL licence for you to make any required changes. The code was taken from an early version of the SharpDevelop IDE. Whilst the text editor in SharpDevelop has changed several times over various SharpDevelop releases TFEdit has not. The current SharpDevelop text editor is a WPF (Windows Presentation Foundation) component called AvalonEdit, it can be loaded into a WPF solution using the Manage NuGet Packages option from the Library Package Manager of the Tools menu, search for AvalonEdit. (To install NuGet select Extensions and Updates from the Tools menu in Visual Studio Express 2012. Select Online in the left hand panel and then NuGet Package Manager, download and install.)

Summary

TFEdit for .NET is a text editor component for WinForms applications. It was used to see if the new 2012 version of Visual Studio Express worked seamlessly with older components. Happily it worked fine for this control. TFEdit is compiled against version 2.0 of the .NET framework so will run on older Windows platforms. In theory it will run across several Operating Systems using Mono but that has not been tried. A compiled DLL, source code and demo program is available in the TFEdit.zip file. It is licensed under LGPL.

Visual Studio Express Requirements – Operating System

Visual Studio Express is the Free Version of Microsoft’s IDE

When writing software for Microsoft Windows based systems there are many free programming tools available, including the free Windows development tools provided by Microsoft themselves. Visual Studio is the Integrated Development Environment (IDE) that Microsoft produces for programmers, a.k.a. developers, to write software for Windows. The free version of these development tools is called Visual Studio Express (VSE), the latest version is the 2013 release. The 2013 release coincides with the release of Windows 8.1 and replaces Visual Studio Express 2012.

The 2013 versions are aimed at users of the latest Windows operating systems, Windows 7, Windows 8, Windows 8.1, Windows Server 2008 and Windows Server 2012. For users still running Windows XP or Windows Vista (not including Starter Editions) then Visual Studio Express 2010 will be needed. There are several different versions of VSE 2013 for programmers, which target different uses. The Visual Studio Express requirements vary depending upon which version is used. The versions are for Web, Windows 8 Apps, Windows Desktop or Windows Phone. To move beyond those types of programs the Professional version and upwards of Visual Studio would be required, and these are paid for products. The Microsoft Visual Studio 2013 Express products can be accessed from this link.

Visual Studio Express 2013 System Requirements
Express 2013 Product Used To Develop Minimum Operating System Required for Installation
Express for Web Web sites and HTML5 applications Windows 7 SP1 (With IE 10)
Express for Windows Windows 8 Apps Windows 8.1
Express for Windows Desktop Windows Programs Windows 7 SP1 (With IE 10)
Express for Windows Phone Windows Phone Apps Windows 8

All current versions of VSE are available from this downloads page (scroll down to the Express products, just after the full product trial versions). There is also a version of Team Foundation Server (TFS), Microsoft’s source code and development management software. TFS Express is limited to 5 users (team members). VSE products require free registration to obtain a product key to use them beyond a 30 day trial period.

What is .NET?

Windows Developers Usually Use Microsoft .NET When Writing Programs

If you are new to writing software for Windows personal computers (PCs) it is not long before you come across Microsoft .NET (pronounced “dot net”). What is .NET? The simplest answer is to say that .NET is used to build and run programs on a computer. (For the complete beginner who knows nothing about computer programs read our article What is Computer Programming?) In this post we attempt to provide a brief explanation to .NET in they way of an introduction. Microsoft .NET is an important Windows technology to ease the development of computer programs.

.NET Provides a Framework to Help Write Complex Programs

Steel Building FrameworkWhenever you see a building, such as an office block or skyscraper, being constructed you will notice that a steel frame is used to provide the basic structure. On to this frame is fixed walls, windows, floors, ceilings and everything else that makes up a building. The framework is made of steel beams, rivets and welds. Everything that is attached to it is made from common building materials and components. It is the architects and builders that determine the final look of the building using standard components.

A similar thing occurs when computer programs are written, the software developer (a.k.a. computer programmer) decides the final functionality and look of a program but will use an existing set of components to help achieve the end requirement.

Microsoft .NET Libraries and Runtime Engine

Microsoft .NET is a computer framework that contains hundreds of useful components. The components are gathered together into libraries to organize them into categories. Libraries that contain components to draw on the screen, read input, talk to the Internet, etc. This allows the programmer to concentrate on producing the functionality of the software and not have to work on the code that controls the hardware. Modern software programs are written with a tool called an Integrated Development Environment (IDE). An IDE allows the programmer to write the code in an editor, run the code to test it and use a debugger to fix any errors found during testing. Many IDEs come with a set of common existing components to use, such as text boxes, buttons and image containers.

Visual Studio IDE

An Intergrated Development Environment

With the .NET framework and an IDE it only takes a few lines of code to get a simple program working. The speed of software development using .NET makes it a very useful framework for developers writing Windows programs. Continue reading