0

RSACryptoServiceProvider()C# でクラスを使用してデータを暗号化しています。c#で暗号化されたubuntuのデータを復号化したい。復号化するために必要なメカニズムを教えてください。次の関数は暗号化用です。

public static void Encrypt(String PublicKey, String plainText, out String cipherText)
{
    try
    {             
        int dwKeySize = 1024;
        // TODO: Add Proper Exception Handlers
        RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
        rsaCryptoServiceProvider.FromXmlString(PublicKey);
        int keySize = dwKeySize / 8;
        byte[] bytes = Encoding.UTF32.GetBytes(plainText);
        // The hash function in use by the .NET RSACryptoServiceProvider here is SHA1
        // int maxLength = ( keySize ) - 2 - ( 2 * SHA1.Create().ComputeHash( rawBytes ).Length );
        int maxLength = keySize - 42;
        int dataLength = bytes.Length;
        int iterations = dataLength / maxLength;
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i <= iterations; i++)
        {
            byte[] tempBytes = new byte[(dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i];
            Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length);
            byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes, true);
            // Be aware the RSACryptoServiceProvider reverses the order 
            // of encrypted bytes after encryption and before decryption.
            // If you do not require compatibility with Microsoft Cryptographic API
            // (CAPI) and/or other vendors.
            // Comment out the next line and the corresponding one in the 
            // DecryptString function.
            Array.Reverse(encryptedBytes);
            // Why convert to base 64?
            // Because it is the largest power-of-two base printable using only ASCII characters
            stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
        }
        cipherText = stringBuilder.ToString();
    }
    catch (Exception e)
    {
        cipherText = "ERROR_STRING";
        Console.WriteLine("Exception in RSA Encrypt: " + e.Message);
        //throw new Exception("Exception occured while RSA Encryption" + e.Message);
    }
} 
4

2 に答える 2

1

そのようにRSAを使用しないでください。そのように使用することを意図したものではなく、遅すぎます。

正しい方法は、AES などの対称アルゴリズムを使用し、RSA で使用したキーを暗号化することです。それを行う C# コードについては、私の古いブログ エントリを参照してください。

于 2012-05-30T21:41:50.330 に答える
0

同じメカニズムが必要ですが、逆です。最初に試して、後で聞いてください。

于 2012-06-01T14:10:28.823 に答える