简体   繁体   English

Rsa 加密解密中的错误长度错误

[英]Bad Length Error in Rsa encryption decryption

Please help me identify problem in following RSA Encryption code请帮助我识别以下 RSA 加密代码中的问题

public static void Test()
{
    CspParameters cspParams = new CspParameters { ProviderType = 1 };
    RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(1024, cspParams);

    var PublicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));  //I have to save it as string in some json/app.config configuration file
    var PrivateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));  //I have to save it as string in some json/app.config configuration file
    
    var encrypt = EncryptText(PublicKey,  Encoding.UTF8.GetBytes(FromSomeFile()));
    
    var decrypt = DecryptData(PrivateKey, encrypt);
}

static byte[] EncryptText(string publicKey, byte[] dataToEncrypt)
{   
    byte[] encryptedData;
    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
    {                
        rsa.ImportCspBlob(Convert.FromBase64String(publicKey));             
        encryptedData = rsa.Encrypt(dataToEncrypt, false);
    }
    return encryptedData;
}

// Method to decrypt the data withing a specific file using a RSA algorithm private key   
static string DecryptData(string privateKey, byte[] dataToDecrypt)
{
    
    // Create an array to store the decrypted data in it   
    byte[] decryptedData;
    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
    {
        rsa.ImportCspBlob(Convert.FromBase64String(privateKey));              
        decryptedData = rsa.Decrypt(dataToDecrypt, false);
    }           
    
    return Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length); ;
}

RSA can only be used to encrypt messages whose length is less than the modulus. RSA 只能用于加密长度小于模数的消息。 How much smaller depends on the padding, eg 11 bytes in case of PKCS#1 v1.5, s.小多少取决于填充,例如在 PKCS#1 v1.5 的情况下为 11 个字节,s。 here .在这里 In the case of OAEP, the number of bytes claimed by padding depends on the digest used, s.在 OAEP 的情况下,填充要求的字节数取决于使用的摘要,s。 here .在这里 The details are described in RFC8017, RSAES-PKCS1-v1_5 and RSAES-OAEP .详细信息在 RFC8017、 RSAES-PKCS1-v1_5RSAES-OAEP中进行了描述。
For completeness: RSA without padding (textbook RSA) allows the encryption of messages up to exactly the length of the modulus.为了完整性:没有填充的 RSA(教科书 RSA)允许将消息加密到模数的长度。 In practice, however, padding must always be used for security reasons, so textbook RSA is not a real option.然而,在实践中,出于安全原因,必须始终使用填充,因此教科书 RSA 并不是一个真正的选择。

The posted code uses an RSA key of 1024 bits and PKCS#1 v1.5 padding.发布的代码使用 1024 位的 RSA 密钥和 PKCS#1 v1.5 填充。 The maximum size of the message to be encrypted is therefore 117 bytes.因此,要加密的消息的最大大小为 117 个字节。 Larger messages throw a CryptographicException (Bad Length) .较大的消息会引发CryptographicException (Bad Length) That is the reason for your issue.这就是你的问题的原因。

A 8192 bits (1024 bytes) key would theoretically allow messages up to 1013 bytes in length to be encrypted with PKCS#1 v1.5 Padding.理论上,一个 8192 位(1024 字节)的密钥将允许使用 PKCS#1 v1.5 填充对最长 1013 字节的消息进行加密。 However, the performance decreases strongly with increasing key size, s.但是,随着密钥大小 s 的增加,性能会急剧下降。 here . 在这里

Symmetric encryption is more performant than asymmetric encryption .对称加密非对称加密性能更高。 Therefore, in practice larger data volumes are encrypted using symmetric encryption, eg AES.因此,在实践中,使用对称加密(例如 AES)对较大的数据量进行加密。 However, symmetric encryption has the problem that the communication partners have to exchange the symmetric key.然而,对称加密存在通信伙伴必须交换对称密钥的问题。 Asymmetric encryption, eg RSA, is typically used for this purpose ( hybrid encryption ), since only the public keys are needed for encryption (which can therefore be exchanged over an insecure channel).非对称加密,例如 RSA,通常用于此目的(混合加密),因为加密只需要公钥(因此可以通过不安全的通道进行交换)。 However, to prevent a deceptive replacement of the public keys ( man in the middel attack ), a complex public key infrastructure is generally necessary.但是,为了防止欺骗性地替换公钥(中间人攻击),通常需要复杂的公钥基础设施

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM