Utilise Microsoft's Next Generation Cryptography (CNG) API in VBA

January 9, 2026 ยท View on GitHub

Title

This article series will show you how to utilise the Next Generation Cryptography (CNG) API from Microsoft for modern hashing and encrypting/decrypting, maintaining 100% compatibility with the implementation of CNG in .Net as well as PowerShell scripts.


Introduction

Purpose of these articles

The Microsoft CNG APIs constitute a collection of more than a dozen APIs that handle all the aspects and supporting functions to calculate hash values and perform encryption and decryption meeting modern high demands and standards.

This series details one method to implement these features in applications supported by VBA (Visual Basic for Applications), for example Microsoft Access and Microsoft Excel.

The theory for and the clever mathematics behind hashing and encryption will not be touched; this is covered massively in books and articles published over a long period of time by dedicated experts. A search with Bing or Google will return a long list of resources to study.

Sections

The series has been split in six parts. This allows you to skip parts you are either familiar with or wish to implement later if at all.

  1. Utilise Microsoft's Next Generation Cryptography (CNG) API in VBA
  2. Hashing in VBA using the Microsoft NG Cryptography (CNG) API
  3. Encryption in VBA using the Microsoft NG Cryptography (CNG) API
  4. Using binary storage to serve the Microsoft NG Cryptography (CNG) API
  5. Storing passwords in VBA using the Microsoft NG Cryptography (CNG) API
  6. HMAC in VBA using the Microsoft NG Cryptography (CNG) API

The three first deal and the last, as stated, with hashing and encrypting/decrypting based on the CNG API, while the fourth explains how to combine these techniques with the little known feature of Access, storing binary data, as this in many cases will represent the optimal storage method for hashed or encrypted data.

The fifth demonstrates how to save and verify passwords totally safe using these tools.

Credit

Based on code at Stack Overflow published 2021-04-28 (with later edits) by Stack Overflow user Erik A.

Documentation and demo

Official CNG API documentation:

Cryptography API: Next Generation

Top level code documentation generated by MZ-Tools is included for Microsoft Access.

Detailed documentation is in-line.

Full documentation can also be found here:

EE Logo

Next Generation Cryptography

Demo applications for Microsoft Access and Microsoft Excel tested with Microsoft 365 are located in the demos folder.


If you wish to support my work or need extended support or advice, feel free to: