Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

cryptopp-api-delphi

CryptoPP Delphi Wrapper

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

CryptoPP Delphi Wrapper

CryptoPP_Lib.pas + CryptoPP.pas give Delphi applications a high-level, TArray<Byte>-based API on top of the cryptopp.dll built from main.h/main.cpp in my other repo.

Files

  • CryptoPP_Lib.pas — thin, 1:1 cdecl external import declarations matching the DLL's exported C functions. Low-level: raw pointers (PByte/PAnsiChar), manual output-buffer sizing, manual delete_byte_array calls. You normally should not need to call this unit directly.
  • CryptoPP.pas — the high-level API. Wraps every DLL call with TArray<Byte> parameters/results, allocates/frees buffers for you, and exposes a single global Crypto: TCrypto record as the entry point.

Requirements

  • cryptopp.dll (built from the C++ side of this repo) must be present on the search path (same directory as the executable, or on PATH). The expected file name is defined by DllName = 'cryptopp.dll' in CryptoPP_Lib.pas.
  • The DLL bitness (x86/x64) must match your Delphi application.
  • System.SysUtils and System.Math (used for DER length handling in TECDSA).

Usage

Add both units to your project's uses clause (only CryptoPP is normally needed; it pulls in CryptoPP_Lib itself) and call through the global Crypto variable:

pascal
uses
  CryptoPP;

var
  Key, IV, PlainText, CipherText: TArray<Byte>;
begin
  Key := TEncoding.UTF8.GetBytes('0123456789abcdef'); // 16 bytes = AES-128
  IV := TEncoding.UTF8.GetBytes('abcdef9876543210');  // 16 bytes
  PlainText := TEncoding.UTF8.GetBytes('Hello, Crypto++!');

  CipherText := Crypto.AES.Cbc_Encrypt(PlainText, Key, IV);
  PlainText := Crypto.AES.Cbc_Decrypt(CipherText, Key, IV);
end;

API overview (Crypto: TCrypto)

Member Type Highlights
Crypto.AES TAES Cbc_*, Cfb_*, Ctr_*, Ecb_* (PKCS7/zero padding via ZerosPadding param), Gcm_* (16-byte tag handled internally).
Crypto.BigInteger TBigInteger Add, Modulus, ModPow, Multiply, Subtract on hex-string values.
Crypto.Blowfish TBlowfish Cbc_Decrypt/Cbc_Encrypt.
Crypto.Camellia TCamellia Cbc_Decrypt/Cbc_Encrypt.
Crypto.ChaCha20 TChaCha20 Decrypt/Encrypt (stream cipher, key/IV sizes per algorithm).
Crypto.DiffieHellman TDiffieHellman KeyPair(p, g), SharedKey(...) using hex p/g.
Crypto.ECDH TECDH KeyPair(EllipticCurve), SharedKey(...).
Crypto.ECDSA TECDSA KeyPair, ExportPublicKey, Sign('sha1'|'sha256', ...), Verify(...). Produces/consumes standard DER-encoded signatures (converted internally to/from Crypto++'s raw format).
Crypto.ED25519 TED25519 KeyPair, ExportPublicKey, Sign, Verify. Public/private keys are exposed as raw 32-byte values (PKCS8/X.509 DER wrapping is added/stripped internally).
Crypto.Hash THash MD2, MD4, MD5, Poly1305_Tls.
Crypto.PBKDF2 TPBKDF2 HmacSha1/256/512(Password, Salt, Iterations, ResultSize).
Crypto.RSA TRSA KeyPair, ExportPublicKey, Encrypt/Decrypt (PKCS#1v1.5), NoPadding_*, Oaep_Encrypt/Decrypt('md2'..'sha512', ...), Sign/Verify (PKCS#1v1.5), Pss_Sign/Pss_Verify.
Crypto.Salsa20 TSalsa20 Decrypt/Encrypt.
Crypto.XSalsa20 TXSalsa20 Decrypt/Encrypt, plus AEAD Poly1305_Tls_Encrypt/Poly1305_Tls_Decrypt.
Crypto.X25519 TX25519 KeyPair, SharedKey. Raw 32-byte keys.

Hash method strings

ECDSA.Sign/Verify and RSA.Sign/Verify/Oaep_*/Pss_* take a HashMethod: string selecting the underlying digest. Matching is case-insensitive (HashMethod.ToLower()). Supported values depend on the operation:

  • ECDSA sign/verify: 'sha1', 'sha256'.
  • RSA OAEP: 'md2', 'md4', 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'.
  • RSA PSS: 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'.
  • RSA PKCS#1v1.5 sign/verify: 'md2', 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'.

An unrecognized HashMethod results in an empty TArray<Byte> (for sign/encrypt operations) or False (for verify operations) — it will not raise an exception, so check the result size/value before use.

AEAD verification (TXSalsa20.Poly1305_Tls_Decrypt)

pascal
function Poly1305_Tls_Decrypt(const CipherData: TArray<Byte>; const Key: TArray<Byte>;
  const IV: TArray<Byte>; out Verified: Boolean; const Verify: Boolean = True): TArray<Byte>;

Always inspect the Verified out-parameter. When Verify = True (the default) and the Poly1305 tag does not match, Verified is False and the returned plaintext buffer is all zero bytes — do not use the returned data unless Verified is True.

Elliptic curve selector

ECDH/ECDSA take an EllipticCurve: Byte parameter:

Value Curve
0 (default) secp256k1
1 secp256r1
2 secp384r1
3 secp521r1

Memory management

All high-level functions in CryptoPP.pas manage the DLL-allocated buffers for you (calling delete_byte_array internally) — you only ever work with regular Delphi TArray<Byte> values with normal ARC/refcount lifetime. You do not need to call anything in CryptoPP_Lib.pas directly unless you are extending the wrapper.