|
security
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
RSACryptoServiceProvider functioning differently in 2005 (vs.2003)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)) I don't understand this line
>rsa.Decrypt(Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(bEncryptedStrAsByt)), why don't you simply pass bEncryptedStrAsByt to the decrypt method?False) 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)) 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)) > > >
How to troubleshoot 401 error when connecting using NetworkCredent
.NET app on a shared directory. if I encrypt key data why do I want or need SSL? ClickOnce and remembering permissions granted Strange problem with X509Certificate2 on Windows 2003 Security issue running unmanaged code in a win form ctrl hosted in SecurityPermission problem How to encrypt a string with ProtectedData (.NET 2.0) JavaScience CD versus book Trying to grant full trust..... (.NET 2.0) |
|||||||||||||||||||||||