Hc128 Class
Definition
- Namespace
- Bodu.Security.Cryptography
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
- Source
- Hc128.cs
Provides a managed implementation of the HC-128 stream cipher specified by Hongjun Wu. This class cannot be inherited.
public sealed class Hc128 : SymmetricStreamAlgorithm, IDisposable
- Inheritance
-
Hc128
- Implements
- Inherited Members
- Extension Methods
Examples
using Bodu.Security.Cryptography;
using Bodu.Security.Cryptography.Extensions;
using var hc128 = new Hc128();
hc128.GenerateKey(); // 128-bit
hc128.GenerateNonce(); // 128-bit IV
byte[] ciphertext = hc128.Encrypt(plaintext);
byte[] roundTrip = hc128.Decrypt(ciphertext);
Remarks
HC-128 is a software-oriented additive stream cipher and a member of the eSTREAM software portfolio. It uses a 128-bit key and a 128-bit IV and drives two large secret tables to produce a keystream that is XORed with the plaintext. This class exposes the raw, confidentiality-only cipher with no authentication; for new designs prefer an AEAD construction.
Like the other stream ciphers in this library it is self-inverse: CreateEncryptor() and CreateDecryptor() are interchangeable. The IV is supplied through the Nonce property.
Parameters at a glance.
- Key size: 128 bits (16 bytes).
- Nonce (IV) size: 128 bits (16 bytes).
Nonce reuse is catastrophic. A given (key, IV) pair must encrypt at most one message;
reusing it reveals the XOR of the plaintexts. HC-128 has a relatively expensive per-instance initialization (it
warms up two 512-word tables), so reuse one configured instance across a single message rather than re-creating it
per block.
Constructors
Hc128()
Initializes a new instance of the Hc128 class with default parameters.
public Hc128()
Remarks
The default configuration uses a 128-bit key and a 128-bit IV.
Methods
Create()
Creates a new Hc128 instance with default parameters.
public static Hc128 Create()
Returns
CreateStreamCipher(byte[], byte[])
Builds a configured IStreamCipher engine from the validated key and nonce.
protected override IStreamCipher CreateStreamCipher(byte[] key, byte[] nonce)
Parameters
keybyte[]The key, already validated to the algorithm's key size.
noncebyte[]The nonce, already validated to the algorithm's nonce size.
Returns
- IStreamCipher
A new IStreamCipher engine positioned at the start of its keystream.
Remarks
Implementations receive a key and nonce whose lengths have already been checked by the base class, so they need only construct their engine. Ownership of the returned engine transfers to the caller.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |