0

RSA と C# を使用して XML ファイルを暗号化してから復号化しようとしていますが、非常に近いのですが、問題があります。復号化すると、ほとんどすべてのファイルがそこにありますが、最後に問題があります. ファイルの最後にギャップがあるか、ファイルの最後にさらにデータが追加されています。

これが私の暗号化方法です:

    public static bool Encrypt(ProcessingHolder ph)
    {
        FileInfo inFile = ph.encryptedFI;
        FileInfo outFile = ph.unEncryptedFI;

        X509Certificate2 daCert = new X509Certificate2(keyFP, daCertPassword);
        RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)daCert.PrivateKey;

        bool done = false;
        FileStream fs = null;
        FileStream fso = null;

        try
        {
            //opens the file to encrypt into a filestream object
            fs = inFile.OpenRead();

            //240 is what the iOS side is using
            //algorithm that calculates max bytes ((KeySize - 384) / 8) + 37 
            //(returns 245)
            int chunkSize = 245;

            fso = outFile.OpenWrite();
            byte[] buffer = new byte[chunkSize];
            int totalRead = 0;


            while (totalRead < fs.Length)
            {
                int readBytes = fs.Read(buffer,0, chunkSize);

                totalRead += readBytes;

                //check to see if the final chunk of data is less than 245 so as not to write empty buffer
                if (readBytes < chunkSize) buffer = new byte[readBytes];
                //byte[] encr = new byte[readBytes];

                //actual encryption
                //encr = RSA.Encrypt(buffer, false);

                byte[] encr = RSA.Encrypt(buffer, false);
                fso.Write(encr, 0, encr.Length);
            }
            fso.Flush();
            fso.Close();
            fs.Close();
            done = true;
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Decrypt failed with message " + ex.Message);
            done = false;
        }
        finally
        {
            if (fs != null) fs.Close();
            if (fso != null) fso.Close();
        }
        return done;
    }
}

ここに私の復号化方法があります:

    public static bool Decrypt(ProcessingHolder ph)
    {
        FileInfo inFile = ph.encryptedFI;
        FileInfo outFile = ph.unEncryptedFI;

        X509Certificate2 daCert = new X509Certificate2(keyFP, daCertPassword);
        RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)daCert.PrivateKey;

        bool done = false;
        FileStream fs = null;
        FileStream fso = null;

        try
        {
            fs = inFile.OpenRead();
            int chunkSize = 256;

            fso = outFile.OpenWrite();
            byte[] buffer = new byte[chunkSize];
            int totalRead = 0;

            while (totalRead < fs.Length)
            {
                int readBytes = fs.Read(buffer, 0, chunkSize);
                totalRead += readBytes;

                //check to see if the final chunk of data is less than 245 so as not to write empty buffer
                //if (readBytes < chunkSize) buffer = new byte[readBytes];

                byte[] decr = RSA.Decrypt(buffer, false);
                fso.Write(decr, 0, decr.Length);
            }
            fso.Flush();
            fso.Close();
            fs.Close();
            done = true;
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Decrypt failed with message " + ex.Message);
            done = false;
        }
        finally
        {
            if (fs != null) fs.Close();
            if (fso != null) fso.Close();
        }
        return done;
    }

ここで壁に頭をぶつけます、よろしくお願いします

4

1 に答える 1