66

次の関数を呼び出す場合:

byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(stringToEncrypt), true);

私は今、エラーが発生しています: 長さが正しくありません。

より小さな文字列で機能します。私が渡している文字列に問題がある可能性があるという考えは、200文字未満です。

4

4 に答える 4

95

RSA 暗号化は少量のデータのみを意味します。暗号化できるデータの量は、使用しているキーのサイズによって異なります。たとえば、1024 ビットの RSA キーと PKCS # 1 V1.5 パディングの場合、暗号化できます。最大で 117 バイト、2048 RSA キーを使用すると、245 バイトを暗号化できます。

これには十分な理由があります。非対称暗号化は計算コストが高くなります。大量のデータを暗号化する場合は、対称暗号化を使用する必要があります。しかし、否認防止が必要な場合はどうでしょうか? 次に、両方を使用します。対称鍵を作成し、非対称暗号化を使用して交換すると、対称鍵が安全に交換され、大量のデータが暗号化されます。これは、SSL と WS-Secure がカバーの下で使用するものです。

于 2009-09-30T08:41:59.140 に答える
56

RSA 不正な長さの例外に関する今後の検索について...

特定の鍵サイズで暗号化できる最大バイト数は、次のように計算できます。

((KeySize - 384) / 8) + 37

ただし、元の投稿にあるように、最適な非対称暗号化パディング (OAEP) パラメーターが true の場合、以下を使用して最大バイト数を計算できます。

((KeySize - 384) / 8) + 7

有効なキー サイズは 384 ~ 16384 で、スキップ サイズは 8 です。

于 2010-07-15T06:56:12.083 に答える
5

I faced the same challenge while doing 2048 RSA encryption of plain text having less than 200 characters.

In my opinion, we can achieve the target without getting into complexity of Symmetric or Asymmetric encryption, with following simple steps;

By doing so I managed to encrypt and decrypt 40x larger text

Encryption:

  1. Compress the plain text by using *Zip() method and convert into array of bytes
  2. Encrypt with RSA

Decryption:

  1. Decrypt cypher text with RSA
  2. un-compress decrypted data by using **Unzip() method

*byte[] bytes = Zip(stringToEncrypt); // Zip() method copied below

**decryptedData = Unzip(decryptedBytes); // Unzip() method copied below


public static byte[] Zip(string str)
{
    var bytes = System.Text.Encoding.UTF8.GetBytes(str);    
    using (var msi = new MemoryStream(bytes))
    using (var mso = new MemoryStream())
    {
        using (var gs = new GZipStream(mso, CompressionMode.Compress))
        {                        
            CopyTo(msi, gs);
        }    
        return mso.ToArray();
    }
}
public static string Unzip(byte[] bytes)
{
    using (var msi = new MemoryStream(bytes))
    using (var mso = new MemoryStream())
    {
        using (var gs = new GZipStream(msi, CompressionMode.Decompress))
        {                     
            CopyTo(gs, mso);
        }    
        return System.Text.Encoding.UTF8.GetString(mso.ToArray());
    }
}

public static void CopyTo(Stream src, Stream dest)
    {
        byte[] bytes = new byte[4096];

        int cnt;

        while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
        {
            dest.Write(bytes, 0, cnt);
        }
    }
于 2019-11-19T08:54:15.950 に答える