【问题标题】:'Key does not exist.' when signing“钥匙不存在。”签约时
【发布时间】:2021-08-28 12:38:36
【问题描述】:

我正在对签名和验证签名进行测试,而不是签名和发送它只会引发错误,这里是签名代码(不包括服务器验证,因为它不相关且太长)和密钥交换代码。

这里是服务器:

            // 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));

【问题讨论】:

    标签: c# network-programming rsa


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      相关资源
      最近更新 更多