Home All Groups Group Topic Archive Search About

RSACryptoServiceProvider functioning differently in 2005 (vs.2003)

Author
29 Mar 2006 8:48 PM
mfroman
The following code works fine in VS2003 (.Net 1.1).  However, in VS2005 (.Net
2.0) it throws a "Bad Data" exception on the decrypt line!

(Although this example may seem pointless, it is just the pertinant lines
from larger source code. It was consolidated to highlight the error).

Is there a new implementation of the RSACryptoServiceProvider in .NET 2.0,
or is my encoding.unidcode conversions not working in 2.0?

Any thoughts would be appreciated.
Thanks,
Michael


        ' Create defaults
        Dim rsa As New RSACryptoServiceProvider
        Dim sPlainText As String = "password"
        ' Pass it to a byte string (w/ padding)
        Dim bEncryptedStrAsByt() As Byte =
rsa.Encrypt(Encoding.Unicode.GetBytes(sPlainText), False)
        ' Decrpyt byte-string using rsa
        Dim bDecryptedStrAsByt() As Byte =
rsa.Decrypt(Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(bEncryptedStrAsByt)), False)
        ' Finally, convert to a string
        Console.WriteLine(Encoding.Unicode.GetString(bDecryptedStrAsByt))

Author
29 Mar 2006 9:07 PM
Dominick Baier [DevelopMentor]
I don't understand this line

>rsa.Decrypt(Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(bEncryptedStrAsByt)),
False)

why don't you simply pass bEncryptedStrAsByt to the decrypt method?

afterwards convert it to a string using GetString().


---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com

Show quoteHide quote
> The following code works fine in VS2003 (.Net 1.1).  However, in
> VS2005 (.Net 2.0) it throws a "Bad Data" exception on the decrypt
> line!
>
> (Although this example may seem pointless, it is just the pertinant
> lines from larger source code. It was consolidated to highlight the
> error).
>
> Is there a new implementation of the RSACryptoServiceProvider in .NET
> 2.0, or is my encoding.unidcode conversions not working in 2.0?
>
> Any thoughts would be appreciated.
> Thanks,
> Michael
> ' Create defaults
> Dim rsa As New RSACryptoServiceProvider
> Dim sPlainText As String = "password"
> ' Pass it to a byte string (w/ padding)
> Dim bEncryptedStrAsByt() As Byte =
> rsa.Encrypt(Encoding.Unicode.GetBytes(sPlainText), False)
> ' Decrpyt byte-string using rsa
> Dim bDecryptedStrAsByt() As Byte =
> rsa.Decrypt(Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(bEncr
> yptedStrAsByt)), False)
> ' Finally, convert to a string
>
> Console.WriteLine(Encoding.Unicode.GetString(bDecryptedStrAsByt))
Author
29 Mar 2006 9:42 PM
mfroman
Yes, your solution works.  

The reason for the additional code was due to an article I was reading that
mentioned "explicitly stating the code page (encoding.unicode) prevents
non-ASCII character gibberish if it is decoded on another machine with a
different system locale."  For example, I wanted to make sure È or É were not
mapped to "E" by windows if these characters could not be found in the
default code page.

I'll admit that I am not well versed on this topic, and the code I provided
worked well for 2 years in .NET 1.1, so I was confused when .NET 2.0 threw
the exception?  (I attempted .convert.ToBase64String as well to explicitly
convert the scrambled bytes back into a string (from which it originated)
before converting back to a byte array for the decrypt method).  This does
seem like overkill, in hindsight.

Thanks for you response. I guess I am still curious as to the 1.1/2.0
difference and if I do not need to worry about the text mapping.

Michael



Show quoteHide quote
"Dominick Baier [DevelopMentor]" wrote:

> I don't understand this line
>
> >rsa.Decrypt(Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(bEncryptedStrAsByt)),
> False)
>
> why don't you simply pass bEncryptedStrAsByt to the decrypt method?
>
> afterwards convert it to a string using GetString().
>
>
> ---------------------------------------
> Dominick Baier - DevelopMentor
> http://www.leastprivilege.com
>
> > The following code works fine in VS2003 (.Net 1.1).  However, in
> > VS2005 (.Net 2.0) it throws a "Bad Data" exception on the decrypt
> > line!
> >
> > (Although this example may seem pointless, it is just the pertinant
> > lines from larger source code. It was consolidated to highlight the
> > error).
> >
> > Is there a new implementation of the RSACryptoServiceProvider in .NET
> > 2.0, or is my encoding.unidcode conversions not working in 2.0?
> >
> > Any thoughts would be appreciated.
> > Thanks,
> > Michael
> > ' Create defaults
> > Dim rsa As New RSACryptoServiceProvider
> > Dim sPlainText As String = "password"
> > ' Pass it to a byte string (w/ padding)
> > Dim bEncryptedStrAsByt() As Byte =
> > rsa.Encrypt(Encoding.Unicode.GetBytes(sPlainText), False)
> > ' Decrpyt byte-string using rsa
> > Dim bDecryptedStrAsByt() As Byte =
> > rsa.Decrypt(Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(bEncr
> > yptedStrAsByt)), False)
> > ' Finally, convert to a string
> >
> > Console.WriteLine(Encoding.Unicode.GetString(bDecryptedStrAsByt))
>
>
>