Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Asymmetric encryption

Status
Not open for further replies.

Durkin

Programmer
Nov 6, 2000
304
GB
Hi. I'm pretty new to encryption(ie. about three days of scratching my head) so forgive me if I am missing something obvious. Also, I am writing it in VB so, if the syntax is someway different to C#, apologies. Anyway, I am using an RSACryptoServiceProvider to encrypt some data and then to decrypt it. I thought that if I used .ToXmlString(false) to export just the public parameters then I could use .FromXmlString to create another RSACryptoServiceProvider which could be used to encrypt only. Unfortunately, this is telling me 'Bad Key'. The only way I can get it to work is by sending out the private key as well which kind of defeats the purpose.
Basically, what I want to do is:
1. Generate a public/private key pair.
2. Send the public key to another machine.
3. Have the other machine encrypt the data with the public key and send it back.
4. Decrypt it using the private key.

As far as I can see, this is exactly what public key encryption is designed for so why isn't that the way these libraries were designed? Thanks in advance for any help.

Durkin
 
Could you post a bit of sample code for others who might have similar questions?

Thanks.
Chip H.
 
Sure. However, like I said it's in VB.
This routine takes the text out of one textbox, encrypts it then decrypts it and puts the result in another textbox. Not very useful but it has all that's required to encrypt/decrypt strings.
Code:
    Public Sub EncryptUnEncrypt()
        'This is the Decrypter
        Dim KeyGenerator As New RSACryptoServiceProvider()

        'This extracts the public key only
        Dim XmlPublicKey As String = KeyGenerator.ToXmlString(False)

        Dim Encryptor As New RSACryptoServiceProvider()
        Encryptor.FromXmlString(XmlPublicKey)


        Dim byteInput() As Byte = (New System.Text.UnicodeEncoding()).GetBytes(TextBox1.Text)

        Dim byteEncrypted() As Byte = Encryptor.Encrypt(byteInput, False)

        Dim byteDecryptedString() As Byte = KeyGenerator.Decrypt(byteEncrypted, False)

        TextBox3.Text = (New System.Text.UnicodeEncoding()).GetString(byteDecryptedString)


    End Sub

Durkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top