【问题标题】:System.Security.Cryptography.Cng: The parameter is incorrectSystem.Security.Cryptography.Cng:参数不正确
【发布时间】:2022-10-25 20:25:02
【问题描述】:

我使用标准代码来加密这个文件。证书未过期且密钥有效。我找不到它引发此异常的原因。


        public byte[] EncryptDataOaepSha256(X509Certificate2 cert, byte[] data)
        {
            RSA rsa = cert.GetRSAPublicKey();

            if (rsa != null)
            {
                return rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA256);
            }

           return null;
        }

enter image description here

【问题讨论】:

  • 填充模式与证书不兼容。
  • @jdweng 我检查了我的证书,如果我将其更改为 SHA-1,它确实是另一个填充,我得到了同样的错误
  • Sha1 是 160 位,但您的链接显示 256。您希望 sha1 填充到 256 吗? Sha1 哈希的长度是 40 个十六进制字符给我们 20 个字节 = 160
  • @jdweng 我发现我的证书是 sha256 抱歉输入错误。我不明白你的意思?我认为我的数据大小是问题,但是如果我全部尝试,我仍然会收到此错误
  • 你的项目目标是什么? Net 中不支持所有加密模式。微软最终决定在 Net 4.7.2 中使用操作系统进行 TLS 加密。您可能使用的是旧版本的网络,其中加密不起作用。

标签: c# encryption certificate rsa public-key-encryption


【解决方案1】:

尝试如下代码将数组分解成更小的部分

            byte[] input = null;
            long blocksize = 1000000;
            for(long i = 0; i < input.Length; i += blocksize)
            {
                long chunksize = (input.Length - i > blocksize) ? blocksize : input.Length - i;
                byte[] temp = new byte[chunksize];
                Array.Copy(input, i, temp, 0, chunksize);
            }

【讨论】:

    【解决方案2】:

    @jdweng 感谢我解决问题的代码

       public byte[] EncryptDataOaepSha256(X509Certificate2 cert, byte[] data)
            {
                RSA rsa = cert.GetRSAPublicKey();
                byte[] input = data;
                long blocksize = 190;
                byte[] output = new byte[0];
    
                for (long i = 0; i < input.Length; i += blocksize)
                {
                    long chunksize = (input.Length - i > blocksize) ? blocksize : input.Length - i;
                    byte[] temp = new byte[chunksize];
                    Array.Copy(input, i, temp, 0, chunksize);
                    byte[] encrypteByte = rsa.Encrypt(temp, RSAEncryptionPadding.OaepSHA256);
    
                    if(output.Length > 0)
                    {
                        output = output.Concat(encrypteByte).ToArray();
                    }
                    else
                    {
                        output = encrypteByte;
                    }
                }
    
                return output;
            }
    

    【讨论】:

    • @jdweng 我现在唯一的问题是我的字典不起作用:/。我得到 e 要解密的数据长度对于此密钥的大小无效。我使用相同的逻辑只加密并使用私钥
    • Concat() 时需要在每个块之间添加一个字节数。然后,当您解密时,您会读取块大小并根据大小提取每个块。加密时,加密大小与原始数据的字节数不同。
    • @jdweng 您是否有该解决方案的示例。
    • 只需在每个块之间添加 4(int)或 8 byte(long):386(386 字节)400(400 字节)600(600 字节)。使用 BitConverter.GetBytes(Number) 和 BitConverter.ToInt32(byte[]) 进行转换。
    • @jdweng 你把我弄丢了
    【解决方案3】:
    public byte[] EncryptDataOaepSha256(X509Certificate2 cert, byte[] data, ILogger log)
        {
            RSA rsa = cert.GetRSAPublicKey();
            byte[] input = data;
            long blocksize = 182;
            long byteCounterInt = 0;
            byte[] byteCounter = BitConverter.GetBytes(byteCounterInt);
            byte[] output = new byte[0];
    
            try
            {
                for (long i = 0; i < input.Length; i += blocksize)
                {
                    long chunksize = (input.Length - i > blocksize) ? blocksize : input.Length - i;
                    byte[] temp = new byte[chunksize];
                    temp = temp.Concat(byteCounter).ToArray();
                    Array.Copy(input, i, temp, 0, chunksize);
                    byte[] encrypteByte = rsa.Encrypt(temp, RSAEncryptionPadding.OaepSHA256);
    
                    if (output.Length > 0)
                    {
                        output = output.Concat(encrypteByte).ToArray();
                    }
                    else
                    {
                        output = encrypteByte;
                    }
                }
    
                return output;
            }
            catch(Exception e)
            {
                log.LogCritical("Error encrypting a stream");
                log.LogCritical(e.Message);
                log.LogCritical(e.StackTrace);
                log.LogCritical(e.ToString());
    
                return null;
            }
        }
    
     public byte[] DecryptDataOaepSha256(X509Certificate2 cert, byte[] data, ILogger log)
        {
            RSA rsa = cert.GetRSAPublicKey();
    
            byte[] input = data;
            long blocksize = 190;
            long byteCounterInt = 0;
            byte[] byteCounter = BitConverter.GetBytes(byteCounterInt);
            byte[] output = new byte[0];
    
            try
            {
                for (long i = 0; i < input.Length; i += blocksize)
                {
                    long chunksize = (input.Length - i > blocksize) ? blocksize : input.Length - i;
                    byte[] temp = new byte[chunksize];
                    Array.Copy(input, i, temp, 0, chunksize);
                    byte[] tempMinBytecount = new byte[temp.Length - byteCounter.Length];
                    Array.Copy(temp, byteCounter.Length, tempMinBytecount, 0, tempMinBytecount.Length);
    
                    byte[] decrypteByte = rsa.Decrypt(tempMinBytecount, RSAEncryptionPadding.OaepSHA256);
    
                    if (output.Length > 0)
                    {
                        output = output.Concat(decrypteByte).ToArray();
                    }
                    else
                    {
                        output = decrypteByte;
                    }
                }
    
                return output;
            }
            catch (Exception e)
            {
                log.LogCritical("Error decrypting a stream");
                log.LogCritical(e.Message);
                log.LogCritical(e.StackTrace);
                log.LogCritical(e.ToString());
    
                return null;
            }
    
        }
    

    @jdweng 我仍然遇到同样的错误

    【讨论】:

      猜你喜欢
      • 2011-06-23
      • 2019-05-23
      • 2015-05-27
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      相关资源
      最近更新 更多