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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RSA Encrypt: Key not valid for use in specified state.

Status
Not open for further replies.

cjelec

Programmer
Jan 5, 2007
491
GB
Hi,

I'm getting the dreaded "Key not valid for use in specified state." with the RSACryptoServiceProvider (vb.net 2008) when trying to encrypt a byte block of under 245 bytes using a public key loaded from an xml string.

I have searched everywhere for this, but the only answers I can find relate to the length of the data being encrypted which isn't the problem here because the length of the data I'm trying to encrypt is only 193 bytes.

I have checked the public key being used with the original key and they are exactly the same.

I would post my code but its dotted around quite a few classes. Its something like the following:

Code:
'Generate key
Dim xmlpublickey As String
Dim rsa As New RSACryptoServiceProvider

xmlpublickey = rsa.ToXmlString(False)


Then Later on, in the sending class
Code:
Dim xmlpublickey As String = getKey() 'this key does match exactly (also in length)
Dim rsa As New RSACryptoServiceProvider

rsa.FromXmlString(xmlpublickey)

Return rsa.Encrypt(data, False) 'Error here

Can anyone help?

Thanks loads in advance,

Chris
 
I have done some tests and found that the encryption will only work if I use the private key (rsa.ToXmlString(True)) which defeats the object of using public/private key encryption...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top