CBC モードの AES アルゴリズムでデータを暗号化しようとしています。このため、.Net Library 'Bouncy Castle' を使用しています。私は暗号のバックグラウンドがないので、簡単な方法で使用しようとしています。ここに私の暗号化コードがあります
public byte[] encrypt(byte[] key, byte[] iv,byte[] data)
{
IBlockCipher engine=new AesFastEngine();
KeyParameter keyParam = new KeyParameter(key);
CbcBlockCipher cipher = new CbcBlockCipher(engine);
ICipherParameters parameters = new ParametersWithIV(keyParam, iv);
byte[] output=new byte[16+data.Length];
cipher.Init(true, parameters);
cipher.ProcessBlock(data, 0, output, data.Length);
//process output
byte[] cipherArray = new byte[data.Length];
/*
int k=0;
for (int i = 0; i < output.Length; i++)
{
if (output[i]!= 0)
{
cipherArray[k++] = output[i];
}
}
*/
return cipherArray;
}
16 の乗算ではない入力を試みると、例外が発生します。配列の右側に (16-length%16) の数値をパディングし、左側をゼロにすると、結果が得られます。しかし、結果は私にとっても問題です。次のような結果が得られます。
[0][0][0][0[111][22][33][44][66][77][33][12][32][23][0][0][0][0][0]
左右ともゼロ。
私はそれが私の機能の使用についてかもしれないと思いましたProcessBlock(data, 0, output, data.Length)
。出力が私の暗号化されたテキストになるという前提で使用していますが、出力は入力の長さよりも長くなるはずです。この関数に関するドキュメントがないため、間違った方法で使用している可能性があります。どんな助けでもいただければ幸いです