我使用标准代码来加密该文件。证书未过期且密钥有效。我找不到它抛出此异常的原因。
public byte[] EncryptDataOaepSha256(X509Certificate2 cert, byte[] data)
{
RSA rsa = cert.GetRSAPublicKey();
if (rsa != null)
{
return rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA256);
}
return null;
}
尝试下面的代码将数组分成更小的部分
byte[] input = null;
long blocksize = 1000000;
for(long i = 0; i < input.Length; i += blocksize)
{
long chunksize = (input.Length - i > blocksize) ? blocksize : input.Length - i;
byte[] temp = new byte[chunksize];
Array.Copy(input, i, temp, 0, chunksize);
}
@jdweng 感谢我修复问题的代码
public byte[] EncryptDataOaepSha256(X509Certificate2 cert, byte[] data, ILogger log)
{
RSA rsa = cert.GetRSAPublicKey();
byte[] input = data;
long blocksize = 182;
long byteCounterInt = 0;
byte[] byteCounter = BitConverter.GetBytes(byteCounterInt);
byte[] output = new byte[0];
try
{
for (long i = 0; i < input.Length; i += blocksize)
{
long chunksize = (input.Length - i > blocksize) ? blocksize : input.Length - i;
byte[] temp = new byte[chunksize];
temp = temp.Concat(byteCounter).ToArray();
Array.Copy(input, i, temp, 0, chunksize);
byte[] encrypteByte = rsa.Encrypt(temp, RSAEncryptionPadding.OaepSHA256);
if (output.Length > 0)
{
output = output.Concat(encrypteByte).ToArray();
}
else
{
output = encrypteByte;
}
}
return output;
}
catch(Exception e)
{
log.LogCritical("Error encrypting a stream");
log.LogCritical(e.Message);
log.LogCritical(e.StackTrace);
log.LogCritical(e.ToString());
return null;
}
}
public byte[] DecryptDataOaepSha256(X509Certificate2 cert, byte[] data, ILogger log)
{
RSA rsa = cert.GetRSAPublicKey();
byte[] input = data;
long blocksize = 190;
long byteCounterInt = 0;
byte[] byteCounter = BitConverter.GetBytes(byteCounterInt);
byte[] output = new byte[0];
try
{
for (long i = 0; i < input.Length; i += blocksize)
{
long chunksize = (input.Length - i > blocksize) ? blocksize : input.Length - i;
byte[] temp = new byte[chunksize];
Array.Copy(input, i, temp, 0, chunksize);
byte[] tempMinBytecount = new byte[temp.Length - byteCounter.Length];
Array.Copy(temp, byteCounter.Length, tempMinBytecount, 0, tempMinBytecount.Length);
byte[] decrypteByte = rsa.Decrypt(tempMinBytecount, RSAEncryptionPadding.OaepSHA256);
if (output.Length > 0)
{
output = output.Concat(decrypteByte).ToArray();
}
else
{
output = decrypteByte;
}
}
return output;
}
catch (Exception e)
{
log.LogCritical("Error decrypting a stream");
log.LogCritical(e.Message);
log.LogCritical(e.StackTrace);
log.LogCritical(e.ToString());
return null;
}
}
在上一篇文章中,代码中有一个小错误,已修复。 此外,它已从 SHA256 更改为 SHA512,这会更改块大小。因此,输入为 382 字节,输出为 512 字节,您应该保存它。 现在你的块有 512 字节,已排列好。 要从解密中读取,您必须分离 512 字节的数据包并检查它。
用于加密
byte[] input=data;
long blockSize = 382;
byte[] output = Array.Empty<byte>();
try {
for (long i = 0; i < input.Length; i += blockSize)
{
long chunkSize = (input.Length - i > blockSize) ? blockSize : input.Length - i;
byte[] temp = new byte[chunkSize];
temp = temp.ToArray();
Array.Copy(input, i, temp, 0, chunkSize);
byte[] encryptedByte = rsa.Encrypt(temp, RSAEncryptionPadding.OaepSHA512);
if (output.Length > 0)
output = output.Concat(encryptedByte).ToArray();
else
output = encryptedByte;
}
}
catch (Exception e)
{
}
用于解密
byte[] input=data;
long blockSize = 512;
byte[] output = Array.Empty<byte>();
try
{
for (long i = 0; i < input.Length; i += blockSize)
{
long chunkSize = (input.Length - i > blockSize) ? blockSize : input.Length - i;
byte[] temp = new byte[chunkSize];
Array.Copy(input, i, temp, 0, chunkSize);
byte[] encryptedByte = rsa.Decrypt(temp, RSAEncryptionPadding.OaepSHA512);
if (output.Length > 0)
output = output.Concat(encryptedByte).ToArray();
else
output = encryptedByte;
}
}
catch (Exception e)
{}
根据你的理由使用输出。