我試圖使用bouncycastle庫加密和解密數據,解密數據後第一個數據塊信息被破壞就好像它未能成功解密並且最後一塊完全丟失一樣。我是bouncycastle圖書館的新手,我一直在搜索整個互聯網,試圖通過使用PKCS7Padding在CBC模式下找到一個明智的AES加密實現,但是我一直沒有遇到過很多文檔。提供的任何幫助將不勝感激,也應該指出,我是一名學生而不是專業開發人員。謝謝。輸出的Rijndael使用BouncyCastle解密產生損壞的數據開始和結尾,但其餘數據完全解密
`public class RijndaelCBC
{
private int _keySize;
private byte[] _passphrase;
public byte[] _iv { get; private set; }
private IBlockCipher blockCipher;
private PaddedBufferedBlockCipher aesCipher;
private ParametersWithIV _param;
public RijndaelCBC(int KeySize, string Passphrase)
{
if (Passphrase.Length < KeySize/8)
Passphrase = Passphrase.PadRight(KeySize/8, '0');
if (Passphrase.Length > KeySize)
Passphrase = Passphrase.Substring(0, KeySize/8);
_passphrase = System.Convert.FromBase64String(Passphrase);
Array.Resize(ref _passphrase, KeySize/8);
_keySize = KeySize;
Random rnd = new Random();
_iv = new byte[_keySize/8];
for (int t = 0; t < _keySize/8; t++)
rnd.Next();
rnd.NextBytes(_iv);
if (_keySize != 128 && _keySize != 192 && _keySize != 256)
throw new Exception(string.Format("Invalid key size of {0} provided, cannot continue with the process.", _keySize));
}
public RijndaelCBC(int KeySize, string Passphrase, byte[] iv)
{
if (Passphrase.Length < KeySize/8)
Passphrase = Passphrase.PadRight(KeySize/8, '0');
if (Passphrase.Length > KeySize)
Passphrase = Passphrase.Substring(0, KeySize/8);
_passphrase = System.Convert.FromBase64String(Passphrase);
Array.Resize(ref _passphrase, KeySize/8);
_keySize = KeySize;
_iv = iv;
if (_keySize != 128 && _keySize != 192 && _keySize != 256)
throw new Exception(string.Format("Invalid key size of {0} provided, cannot continue with the process.", _keySize));
}
public byte[] Encrypt(byte[] data)
{
try
{
blockCipher = new CbcBlockCipher(new RijndaelEngine(_keySize));
_param = new ParametersWithIV(new KeyParameter(_passphrase), _iv);
blockCipher.Init(true, _param);
aesCipher = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding());
byte[] cipherTextBlock = null;
int blockSize = aesCipher.GetBlockSize();
List<byte> output = new List<byte>();
int outputLen = 0;
int chunkPosition = 0;
for (chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
{
byte[] dataToProcess = new byte[blockSize];
int chunkSize = (data.Length - chunkPosition) < blockSize ? (data.Length - chunkPosition) : blockSize;
Buffer.BlockCopy(data, chunkPosition, dataToProcess, 0, chunkSize);
cipherTextBlock = new byte[blockSize];
outputLen = aesCipher.ProcessBytes(dataToProcess, 0, chunkSize, cipherTextBlock, 0);
output.AddRange(cipherTextBlock);
}
try
{
if(chunkPosition < data.Length &&
chunkPosition + blockSize > data.Length)
{
byte[] dataToProcess = new byte[blockSize];
int chunkSize = (chunkPosition + blockSize) - data.Length;
Buffer.BlockCopy(data, chunkPosition, dataToProcess, 0, chunkSize);
aesCipher.DoFinal(cipherTextBlock, 0);
output.AddRange(cipherTextBlock);
}
}
catch (CryptoException ex)
{}
return output.ToArray();
}
catch (System.Exception ex)
{ }
return null;
}
public byte[] Decrypt(byte[] data)
{
try
{
blockCipher = new CbcBlockCipher(new RijndaelEngine(_keySize));
_param = new ParametersWithIV(new KeyParameter(_passphrase), _iv);
blockCipher.Init(false, _param);
aesCipher = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding());
byte[] cipherTextBlock = null;
int blockSize = aesCipher.GetBlockSize();
List<byte> output = new List<byte>();
int outputLen = 0;
int chunkPosition = 0;
for (chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
{
byte[] dataToProcess = new byte[blockSize];
int chunkSize = (data.Length - chunkPosition) < blockSize ? (data.Length - chunkPosition) : blockSize;
Buffer.BlockCopy(data, chunkPosition, dataToProcess, 0, chunkSize);
cipherTextBlock = new byte[blockSize];
outputLen = aesCipher.ProcessBytes(dataToProcess, 0, chunkSize, cipherTextBlock, 0);
output.AddRange(cipherTextBlock);
}
try
{
if (chunkPosition < data.Length &&
chunkPosition + blockSize > data.Length)
{
byte[] dataToProcess = new byte[blockSize];
int chunkSize = (chunkPosition + blockSize) - data.Length;
Buffer.BlockCopy(data, chunkPosition, dataToProcess, 0, chunkSize);
aesCipher.DoFinal(cipherTextBlock, 0);
output.AddRange(cipherTextBlock);
}
}
catch (CryptoException ex)
{ }
return output.ToArray();
}
catch (System.Exception ex)
{ }
return null;
}
}`
示例: 開頭數據的應該是:
生產版本發佈
但作爲最終: [¨dZJÊ)UOL)ȱýº-NU 〜VE'·ðúœ×ñðersion發行
和數據結束應該是: 對於此類產品,英特爾不承擔任何責任, 和英特爾不做任何明示或暗示的擔保, 與銷售和/或使用英特爾產品,包括 責任或適用性的保證了 特定目的,適銷性或侵犯任何 專利,版權或其他知識產權。英特爾產品不適用於醫療, 拯救生命或維持生命的應用程序。
但作爲結束: 此類產品,英特爾不承擔任何責任, 和英特爾不做任何明示或暗示的擔保, 與銷售和/或使用英特爾產品,包括 責任或warran
我試圖加密和解密一個例子,但是最終數據丟失了,開始時數據沒有正確解密,但52KB文件的其餘部分是完美的。
您能否將您的代碼示例轉換爲[SSCCE](http://sscce.org),我們可以複製/粘貼並自行觀察問題? –
如果您希望代碼在完成後能夠正常工作,則不應該捕獲Exception或CryptoException。如果你希望你的代碼在某些情況下工作,其餘的是一個錯誤的混亂,請不要讓它進入。 – Henrik
有些東西告訴我,局部變量keySizeBytes是個好主意。 –