Rsa Conversion Failing Help

P

Peter

I wrote this rsa proceedure that works great:
Dim xmlPublicKey As String =
"<RSAKeyValue><Modulus>qZphGJhaxkcSkQiKbzbp4PqWgC0VsFthijbZ1tioCCayBH+7QUwiOzzC9AAu/cTyemxf2YYXA9ozbafsPjvV2T7dOEVVs3bRooKbSPTwxQWwny5pgexOPABBf7vGmtq9r2/mMWnCGMXRGscW2UtV/FFfb1ga6yC9ovGEUM8kpvU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
Dim rsa As New RSACryptoServiceProvider
rsa.FromXmlString(xmlPublicKey)
Dim message As String = "Entcrypt this data"
Dim plainTextinBytes As Byte()

plainTextinBytes = (New UnicodeEncoding).GetBytes(message)
Try
plainTextinBytes = rsa.Encrypt(plainTextinBytes, False)

Catch ex As CryptographicException
MsgBox(ex.ToString)
End Try

For i As Integer = 0 To plainTextinBytes.Length - 1
txtReg.Text += (Chr(plainTextinBytes(i)))

Me.Refresh()
Next

And I can decrypt this message from within the same proceedure with no
issues:
rsa.FromXmlString("<RSAKeyValue><Modulus>qZphGJhaxkcSkQiKbzbp4PqWgC0VsFthijbZ1tioCCayBH+7QUwiOzzC9AAu/cTyemxf2YYXA9ozbafsPjvV2T7dOEVVs3bRooKbSPTwxQWwny5pgexOPABBf7vGmtq9r2/mMWnCGMXRGscW2UtV/FFfb1ga6yC9ovGEUM8kpvU=</Modulus><Exponent>AQAB</Exponent><P>2LLhPL5KkAVVkJpk9Z2aXiuaRkftfdWpKzwPoJ1XywWvQagZj63scCUlxV/DDU6alG7GEduMK6XFqPJWwDhNCw==</P><Q>yFzj0cjy71Cv+Knpq09jmw7m4yLu6TdbmYxyqce10Jg3Kyff7E8iE
KqDANvhUE2rLKMpIla5f2ZP0G3JSdb/w==</Q><DP>y+BIT1kNV8DdBuZr1DYKVU0aocLsazaJKbchUNCYF9fqUpBwNCDDUaNxjOgS4EMy5jHuQkDX+PzHIbOW8NRHjw==</DP><DQ>vOnYZBRz+fC5+ls6Vquj3q9OQxrM6Feppj0mX65NX0AlxCr0lnqGRFK0wl7650l1o581vG1Fp1RVCa+MXxudQQ==</DQ><InverseQ>aRzkx6cn4aGwHQ1l5qadZFgrCJr43dzEAGZm7Uo+8+cb8uN12RxoPWsk0pmwaprZhIapAo/tqWtJ1SnyN7iJ2Q==</InverseQ><D>Q43yTtGglomIo6w7N8EvtyUY/UT39p7vYxYl/qWdiGGhVplqBMBGgkHZ
UbtcLcZvhmctA/s6uPmyC6ysrcyngeE5fdWlTZWTlgihOMh/g7AnYRvyY4iQ9W0g3691HzoNDC+kNhsO4LHbEwhmq7OgCigFNd0oHlDhQYQqhZjGKE=</D></RSAKeyValue>")
'plainTextinBytes = (New
UnicodeEncoding).GetBytes(txtReg.Text)
Dim decrytion As Byte() = rsa.Decrypt(plainTextinBytes, False)
For i As Integer = 0 To (decrytion.Length - 1) Step 2
Console.Write(Chr(decrytion(i)))
Next

But I can not get this message to decrypt in another program. If all
I have is the private key and the encrypted message how should my
decrypt proceedure look?

-Peter
 
P

Peter

I re-read this post and wanted to clarify... I'm making a mistake on
the decrypting...

I've encrypted the contents of a textbox using RSA encryption with no
issues but when I try to decode the message in another application I'm
getting a 'bad data' error. Here is the the general proceedure I'm
using:
VB.net

'This is how I encoded:
Dim xmlPublicKey As String = "MyKeyPublic" 'Fake key for illustration
purposes
Dim rsa As New RSACryptoServiceProvider
rsa.FromXmlString(xmlPublicKey)
Dim message As String = txtSerial.Text
Dim plainTextinBytes As Byte()
plainTextinBytes = (New UnicodeEncoding).GetBytes(message)

'I removed error handling to illustrate point
plainTextinBytes = rsa.Encrypt(plainTextinBytes, False)

For i As Integer = 0 To plainTextinBytes.Length - 1
txtReg.Text += (Chr(plainTextinBytes(i))) 'Encrypted data
Me.Refresh()
Next
Dim MyStringToDecrypt as string = txtReg.Text

Now I take MyStringToDecrypt at this point and save it to a text file.
On another computer I try to decode this message using this code:

Dim rsa As New RSACryptoServiceProvider
Dim plainTextInBytes As Byte()
Dim xmlKeys As String
Dim DecryptedSerial As String

rsa.FromXmlString("MyPrivateKLeyData")
'RegText holds the data encrypted with the public key
Dim Z() As Byte
For i As Integer = 0 To txtReg.Text.Length - 1
ReDim Preserve Z(i)
Z(i) = CType(Asc(Mid(txtReg.Text, i + 1, 1)), Byte)
Next
plainTextInBytes = Z

Dim decrytion As Byte() = rsa.Decrypt(plainTextInBytes,
False)
For i As Integer = 0 To (decrytion.Length - 1) Step 2
Console.Write(Chr(decrytion(i)))
Next

What am I doing wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top