【发布时间】:2020-09-03 18:15:43
【问题描述】:
我已经使用 BounyCastle v1.8.6 .net 核心实现了 AES 128 CBC 加密,当我尝试加密长度为 16 字节的数据时,它会添加另一个包含数据的 16 字节 - 我猜是填充 - 。我不知道为什么它会添加这个。知道默认的 c# 实现不会这样做
以十六进制格式作为字符串的数据示例 6752b87027fc7728fbe4ec2f0d76da75 => EE93F245FCDB928BF8E3012C9E5150EDA1B876A6D790CFC69C2F129215B3C938
如果要加密的数据是 32、64、128 等,也会发生这种情况
代码:
int DefaultBlockSize = 16;
public byte[] Encrypt(byte[] plainBytes, byte[] key, byte[] iv = null)
{
iv ??= new byte[DefaultBlockSize] ;
AesEngine engine = new AesEngine();
CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
IBlockCipherPadding padding = new ZeroBytePadding();
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher, padding);
KeyParameter keyParam = new KeyParameter(key);
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, DefaultBlockSize);
// Encrypt
cipher.Init(true, keyParamWithIV);
byte[] outputBytes = new byte[cipher.GetOutputSize(plainBytes.Length)];
int length = cipher.ProcessBytes(plainBytes, outputBytes, 0);
cipher.DoFinal(outputBytes, length);
return outputBytes ;
}
任何想法如何解决这个问题?
【问题讨论】:
标签: c# asp.net-core bouncycastle