Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
3.25/5 (4 votes)
See more:
Pls can anyone give me a source code for rsa algoritm in c# which encrypt & decrypt a file through code.means using StreamWriter function.that function which will give the file through code & create two files one is encrypted & other is decrypted file.
Pavel Yermalovich 25-Mar-11 4:49am
Pavel Yermalovich 25-Mar-11 4:50am    
Sandeep Mewara 25-Mar-11 5:32am    
no effort.
Sergey Alexandrovich Kryukov 25-Mar-11 12:52pm    
Why do you think you need source code (not just code like Pavel shows)?

RSA encryption but just for strings:

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace RSA
    class Program
        static void Main(string[] args)

            RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();//Encode String to Convert to Bytes

            string data = "RsA EnCryPTion is cool!!!";//whatever you want to encrypt
            Byte[] newdata = encoding.GetBytes(data);//convert to Bytes
            Byte[] encrypted = myrsa.Encrypt(newdata, false);

            Console.WriteLine("Encrypted Data:  ");
            for (int i = 0; i < encrypted.Length; i++)
                Console.Write("{0} ", encrypted[i]);

            Byte[] decrypted = myrsa.Decrypt(encrypted, false);//decrypt 
            Console.WriteLine("Decrypted Data:  ");

            string dData = encoding.GetString(decrypted); //encode bytes back to string 
            for (int i = 0; i < decrypted.Length; i++)
                Console.Write("{0}", dData[i]);

Share this answer
This is a common requirement and there are, therefore, thousands of articles/blogs on the internet.

All you have to do to find them is to think of a suitable search phrase and have access to a search engine.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900