---
title: "OpenSSH bcrypt-pbkdf key derivation in C#"
url: https://recursiveai.org/onecrypto/cookbook/derive-openssh-key-with-bcrypt-pbkdf/
description: "Run the OpenSSH bcrypt-pbkdf key derivation in C#. Check an OpenBSD test vector and derive the key and IV an encrypted OpenSSH key file needs."
version: "0.4.0-preview.2"
---

# OpenSSH bcrypt-pbkdf key derivation in C#

**OneCrypto** is a free, open-source cryptography library for .NET from Recursive AI. MIT or Apache-2.0, preview, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCrypto) · [GitHub](https://github.com/recursive-ai-oss/onecrypto-csharp) · [Docs](https://recursiveai.org/onecrypto/docs/) · [Download example (.zip)](https://recursiveai.org/onecrypto/cookbook/derive-openssh-key-with-bcrypt-pbkdf/derive-openssh-key-with-bcrypt-pbkdf.zip)

bcrypt-pbkdf turns an OpenSSH key file’s passphrase into cipher key material.

```shell
dotnet add package RecursiveAI.OneCrypto --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onecrypto/cookbook/derive-openssh-key-with-bcrypt-pbkdf/derive-openssh-key-with-bcrypt-pbkdf.zip), then `dotnet run`.

```text
5BBF0CC293587F1C3635555C27796598
D47E579071BF427E9D8FBE842ABA34D9
```

## Full program

```csharp
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.");
```

```text
5BBF0CC293587F1C3635555C27796598
D47E579071BF427E9D8FBE842ABA34D9
OpenBSD vector: match.
```

The program compares the full 32-byte result with the [OpenBSD bcrypt-pbkdf test vector](https://github.com/openbsd/src/blob/master/regress/lib/libutil/bcrypt_pbkdf/bcrypt_pbkdf_test.c#L35-L39). 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:

```csharp
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);
```

```text
Key: 32; IV: 16
```

## Related recipes

- [Generate an SSH key](https://recursiveai.org/onecrypto/cookbook/generate-ssh-key/)
- [Derive an encryption key with scrypt](https://recursiveai.org/onecrypto/cookbook/derive-key-with-scrypt/)
- [Hash and verify passwords with Argon2id](https://recursiveai.org/onecrypto/cookbook/argon2-password-hashing/)

## 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](https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key) 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](https://recursiveai.org/onecrypto/docs/pkcs/) 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](https://recursiveai.org/onecrypto/docs/kdf/) 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](https://github.com/recursive-ai-oss/onecrypto-csharp/blob/d7fde2e944b89d5a6f0b5b1402c62dbadbf10039/tests/OneCrypto.Tests/Primitives/BcryptPbkdfTests.cs#L29-L46) uses that same sequence.
