Click here to Skip to main content
14,640,935 members
Rate this:
Please Sign up or sign in to vote.
See more:
<how to="" get="" private="" kye="" for="" rsa="" algorithm="">
Updated 11-Apr-11 19:03pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

You have to use the private key to decrypt. You're using public key for encrypting and decrypting.
RSA is asymetric.
bipin9 7-Apr-11 1:30am
yes thats ok but how do i get that private key or what would be that key
Rate this:
Please Sign up or sign in to vote.

Solution 2

I set up a small example where all the magic happens in just one method.
I'm using 3 TextBoxes to show the path from plain text via encryption to the decrypted text.
TextBoxes are named textBoxPlain,textBoxEncrypted and textBoxDecrypted.
So you can quite easily open a new project and just put 3 textboxes on the form.
textBoxEncrypted is set to multiline. Encrypting and decrypting all happens in textBoxPlain_TextChanged

using System;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Collections;

namespace EncryptionRSA
    public partial class Form1 : Form
        private RSACryptoServiceProvider rsaSender;
        private RSACryptoServiceProvider rsaReceiver;
        private string publicKey;
        private string privateKey;

        private int keySize = 1024;

        public Form1()
            rsaReceiver = new RSACryptoServiceProvider(keySize);
            // public key which can be spread to all senders
            publicKey = rsaReceiver.ToXmlString(false);
            // private key. Keep it secret!
            // the private key XML always contains public and private key
            privateKey = rsaReceiver.ToXmlString(true);

        private void textBoxPlain_TextChanged(object sender, EventArgs e)
            // sender using the public key to encrypt data
            rsaSender = new RSACryptoServiceProvider();
            rsaSender.FromXmlString(publicKey);// "loading" public key
            // code from your sample
            byte[] bytes = Encoding.UTF32.GetBytes(textBoxPlain.Text);
            int maxLength = keySize/8 - 42;
            int dataLength = bytes.Length;
            int iterationsenc = dataLength / maxLength;
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i <= iterationsenc; i++)
                byte[] tempBytes = new byte[(dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i];
                Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length);
                byte[] encryptedBytes = rsaSender.Encrypt(tempBytes, true);

            // display encrypted data
            textBoxEncrypted.Text = stringBuilder.ToString();

            // receiver using his privat key
            rsaReceiver = new RSACryptoServiceProvider();

            // code from your sample
            int base64BlockSize = ((keySize / 8) % 3 != 0) ? (((keySize / 8) / 3) * 4) + 4 : ((keySize / 8) / 3) * 4;
            int iterationsdec = textBoxEncrypted.Text.Length / base64BlockSize;
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < iterationsdec; i++)
                byte[] encryptedBytes = Convert.FromBase64String(textBoxEncrypted.Text.Substring(base64BlockSize * i, base64BlockSize));
                arrayList.AddRange(rsaReceiver.Decrypt(encryptedBytes, true));

            // display decrypted data
            textBoxDecrypted.Text = Encoding.UTF32.GetString(arrayList.ToArray(Type.GetType("System.Byte")) as byte[]);
bipin9 7-Apr-11 9:23am
cool ha..
but what was the problem in my code
bipin9 7-Apr-11 9:26am
string generatedKey = Asymmetric.GenerateKey(1024, BATCrypto.KeyType.Public); Console.WriteLine("This is generated key :-- " + generatedKey); string rsaencrypted = Asymmetric.Encrypt(BATCrypto.AsymmetricAlgo.RSA, "bipin", 1024, generatedKey); Console.WriteLine("rsaencrypted is :- " + rsaencrypted); string rsaDecrypted = Asymmetric.Decrypt(BATCrypto.AsymmetricAlgo.RSA, rsaencrypted, 1024, generatedKey);.
Michel [mjbohn] 7-Apr-11 10:02am
OK after telling you that you're using the public key for both encrytion *and* decrytion and giving you a detailed example of how to use these keys, I thought it was obvious.
It was not. Might be my fault.
Look at this lines of code:
string generatedKey = Asymmetric.GenerateKey(1024, BATCrypto.KeyType.Public);
You build a PUBLIC key which you use for encrytion in this line:
string rsaencrypted = Asymmetric.Encrypt(BATCrypto.AsymmetricAlgo.RSA, "bipin", 1024, generatedKey);
Then you use the same PUBLIC key for decryption in this line:
string rsaDecrypted = Asymmetric.Decrypt(BATCrypto.AsymmetricAlgo.RSA, rsaencrypted, 1024, generatedKey);
For decryption you have to use the PRIVATE key which I guess get's build by BATCrypto.KeyType.Private
bipin9 8-Apr-11 1:06am
Asymmetric.GenerateKey(1024,BATCrypto.KeyType.Private); this giving exception as Safe handle has been closed.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100