SubSpecs
SubSpecs

Reputation: 57

Encrypt RSA, encode to string. On other end encode back to bytes and Decrypt RSA

I've been having trouble encoding RSA Encrypted bytes to string, and then getting them back to bytes on the other end(Remote host), the other end throws an exception of 'Bad Data'.

I tried google, no luck.

My Code so far:

Client:

array<Byte>^Test=Encoding::ASCII->GetBytes(Input);
array<Byte>^TempByteText=RSA->Encrypt(Test,false);
String^ TempString = Encoding::ASCII->GetString(TempByteText);
return TempString;

Then I send TempString to Server. Anything else I send, any text or bytes of any sort arrive on the other end as intended, but except for RSA Encrypted bytes.

Server:

array<Byte>^Test=Encoding::ASCII->GetBytes(Input);
array<Byte>^TempByteText=RSA->Decrypt(Test,false);
return TempByteText;

Even if I return the functions locally, it's still says the same Bad Data. I have a 1024bit key, so the Encrypt size should be 128 bytes, which it IS, but it still say's 'Bad Data'.

Any help would be appreciated.

Upvotes: 1

Views: 456

Answers (1)

zaph
zaph

Reputation: 112855

Encrypted data is not going to be ASCII, most of the bytes will fall into unprintable region, you need to convert to another encoding such as Base64 or hexadecimal if you need the encrypted data in String format.

Upvotes: 1

Related Questions