Show / Hide Table of Contents

KeySizeBits Method

KeySizeBits(PivAlgorithm)

The size of a key, in bits, of the given algorithm.

C#
public static int KeySizeBits(this PivAlgorithm algorithm)

Parameters

Type Name Description
PivAlgorithm algorithm

The algorithm name to check.

Returns

int

An int, the size, in bits, of a key of the given algorithm.

Remarks

The PivAlgorithm enum specifies algorithm and key size for RSA and ECC. If you have a variable of type PivAlgorithm, use this extension to get the bit size out.

For example, suppose you obtain a public key from storage, and have a PivPublicKey object. Maybe your code performs different tasks based on the key size (e.g. use SHA-256 or SHA-384, or build a buffer for signing). You can look at the Algorithm property to learn the algorithm and key size. However, if all you want is the key size, use this extension:

PivPublicKey publicKey = SomeClass.GetPublicKey(someSearchParam);
byte[] buffer = new byte[publicKey.Algorithm.KeySizeBits() / 8];

This will return the following values for each value of PivAlgorithm.

Rsa1024    1024
Rsa2048    2048
Rsa3072    3072
Rsa4096    4096
EccP256     256
EccP384     384
EccP521     521
EccEd25519  256
EccX25519   256
Aes128      128
Aes192      192
Aes256      256
TripleDes   192
Pin          64
None          0
Note that a Triple-DES key is made up of three DES keys, and each DES key is 8 bytes (64 bits). However, because there are 8 "parity bits" in each DES key, the actual key strength of a DES key is 56 bits. That means the actual key strength of a Triple-DES key is 168 bits. In addition, because of certain attacks, it is possible to reduce the strength of a Triple-DES key to 112 bits (it takes the equivalent of a 112-bit brute-force attack to break a Triple-DES key). Nonetheless, this extension will return 192 as the key length, in bits, of a Triple-DES key.

A PIN or PUK is 6 to 8 bytes long. Hence, the maximum size, in bits, of a PivAlgorithm.Pin is 64.

In this article
Back to top Generated by DocFX