'Key does not exist.' 签约时

'Key does not exist.' when signing

我正在做一个签名和验证签名的测试,而不是签名和发送它只是抛出一个错误,这里是两个签名代码(不包括服务器验证,因为它不相关且太长)和密钥交换代码。

这是服务器:

            // variables
            byte[] bytesFrom = new byte[8192];
            // initialize network stream
            NetworkStream networkStream = clientSocket.GetStream();
            // send length of public key
            networkStream.Write(Encoding.UTF8.GetBytes(Convert.ToBase64String(rsa.ExportRSAPublicKey()).Length.ToString()));
            // send public key
            networkStream.Write(Encoding.UTF8.GetBytes(Convert.ToBase64String(rsa.ExportRSAPublicKey())));
            // read public key length
            networkStream.Read(bytesFrom);
            // apply public key length
            bytesFrom = new byte[int.Parse(Encoding.UTF8.GetString(bytesFrom))];
            // read public key
            networkStream.Read(bytesFrom);
            // import public key
            int a;
            rsa.ImportRSAPublicKey(rsa.Decrypt(bytesFrom, RSAEncryptionPadding.OaepSHA512), out a);

这是客户:

                // initialize RSA and connect
                RSA rsa = RSA.Create();
                IPEndPoint end = new IPEndPoint(IPAddress.Loopback, 500);
                Socket sock = new Socket(end.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                sock.Connect(end);
                NetworkStream ns = new NetworkStream(sock);
                byte[] buffer = new byte[8192];
                // read public key length
                ns.Read(buffer);
                // apply public key length
                buffer = new byte[int.Parse(Encoding.UTF8.GetString(buffer))];
                // read public key
                ns.Read(buffer);
                // apply public key
                int a;
                rsa.ImportRSAPublicKey(buffer, out a);
                // send public key length and public key 
                ns.Write(Encoding.UTF8.GetBytes(rsa.ExportRSAPublicKey().Length.ToString()));
                ns.Write(rsa.ExportRSAPublicKey());
                // send signed "h"
                ns.Write(Encoding.UTF8.GetBytes(rsa.SignData(Encoding.UTF8.GetBytes("h"), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1).Length.ToString()));
                ns.Write(rsa.SignData(Encoding.UTF8.GetBytes("h"), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));

我发现我只需要在切换密钥时临时转储私钥,然后最后将其导入回来。