bcrypt-pbkdf turns an OpenSSH key file’s passphrase into cipher key material.
dotnet add package RecursiveAI.OneCrypto --prereleaseRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneCrypto.Primitives; // Public OpenBSD test vector. byte[] result = BcryptPbkdf.DeriveKey( "password"u8, "salt"u8, 4, 32); string hex = Convert.ToHexString(result); Console.WriteLine(hex[..32]); Console.WriteLine(hex[32..]);
Unzip the example, then dotnet run.
5BBF0CC293587F1C3635555C27796598 D47E579071BF427E9D8FBE842ABA34D9
Full program
using RecursiveAI.OneCrypto.Primitives; // Public OpenBSD test vector. byte[] result = BcryptPbkdf.DeriveKey( "password"u8, "salt"u8, 4, 32); string hex = Convert.ToHexString(result); Console.WriteLine(hex[..32]); Console.WriteLine(hex[32..]); byte[] expected = Convert.FromHexString( "5BBF0CC293587F1C3635555C27796598" + "D47E579071BF427E9D8FBE842ABA34D9"); if (!result.AsSpan().SequenceEqual(expected)) throw new InvalidOperationException("bcrypt-pbkdf vector mismatch."); Console.WriteLine("OpenBSD vector: match.");
5BBF0CC293587F1C3635555C27796598 D47E579071BF427E9D8FBE842ABA34D9 OpenBSD vector: match.
The program compares the full 32-byte result with the OpenBSD bcrypt-pbkdf test vector. Only print public test material, never bytes derived from a real passphrase. OneCrypto rejects an empty salt or nonpositive round count; rounds above the configured bound throw KdfLimitExceededException.
Variations
Split the result for aes256-ctr. The cipher needs a 32-byte key followed by a 16-byte initialization vector (IV). Derive all 48 bytes in one call, then slice the result:
using System.Security.Cryptography; using RecursiveAI.OneCrypto.Primitives; // Sample only: use the salt and rounds from your file. byte[] material = BcryptPbkdf.DeriveKey( "password"u8, "salt"u8, 4, 32 + 16); ReadOnlySpan<byte> key = material.AsSpan(0, 32); ReadOnlySpan<byte> iv = material.AsSpan(32, 16); Console.WriteLine($"Key: {key.Length}; IV: {iv.Length}"); CryptographicOperations.ZeroMemory(material);
Key: 32; IV: 16Related recipes
The short answer
Use BcryptPbkdf.DeriveKey from RecursiveAI.OneCrypto.Primitives, the namespace supplied by the RecursiveAI.OneCrypto NuGet package. For an existing encrypted OpenSSH key file with kdfname set to bcrypt, pass the passphrase bytes, salt and rounds from the file’s KDF options, and the total output length required by its cipher. The result is symmetric cipher material, not the SSH private key itself.
Is OpenSSH bcrypt-pbkdf the same as a bcrypt password hash?
No. A bcrypt login hash is a stored value you check when a user enters a password. The private-key format calls its KDF bcrypt, but uses bcrypt-pbkdf to produce raw bytes for a cipher, not a bcrypt hash string. A bcrypt password verifier can’t substitute for the KDF or turn a hash into the original password. The OpenSSH format specification lists the KDF fields and how the output is used.
How do I read the bcrypt round count from an OpenSSH key file?
In the decoded openssh-key-v1 container, the header has ciphername, kdfname, and kdfoptions. If kdfname is bcrypt, kdfoptions contains an SSH string of salt bytes followed by an unsigned 32-bit round count. Read both values from that binary field, not from the PEM text. OneCrypto can open OpenSSH key files but doesn’t expose the salt or rounds through its public API. Use that integer directly as the rounds argument. Don’t read it as the exponent used in a bcrypt password hash. In this OneCrypto release, the default cap is 4096 rounds so an input file can’t request unbounded work.
How do I derive the encryption key and IV for OpenSSH?
Read the cipher name first. It tells you how many bytes to request and where to split them. For aes256-ctr, requesting 32 bytes and then another 16 with the same inputs does not recreate a 48-byte derivation. bcrypt-pbkdf distributes output from multiple internal blocks across the result, so even the first 32 bytes can differ when the requested length changes. Request the combined length and then split the result, as the variation does. OneCrypto’s OpenSSH fixture test uses that same sequence.