Efficient implementation of Blowfish cipher with minimal memory allocations in ECB, CBC and CTR block modes. CTR is also available as encryption and decryption stream.
License
—
Deps
0
Install Size
—
Vulns
✓ 0
Published
Feb 16, 2026
$ dotnet add package Encryption.BlowfishEfficient implementation of Blowfish cipher with minimal memory allocations in ECB, CBC and CTR block modes.
CTR is also available as encryption and decryption stream.
Non-stream variants work by mutating input buffer (Span<byte>).
Implemented using defaults, 16-round with pre-computed subkeys.
Tested against well-known test vectors.
nuget package Encryption.Blowfish
dotnet add package Encryption.Blowfish [ -v 2.0.0 ]
using Encryption.Blowfish;
using System.Security.Cryptography;
var key = "a3bd614b27864e3f854b971f9df1a802"; // cipher key
var iv = RandomNumberGenerator.GetBytes(8); // IV
byte[] buf = ...; // data you want to encrypt
buf = buf.CopyAndPadIfNotAlreadyPadded();
var cbc = new BlowfishCbc(key);
var ok = cbc.Encrypt(buf, iv);
ok = cbc.Decrypt(buf, iv);
using Encryption.Blowfish;
using System.Security.Cryptography;
var codec = new Codec(Convert.FromHexString("df83d31539c244d298ce302036f91edd"));
var iv = RandomNumberGenerator.GetBytes(8);
// encrypt
using var encrypted = new MemoryStream();
using (var bfe = new BlowfishCtrEncryptStream(encrypted, codec, iv))
{
bfe.Write(...);
bfe.Write(...);
// ...
}
// It is important to dispose or Close the stream as soon as the writing is finished.
enc.Position = 0;
// decrypt
using var decrypted = new MemoryStream();
using var bfd = new BlowfishCtrDecryptStream(encrypted, codec, iv);
bfd.CopyTo(decrypted);
| type | mode | usage | works on | thread-safe |
|---|---|---|---|---|
| BlowfishEcb | ECB | Only when forced to. Lack of diffusion. Require padded original data. | buffer | yes |
| BlowfishCbc | CBC | Recommended. Require padded original data. | buffer | yes |
| BlowfishCtr | CTR | Recommended. Works without padding. | buffer | yes |
| ParallelBlowfishEcb | EBC | Only when forced to. Parallel computation from certain data size. Require padded original data. | buffer | yes |
| ParallelBlowfishCtr | CTR | Recommended. Parallel computation from certain data size. Works without padding. | buffer | yes |
| BlowfishCtrEncryptStream | CTR | Only when you need stream-sematics; otherwise you are better off with input buffer variants. | stream | no |
| BlowfishCtrDecryptStream | CTR | dtto | stream | no |