2 回答
Thanks to Hans Passant I found the solution. The problem was that I was using Encoding.GetString()
or Encoding.GetBytes()
when I was encrypting and decrypting, when I should have been using Convert.ToBase64String()
or Convert.FromBase64String()
.
I had the same problem of extra output. For me it was not encoding-problem, because I was passing it as byte array in BCrypt library. As it is plain-text, I would use space-character as padding before encryption and trim after decryption.
int padding = BLOCK_SIZE - (input_len+1)%BLOCK_SIZE;
if(padding && (input_len+padding) <= buf_size)
{
memset(buf+input_len, ' ', padding);
input_len += padding;
}
For 128
bit encryption, the block-size is 16
. Note that the buf_size
should be multiple of the block-size
to make it work all the time. As we padded the input already, we do not need the padding algorithm in decryption.
tdsAlg.Padding = PaddingMode.None;
And at the end of decryption, I would trim the output.