填充无效,无法在 C# 许可方法中删除

本文关键字:许可 方法 删除 无效 填充 | 更新日期: 2023-09-27 18:33:38

在我的项目中,我正在执行一种许可方法,在用户输入许可证密钥后,他的产品将被激活。我使用以下代码,但我收到一个异常说"填充无效,无法删除"。以下是我的代码

public void ValidateProductKey()
        {
        RSACryptoServiceProvider _cryptoService = new RSACryptoServiceProvider();
        string productKey = "G7MA4Z5VR5R3LG001AS1N5HA3YHX05";
            byte[] keyBytes = Base32Converter.FromBase32String(productKey); //Base32Converter is my customized method which returns byte of values;
            byte[] signBytes = new byte[2];
            byte[] hiddenBytes = new byte[16];
            using (MemoryStream stream = new MemoryStream(keyBytes))
            {
                stream.Read(hiddenBytes, 0, 8);
                stream.Read(signBytes, 0, 2);
                stream.Read(hiddenBytes, 8, hiddenBytes.Length - 8);
                keyBytes = stream.ToArray();
            }
            byte[] sign = _cryptoService.SignData(signBytes, new SHA1CryptoServiceProvider());
            byte[] rkey = new byte[32];
            byte[] rjiv = new byte[16];
            Array.Copy(sign, rkey, 32);
            Array.Copy(sign, 32, rjiv, 0, 16);
            SymmetricAlgorithm algorithm = new RijndaelManaged();
            try
            {
                hiddenData = algorithm.CreateDecryptor(rkey, rjiv).TransformFinalBlock(hiddenBytes,0,hiddenBytes.Length);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

当到达"隐藏数据"变量时,我得到一个异常,因为"填充无效,无法删除"。任何帮助将不胜感激。

填充无效,无法在 C# 许可方法中删除

您希望通过使用 SHA1/RSA 对signBytes数组进行签名来生成 Rijndael 解密的密钥和 IV,但是从您给出的代码来看,您不会使用密钥对初始化签名过程中使用的RSACryptoServiceProvider,因此它将具有随机生成的密钥,因此签名过程的结果每次都会不同。因此,Rijndael 解密中使用的密钥/IV 与创建许可证密钥时用于加密hiddenData的密钥/IV 不同。

您使用的方法本身还有许多其他问题,但这超出了您的问题范围。