CryptoStream无法正确解密

时间:2013-09-13 15:04:16

标签: c# cryptography aes cryptostream

我在服务器应用程序和客户端之间有一个有效的TCP连接,用AES加密通信。它适用于具有RijndaelManaged En / Decrypter的CryptoStream。那么现在,如果我想发送某事。像这样:“F4FBF-B60FC-6F3ED-1E5A9”我得到了输出:“Ü]Þí£ôÉ×â×$ÚÚD-1E5A9”所以......这里可能出现什么问题?为什么还要加密到“D-1E5A9”?

这里有一些代码: 服务器:

while (true)
{   
    Console.Write("Waiting for a connection... ");

    TcpClient client = server.AcceptTcpClient();
    Console.WriteLine("Connected!");

    NetworkStream stream = client.GetStream();
    stream.ReadTimeout = System.Threading.Timeout.Infinite;
    stream.WriteTimeout = System.Threading.Timeout.Infinite;

    byte[] KeyBuffer = new Byte[1510];
    stream.Read(KeyBuffer, 0, KeyBuffer.Length);
    stream.Flush();
    RSACryptoServiceProvider RSAC = new RSACryptoServiceProvider(4096);
    RSAC.FromXmlString(bla.GetString(KeyBuffer));

    RijndaelManaged RM = new RijndaelManaged();

    byte[] RMKeyBuffer = RSAC.Encrypt(RM.Key, true);
    stream.Write(RMKeyBuffer, 0, RMKeyBuffer.Length);
    stream.Flush();
    CryptoStream cs = new CryptoStream(stream, RM.CreateDecryptor(), CryptoStreamMode.Read);
    byte[] licence = new byte[23];
    cs.Read(licence, 0, licence.Length);
    cs.Dispose();

    string myConnectionString = "SERVER=Server;" +
    "DATABASE=Database;" +
    "UID=UID;" +
    "PASSWORD=Password;";

    MySqlConnection mysqlConn = new MySqlConnection(myConnectionString);
    MySqlCommand myComm = mysqlConn.CreateCommand();
    myComm.CommandText = "SELET * From lizenzen WHERE Lizenznummer='"+bla.GetString(licence)+"'";
    mysqlConn.Open();
}

客户:

private void DoTheThing(string Lizenz)
       { 
        textBox2.Text = "Passed Beginning";
                Encoding bla = Encoding.BigEndianUnicode;
                Int32 port = 14000;
                TcpClient client = new TcpClient("192.168.1.177", port);
                NetworkStream stream = client.GetStream();
                stream.ReadTimeout = System.Threading.Timeout.Infinite;
                stream.WriteTimeout = System.Threading.Timeout.Infinite;
                textBox2.Text = "Passed Socket Creation";

                RSACryptoServiceProvider RSAC = new RSACryptoServiceProvider(4096);
                byte[] Key = bla.GetBytes(RSAC.ToXmlString(false));
                stream.Write(Key, 0, Key.Length);
                textBox2.Text = "Passed sending KEy";

                byte[] encryAES = new byte[512];
                stream.Read(encryAES, 0, encryAES.Length);
                textBox2.Text = "Passed receiving Encrypted AES Key";

                byte[] decryAES = RSAC.Decrypt(encryAES, true);
                string decryAesString = string.Empty;
                textBox2.Text = "Passed Decryption";

                foreach (byte item in decryAES)
                {
                    decryAesString += ((int)item).ToString();
                }
                textBox1.Text = decryAesString;

                RijndaelManaged RM = new RijndaelManaged();
                RM.Key = decryAES;
                CryptoStream csS = new CryptoStream(stream, RM.CreateEncryptor(), CryptoStreamMode.Write);
                byte[] Licence = bla.GetBytes(Lizenz);
                csS.Write(Licence,0,Licence.Length);
                csS.Flush();
                csS.Dispose();

                CryptoStream csR = new CryptoStream(stream, RM.CreateDecryptor(), CryptoStreamMode.Read);
                byte[] Data = new byte[4096];
                csR.Read(Data, 0, Data.Length);
                csR.Flush();
                csR.Dispose();

                textBox3.Text = bla.GetString(Data);
}

有时候,我将开始使用“使用”来摆脱冲洗和处置,但是现在,我只是试图让它工作并且经过漫长的一天工作RSA加密以使其工作,现在我遇到了这个问题。如果你想知道什么是'bla',那就想象你想要什么,它没有什么区别。

0 个答案:

没有答案