Home > Microsoft .Net Development Tips > Application Testing and Security > Generate RSA public and private keys, export to XML
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

APPLICATION TESTING AND SECURITY

Generate RSA public and private keys, export to XML


Stefan Prodan
05.21.2007
Rating: --- (out of 5)


.NET Essentials Channel
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


The following method shows how RSA keys can be saved to disk as an XML file. The XML files can then be used to make an RSA secure channel -- the public key is used for encryption and the private one for decryption.
/// <summary>
/// Generates 2 XML files (public and private key) 
/// </summary> 
/// <param name="privateKeyPath">RSA private key file path</param> 
/// <param name="publicKeyPath">RSA private key file path</param> /
// <param name="size">secure size must be above 512</param> 
public static void GenerateRsa(string privateKeyPath, string publicKeyPath, int size) 
{
    //stream to save the keys
    FileStream fs = null;
    StreamWriter sw = null;

    //create RSA provider
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(size);
    try
    {
        //save private key
        fs = new FileStream(privateKeyPath, FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(fs);
        sw.Write(rsa.ToXmlString(true));
        sw.Flush();
    }
    finally
    {
        if (sw != null) sw.Close();
        if (fs != null) fs.Close();
    }

    try
    {
        //save public key
        fs = new FileStream(publicKeyPath, FileMode.Create, FileAccess.Write);
        sw = new StreamWriter(fs);
        sw.Write(rsa.ToXmlString(false));
        sw.Flush();
    }
    finally
    {
        if (sw != null) sw.Close();
        if (fs != null) fs.Close();
    }
    rsa.Clear();
}

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
Application Testing and Security
Advanced Windows Debugging Book Chapter and Podcast
Book excerpt: Advanced Windows Debugging
Book excerpt: Pragmatic unit testing in C# with NUnit
Security interoperability with .NET/WSE and WebLogic Workshop 8.1
How to avoid regression bugs while adding new features
NDepends: How you look at code
Ten ways to unit test your .NET code
On ASP.NET AJAX testing and debugging tools
Beginning Windows CardSpace development
Need Web services security? Dig into WSE 3.0 for Microsoft .NET

C# Development
Picking a .NET smart client communications technology
LINQ beyond queries: Strong-typed refection
Book excerpt: An introduction to DSL tools
Assembly versioning in the .NET Framework 2.0
Book excerpt: Creating graphical output using the .NET Compact Framework
On Extension Methods in C# and .NET Framework 3.5
Book excerpt: Upgrading to Visual Studio 2005
Learning .NET: Tips for getting started with .NET development
Book excerpt: Code snippets in Visual Studio 2005
Sophisticated math add-in for Visual Studio 2005

.NET Framework security best practices
Podcast: Windows CardSpace authors speak
Book excerpt: Java EE and .NET security interoperability
Book excerpt: Advanced Windows Debugging
Book excerpt: Pragmatic unit testing in C# with NUnit
Security interoperability with .NET/WSE and WebLogic Workshop 8.1
Windows Developments: Product news, December 2007
How to avoid regression bugs while adding new features
VB code: New additions, November 2007
VB code: Application security downloads
Learning .NET: Application development resources for the .NET Framework

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
common test platform  (SearchWinDevelopment.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2000 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts