C#RSA Decrypt(参数不正确)

时间:2017-09-07 02:26:27

标签: c# encryption rsa

我使用以下代码使用RSA加密和解密一个小文件并使用USB驱动器安全地传输它们(我知道,RSA不是最好的加密文件......)

但由于某种原因它不再工作了。我试图解密我的文件,它不断抛出“参数不正确”异常

密钥没有改变(私人和公共) 代码没有改变(加密和解密)

任何人都知道可能出现什么问题?

static int SegmentLength = 213; //85; // keysize/8 -42 -1

    public static byte[] EncryptRSA(string publicKey, byte[] data)
    {
        CspParameters cspParams = new CspParameters { ProviderType = 1 };
        RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(2048, cspParams);


        rsaProvider.ImportCspBlob(Convert.FromBase64String(publicKey));

        //byte[] plainBytes = data;
        //byte[] encryptedBytes = rsaProvider.Encrypt(plainBytes, false);

        var length = data.Length / SegmentLength + 1;
        MemoryStream stream = new MemoryStream();

        for (var i = 0; i < length; i++)
        {
            int lengthToCopy;
            if (i == length - 1 || data.Length < SegmentLength)
                lengthToCopy = data.Length - (i * SegmentLength);
            else
                lengthToCopy = SegmentLength;


            //var segment = decryptedData.Substring(i * SegmentLength, lengthToCopy);
            byte[] segment = new byte[lengthToCopy];
            Buffer.BlockCopy(data, i * SegmentLength, segment, 0, lengthToCopy);
            byte[] encrypted = rsaProvider.Encrypt(segment,false);
            stream.Write(encrypted,0,encrypted.Length);
        }

        return stream.ToArray();
    }

private const int EncryptedLength = 256; //keylen * 8

    public static byte[] DecryptRSA(string privateKey, byte[] data)
    {
        CspParameters cspParams = new CspParameters { ProviderType = 1 };
        RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(2048, cspParams);

        rsaProvider.ImportCspBlob(Convert.FromBase64String(privateKey));

        var rsainfo = rsaProvider.ExportParameters(true);

        var length = data.Length / EncryptedLength;
        MemoryStream stream = new MemoryStream();

        for (var i = 0; i < length; i++)
        {
            byte[] segment = new byte[EncryptedLength];
            Buffer.BlockCopy(data, i * EncryptedLength, segment, 0, EncryptedLength);
            byte[] decrypted = rsaProvider.Decrypt(segment, false);

            stream.Write(decrypted, 0, decrypted.Length);

        }

        //string plainText = Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);

        return stream.ToArray();
    }
  

发生System.Security.Cryptography.CryptographicException     HResult = 0x80070057消息=Parâmetroincorreto。

     

Source = mscorlib StackTrace:at   System.Security.Cryptography.CryptographicException.ThrowCryptographicException(的Int32   hr)at   System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle   pKeyContext,Byte [] pbEncryptedKey,Int32 cbEncryptedKey,Boolean   fOAEP,ObjectHandleOnStack ohRetDecryptedKey)at   System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(字节[]   rgb,布尔fOAEP)在Decriptor.Program.DecryptRSA(字符串   privateKey,Byte [] data)in   Program.cs:第149行   在Decriptor.Program.Main(String [] args)中   Program.cs:第45行

     
      
  • rsainfo {System.Security.Cryptography.RSAParameters} System.Security.Cryptography.RSAParameters
  •   
  • D {byte [256]} byte []
  •   
  • DP {byte [128]} byte []
  •   
  • DQ {byte [128]} byte []
  •   
  • 指数{byte [3]} byte []
  •   
  • InverseQ {byte [128]} byte []
  •   
  • 模数{byte [256]} byte []
  •   
  • P {byte [128]} byte []
  •   
  • Q {byte [128]} byte []
  •   

0 个答案:

没有答案