By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,506 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,506 IT Pros & Developers. It's quick & easy.

Rsa Conversion Failing Help

P: n/a
I wrote this rsa proceedure that works great:
Dim xmlPublicKey As String =
"<RSAKeyValue><Modulus>qZphGJhaxkcSkQiKbzbp4PqWgC0 VsFthijbZ1tioCCayBH+7QUwiOzzC9AAu/cTyemxf2YYXA9ozbafsPjvV2T7dOEVVs3bRooKbSPTwxQWwny5 pgexOPABBf7vGmtq9r2/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>qZphGJhax kcSkQiKbzbp4PqWgC0VsFthijbZ1tioCCayBH+7QUwiOzzC9AA u/cTyemxf2YYXA9ozbafsPjvV2T7dOEVVs3bRooKbSPTwxQWwny5 pgexOPABBf7vGmtq9r2/mMWnCGMXRGscW2UtV/FFfb1ga6yC9ovGEUM8kpvU=</Modulus><Exponent>AQAB</Exponent><P>2LLhPL5KkAVVkJpk9Z2aXiuaRkftfdWpKzwPoJ 1XywWvQagZj63scCUlxV/DDU6alG7GEduMK6XFqPJWwDhNCw==</P><Q>yFzj0cjy71Cv+Knpq09jmw7m4yLu6TdbmYxyqce10Jg3K yff7E8iE
KqDANvhUE2rLKMpIla5f2ZP0G3JSdb/w==</Q><DP>y+BIT1kNV8DdBuZr1DYKVU0aocLsazaJKbchUNCYF9fq UpBwNCDDUaNxjOgS4EMy5jHuQkDX+PzHIbOW8NRHjw==</DP><DQ>vOnYZBRz+fC5+ls6Vquj3q9OQxrM6Feppj0mX65NX0A lxCr0lnqGRFK0wl7650l1o581vG1Fp1RVCa+MXxudQQ==</DQ><InverseQ>aRzkx6cn4aGwHQ1l5qadZFgrCJr43dzEAGZm7 Uo+8+cb8uN12RxoPWsk0pmwaprZhIapAo/tqWtJ1SnyN7iJ2Q==</InverseQ><D>Q43yTtGglomIo6w7N8EvtyUY/UT39p7vYxYl/qWdiGGhVplqBMBGgkHZ
UbtcLcZvhmctA/s6uPmyC6ysrcyngeE5fdWlTZWTlgihOMh/g7AnYRvyY4iQ9W0g3691HzoNDC+kNhsO4LHbEwhmq7OgCigFNd 0oHlDhQYQqhZjGKE=</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
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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?
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.