Key wrapping encrypts one key with another, called the key-encryption key (KEK). This example uses unpadded RFC 3394, not RFC 5649.
dotnet add package RecursiveAI.OneCrypto --prereleaseRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using System.Security.Cryptography; using RecursiveAI.OneCrypto.Primitives; byte[] kek = RandomNumberGenerator.GetBytes(32); byte[] key = RandomNumberGenerator.GetBytes(32); using var wrapper = new AesKeyWrap(kek); byte[] wrapped = new byte[key.Length + 8]; byte[] restored = new byte[key.Length]; wrapper.Wrap(key, wrapped); wrapper.Unwrap(wrapped, restored); Console.WriteLine(key.AsSpan().SequenceEqual(restored));
TrueUnzip the example, then dotnet run.
The full program
The program also rejects a modified result.
using System.Security.Cryptography; using RecursiveAI.OneCrypto.Primitives; byte[] kek = RandomNumberGenerator.GetBytes(32); byte[] key = RandomNumberGenerator.GetBytes(32); using var wrapper = new AesKeyWrap(kek); byte[] wrapped = new byte[key.Length + 8]; byte[] restored = new byte[key.Length]; wrapper.Wrap(key, wrapped); wrapper.Unwrap(wrapped, restored); Console.WriteLine(key.AsSpan().SequenceEqual(restored)); Console.WriteLine($"Wrapped: {wrapped.Length} bytes"); wrapped[0] ^= 1; Console.WriteLine($"Modified accepted: {wrapper.TryUnwrap(wrapped, restored)}"); CryptographicOperations.ZeroMemory(kek); CryptographicOperations.ZeroMemory(key); CryptographicOperations.ZeroMemory(restored);
True Wrapped: 40 bytes Modified accepted: False
Unwrap throws AuthenticationTagMismatchException and clears the destination when the integrity check fails; TryUnwrap returns false.
Variations
Wrap with a 128-bit KEK.
using System.Security.Cryptography; using RecursiveAI.OneCrypto.Primitives; byte[] kek = RandomNumberGenerator.GetBytes(16); byte[] key = RandomNumberGenerator.GetBytes(32); using var wrapper = new AesKeyWrap(kek); byte[] wrapped = new byte[key.Length + 8]; byte[] restored = new byte[key.Length]; wrapper.Wrap(key, wrapped); wrapper.Unwrap(wrapped, restored); Console.WriteLine(key.AsSpan().SequenceEqual(restored));
TrueRelated recipes
- Prepare and complete a detached signature in C#
- Sign and verify with Ed448 and a context in C#
- Read and write ASN.1 DER in C#
The short answer
Use AesKeyWrap.Wrap and Unwrap with separate buffers. The wrapped key is eight bytes longer than the original key data. This is unpadded RFC 3394; it is not the padded KWP format.
What is key wrapping in cryptography?
Key wrapping protects key data under a separate key-encryption key. RFC 3394 defines AES Key Wrap for this job. It is not a password-based encryption format: the constructor takes AES key bytes, not a password. Keep the KEK separately from the wrapped key; someone who obtains both can unwrap it.
The example generates both keys locally so you can run it without an account, a key server or a hardware device. It does not persist either key. In an application that needs to recover the wrapped key later, retain the original KEK through your existing key-management mechanism. Generating another random KEK at recovery time will not work.
How do I wrap and unwrap an AES key in C#?
Construct AesKeyWrap with the KEK, allocate a separate output buffer, then call Wrap. The output buffer must be exactly eight bytes longer than the input key data. Unwrapping needs the inverse size: allocate a destination eight bytes shorter than the wrapped value. The top snippet demonstrates both calls and compares the restored key with the original.
The AES modes documentation specifies KEKs of 16, 24 or 32 bytes. Key data must be a multiple of eight bytes, at least 16 bytes and at most 1 MiB. These are distinct lengths: a 32-byte KEK does not require 32-byte key data.
Neither in-place operation nor partially overlapping input and output is allowed for key wrap. Use independent arrays as shown. Dispose the wrapper after use, and do not share one instance between concurrent operations.
Is AES key wrap the same as AES-KWP?
No. This API implements RFC 3394 without padding. AES-KWP is the padded construction in RFC 5649, which the library documentation says is provided by the platform rather than duplicated in OneCrypto. If an external format requires KWP, this recipe’s output is not a replacement for it. Check the algorithm identifier in that format before choosing an API.
There is no nonce parameter on AesKeyWrap. Do not prepend a nonce or apply ordinary block-cipher padding to make an incompatible input fit. Choose the construction required by the receiving protocol instead.
How does RFC 3394 detect a modified wrapped key?
Unwrapping checks the recovered integrity block before accepting the result. The full program flips a ciphertext byte and calls TryUnwrap; it prints False. A failed check is not usable key material, even if a buffer happens to contain bytes. OneCrypto clears the destination on an integrity failure.
The throwing form reports an authentication-tag mismatch. Invalid sizes and overlapping buffers are argument errors rather than an authentication result. Handle those as programming or format errors, not as successful recovery with a warning. The integrity check does not manage KEK storage, identify the intended recipient or attach application metadata; those responsibilities remain with the surrounding key format or protocol.