【发布时间】:2010-11-15 00:06:44
【问题描述】:
我是加密新手。我需要实现非对称加密算法,我认为它使用私钥/公钥。我开始使用 RSACryptoServiceProvider 的示例。可以加密小数据。但是当在相对较大的数据“2行”上使用它时,我得到异常 CryptographicException “Bad Length”!
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
//Import the RSA Key information. This only needs
//toinclude the public key information.
//RSA.ImportParameters(RSAKeyInfo);
byte[] keyValue = Convert.FromBase64String(publicKey);
RSA.ImportCspBlob(keyValue);
//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
然后我发现了一些使用 CryptoStream 加密大数据(或文件)的示例,并且仅使用 DES 或 3DES 等对称算法,这些算法具有 CreateEncryptor 函数以返回 ICryptoTransform 作为 CryptoStream 构造函数的输入之一!! !
CryptoStream cStream = new CryptoStream(fStream,
new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
CryptoStreamMode.Write);
使用 RSA 加密文件的方法是什么?
【问题讨论】:
-
私有/公共算法不适合加密大数据。在实践中,它们用于在两方之间交换私钥,用于对称加密/解密大数据。
标签: c# cryptography rsa encryption-asymmetric 3des