EXP Bad data in RSA client server app

Oct 15, 2013 at 1:29 AM
I am receiving a CryptographicException stating "Bad Data" when attempting to decrypt data sent across the network encrypted using RSACryptoServiceProvider.

I receive no exceptions client side when attempting to send the data.


public byte[] ENC(string strdata, CspParameters prk)
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(prk);
byte[] data = rsa.Encrypt(Encoding.Unicode.GetBytes(strdata),false);
return data;

void clint(byte[] data)
    object op = (object)data;
    BinaryFormatter br = new BinaryFormatter();
    TcpClient myclient = new TcpClient("", 7000);
    NetworkStream myns = myclient.GetStream();
    br.Serialize(myns, op);
catch (Exception e)

CspParameters cp = new CspParameters();
cp.KeyContainerName = "25522100";
string usb = ":RIO";
string Name = System.Environment.MachineName;
Name += usb;
mymsg = ENC(Name, cp);

TcpListener mytcp = new TcpListener(7000);
TcpSocket mysocket = mytcp.AcceptSocket();
NetworkStream myns = new NetworkStream(mysocket);
BinaryFormatter br = new BinaryFormatter();
object op;
op = br.Deserialize(myns);
byte[] data = (byte[])op;
CspParameters cp = new CspParameters();
cp.KeyContainerName = "25522100";
byte[] messge = DEC(data, cp);

public byte[] DEC(byte[] en_data, CspParameters key)
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(key);
byte[] data1 = rsa.Decrypt(en_data, false);
return data1;
Why am I receiving this CryptographicException when attempting to decrypt the received data?