Function List Functions by Category
The CryptoSysTM API gives you the ability to call fast, efficient cryptographic functions from Visual Basic projects and VBA applications like Access, Excel and Word. It provides four of the major block cipher algorithms, two secure hash algorithms, the HMAC message authentication algorithm, and a secure random number generator.
The block cipher algorithms are the original Data Encryption Standard (DES); its currently approved replacement, Triple DES (TDEA); Bruce Schneier's Blowfish; and the Rijndael Block Cipher, which is the new Advanced Encryption Standard (AES) chosen by the US National Institute of Standards and Technology (NIST) . The one-way hash functions are the Secure Hash Algorithm (SHA-1) and the new SHA-256 version. The random number generator is based on Peter Gutmann's paper Software Generation of Practically Strong Random Numbers [GUTM] and complies with the latest Federal Information Processing Standards Publication FIPS PUB 140-2 Security Requirements for Cryptographic Modules [FIPS140].
The CryptoSys API functions allow you to encrypt, decrypt and hash data in a variety of formats, as well as generating secure random keys to use in your applications. Your input data can be in a byte array, a hexadecimal string, or a file. The functions can be called to process the data in a one-off manner, or you can call them block-by-block after initialising with the key. The cipher algorithms work in both Electronic Codebook (ECB) and Cipher Block Chaining (CBC) modes. You can generate random keys in a secure manner.
It's assumed that you are familiar with the basics of cryptography and can program in Visual Basic to a reasonably advanced standard. For a good introduction to the principles of cryptography refer to Bruce Schneier's Applied Cryptography [SCHN] or, for a more advanced treatment, see Handbook of Applied Cryptography by Menezes, van Oorschot and Vanstone [MENE]. For a detailed explanation on accessing DLLs from Visual Basic, see the Access 97/2000 Developer's Handbook by Getz, Litwin and Gilbert [GE97] [GE2K].
The API functions in the CryptoSys package are called in a similar manner to the way you call Windows API functions from Visual Basic. They have the advantage of much greater speed and better security than interpreted Visual Basic code. You need to install the file on the client computer and declare the functions in your VB project (just copy and paste into a module and it's done). A full list of the declarations is provided.
The CryptoSysTM API system and this document were written by David Ireland and are copyright (c) 2001 by DI Management Services Pty Limited, all rights reserved.
The Personal version of CryptoSys API system may be used for personal use and for reasonable testing purposes. Use for commercial or non-personal purposes requires the purchase of the Commercial version and a valid licence which may be obtained from DI Management at <www.di-mgt.com.au>. The program executables and this document may not be distributed or reproduced separately by any means without the express written permission of the author. The latest Personal version of CryptoSys may be obtained from <http://www.di-mgt.com.au/crypto.html>.
All block cipher algorithms operate on a fixed-length block of data to produce a seemingly-random output of the same size. The security of the encryption process depends on a secret key, the length of which depends on the particular algorithm. It should be impossible (strictly, computationally infeasible) to derive the plaintext from the resultant ciphertext without knowing the key.
In Electronic Codebook (ECB) mode, each block is encrypted independently. In Cipher Block Chaining (CBC) mode, an initialisation vector (IV) is added to the first block of plaintext before encryption and the resultant ciphertext is added to the next block of plaintext before encryption, and so on. Decryption is the reverse process. The IV does not need to be kept secret.
All block ciphers require their input to be an exact multiple of the block length. Any odd bytes need to be padded to the next multiple. For encrypting files, CryptoSys adds a padding string using the convention from RFC 2630 [RFC2630].
The block and key lengths supported in the CryptoSys API package are as follows:
Algorithm | Block length | Key length |
---|---|---|
DES | 8 bytes (64 bits) | 8 bytes (only 56 bits used) |
Triple DES (TDEA) | 8 bytes (64 bits) | 24 bytes (only 168 bits used) |
Blowfish | 8 bytes (64 bits) | up to 56 bytes (variable up to 448 bits) |
Rijndael AES | 16 or 24 or 32 bytes (128/192/256 bits) | 16 or 24 or 32 bytes (128/192/256 bits) |
A hash function is a cryptographic primitive used for digital signatures and password protection. It maps a message of arbitrary length to a fixed-length hash value or "message digest". A cryptographic hash should be one-way and collision-resistant. "One-way" means that, given an n-bit hash value, it should require work equivalent to about 2^n hash computations to find any message that hashes to that value. "Collision-resistant" means that finding any two messages which hash to the same value should require work equivalent to about 2^n/2 hash computations. In other words, it should be computationally infeasible to find the original message from the digest or to create another message that produces the same result.
SHA-1 is a 160-bit (20-byte) hash function specified in FIPS PUB 180-1 Secure Hash Standard [FIPS180].
SHA-256 is the new draft standard intended as a companion for the new Advanced Encryption Standard (AES) to provide a similar level of enhanced security. SHA-256 is a 256-bit (32-byte) hash and is meant to provide 128 bits of security against collision attacks.
Most cryptographic procedures require random numbers. Many security applications have failed or been severely compromised because their random number generators failed to be sufficiently random. In particular, the "rand" functions provided with popular programming packages like Visual Basic, C and Perl are not secure from a cryptographic point of view. For more details and examples see [GUTM].
Many procedures use a random session key to encrypt the body of the message. If this key is ever compromised - because the random numbers are predictable or can be manipulated before being generated - an opponent who has had access to your encrypted messages can decipher them at his leisure.
The design rules adopted in CryptoSys provide random numbers that are "practically strong", i.e. as close to the usual definition of cryptographically strong as is practically possible. The system derives a key using repeated applications of the SHA-1 hash function on a pool of random data in an algorithm equivalent to the ANSI X9.17 key generator. It creates and maintains a "pool of randomness" from a variety of changing operating system parameters including the current time, system clock, current window sizes and handles, memory state, hard disk usage, and other variables. At the programmer's option, the pool can be updated by the user immediately before generating a key with whatever secret or random keys the user wishes to type, together with samples of mouse movements and timings between key strokes.
CryptoSys protects the location of its randomness pool by constantly moving and concealing the data in memory. It uses SHA-1 to mix the pool thoroughly several times before generating a key.
Keys for DES and Triple-DES are generated with the odd parity bits set and weak and semi-weak keys are checked for and rejected.
Failsafe routines are built into CryptoSys in accordance with Federal Information Processing Standards Publication FIPS PUB 140-2 Security Requirements for Cryptographic Modules [FIPS140] whereby the system carries out a power-up statistical test on the first 20,000 bits generated and then carries out a continuous test on samples of subsequently-generated bytes to ensure nothing untoward is happening. If either of these tests fail, the system will stop and display a warning message as per the standard.
If you don't trust the system-generated key, you can always just mix and hash the key with SHA-1 a few more times.
CryptoSys also lets you generate nonces - no, nothing to do with certain types of prison inmates - but a term used in security engineering meaning "number used once". Use a nonce where random but not-necessarily-unpredictable* numbers are required: e.g. for initialisation vectors, SSL cookies and random padding data. The principle here is that you should not compromise your important secure key generator by providing an opponent with any potentially useful information about what numbers it is generating. CryptoSys follows Peter Gutmann's guidelines from his paper [GUTM].
*Note that the nonce generator still passes the FIPS140-2 statistical tests for randomness [FIPS140].
Carrying out cryptographic functions in Visual Basic is complicated by the
need to use non-printable characters. When you encrypt your plaintext message
"We attack at dawn",
the ciphertext will consist of characters that won't display or print properly.
Out of the 256 possible values of a byte, only 95 can be printed. You can store all these
256 values in a VB String
type, and access their values with the Asc()
and Mid()
functions. This is OK within a VB project but, unfortunately,
you cannot guarantee that
these strings will be passed properly
to a DLL function. To complicate matters, VB normally stores strings internally as Unicode using
two bytes for each character.
One solution is to use the Byte
type, which was introduced especially to
deal with this issue. The cryptographic operations all operate on arrays of Bytes
.
You need to convert plaintext strings to an array of bytes. For example, the four-character
String "abc." can be represented by the four-byte array {97, 98, 99, 46}.
Note that byte arrays, unlike strings, must be dimensioned before use.
Another solution is to represent the ascii values as a hexadecimal string. The string "abc." is represented by another string "6162632E", where 61, 62, 63, 2E are the values represented in hexadecimal. Each ascii character is represented as two hexadecimal digits between &H00 and &HFF. This method is particularly useful for carrying out validation tests on the cryptographic functions, because the tests are usually specified as hexadecimal strings.
The functions in
CryptoSys
allow you to use either Byte mode or Hex string mode. The block cipher and hash functions
also work directly on files. To convert back-and-forth between String
types
and Byte
arrays, use the standard (but not well-known)
StrConv
function.
Dim i As Long Dim x() As Byte Dim str As String ' Convert string to ANSI byte array x = StrConv("abcdef", vbFromUnicode) For i = 0 To UBound(x) Debug.Print x(i); Next Debug.Print ' Convert back to a string str = StrConv(x(), vbUnicode) Debug.Print strThis should produce the results:
97 98 99 100 101 102 abcdef
Note how the StrConv
function avoids the need to declare the size of the byte array beforehand.
The VB module basByteUtils
included with the
CryptoSys
package includes functions to create and convert strings of hexadecimal digits
from byte arrays and normal strings.
Use the setup.exe
file to install (and uninstall)
CryptoSys onto your computer.
To call from a Visual Basic project or VBA application, just copy and paste the
necessary declaration statements to a basic module in your application.
A full list is provided in the module basCryptoSys.txt
.
You can then call the functions you require
as you would a normal VB function. You do not make a reference to it from your project.
Make sure you use the correct variable types.
Byte
arrays must be at least the required length.
Some functions have a pre-specified length
(e.g. the key for a DES function is always 8 bytes long),
others require the user to specify the length (e.g. keys for Blowfish).
String
variable must have the
result string already filled out to the required length.
Using an empty string may cause a fatal error.
To create a string of length 40 characters, you could do one of the following:
Dim sDigest As String * 40or
Dim sDigest As String sDigest = String(40, " ")
_Init
functions return zero
if an error occurs. Always check the return value. Do not change the value of
the context handle.
DES and Triple DES comply with:
AES complies with:
The SHA algorithms comply with:
The random number key generator complies with:
There are over 80 functions in the CryptoSys API package. Although this may seem daunting, there are only a few generic block cipher functions. The exact parameters of the functions may vary slightly depending on the particular block cipher, but the basic principles are the same. The generic functions are as follows:
_Hex
- en/decrypt a hex string in one step
_HexMode
- en/decrypt a hex string in one step using specified mode
_Bytes
- en/decrypt a byte array in one step
_BytesMode
- en/decrypt a byte array in one step using specified mode
_File
- en/decrypt a file
_FileHex
- en/decrypt a file using a hex key
_Init
- initialise the context ready for repeated use
_InitHex
- initialise the context using hex values ready for repeated use
_Update
- en/decrypt the next set of data and update the context
_UpdateHex
- en/decrypt the next set of data in hex and update the context
_Final
- close the context
_Ecb
- use existing key schedule to en/decrypt data in ECB mode
_EcbHex
- use existing key schedule to en/decrypt hex data in ECB mode
_Hex
is the simplest. It uses hexadecimal strings to represent the
data and the key. It encrypts or decrypts in one step in Electronic Codebook (EBC) mode.
There is no need to specify the length of the strings. Odd trailing characters are ignored.
Invalid characters will return an error.
Examples:
lngRet = BLF_Hex(sOutput, "0123456789ABCDEF", "FEDCBA9876543210", True)
will use the Blowfish algorithm to encrypt the plaintext represented in hex by "0123456789ABCDEF" using the key 0xFEDCBA9876543210.
lngRet = BLF_Hex(sOutput, "0ACEAB0FC6A0A28D", "FEDCBA9876543210", False)
will decrypt the ciphertext "0ACEAB0FC6A0A28D" using the same key.
_HexMode
is a one-step function like _Hex
except it allows
the user to specify the mode of operation. In the current version, ECB and CBC modes
are supported.
Examples:
lngRet = BLF_HexMode(sOutput, "37363534333231204E6F77206973207468652074696D6520666F722000000000", "0123456789ABCDEFF0E1D2C3B4A59687", True, "CBC", "FEDCBA9876543210")
will use the Blowfish algorithm to encrypt the plaintext "7654321 Now is the time for " padded with 4 zeroes represented in hex using the key 0x0123456789ABCDEFF0E1D2C3B4A59687 in Cipher Block Chaining (CBC) mode with Initialisation Vector 0xFEDCBA9876543210.
lngRet = BLF_HexMode(sOutput, "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC", "0123456789ABCDEFF0E1D2C3B4A59687", False, "CBC", "FEDCBA9876543210")
will decrypt the ciphertext 0x6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC using the same key and IV in the same mode.
_Bytes
is a one-step function like _Hex
except it operates
using arrays of bytes instead of hex strings. The user must specify the sizes
of the arrays.
_BytesMode
is a one-step function like _Bytes
that allows the user
to specify the mode of operation.
_File
encrypts or decrypts a file. The user specifies the filenames, the key,
the direction and mode of operation, and, if necessary, an initialisation vector.
The key and IV are passed as arrays of bytes.
A new file is created, overwriting any existing file. The RFC 2630 method of padding is
used as specified in Section 6.3 of [RFC2630].
_FileHex
is the same as _File
except the key is specified
as a hexadecimal string.
Example:
lngRet = BLF_FileHex("C:\hello.enc", "C:\hello.txt", "fedcba9876543210", True, "ECB", vbNullString)
will use the Blowfish algorithm to encrypt the file C:\hello.txt in ECB mode using the key 0xfedcba9876543210.
_Init
initialises the context by setting the
key, the direction (encrypt or decrypt), the mode of operation, and the
initialisation vector (IV).
Subsequent operations only need pass the data to be processed and the handle to the context.
_Init
requires the key and IV to be specified as arrays of bytes.
Depending on the particular block cipher algorithm, if the key or IV can be of variable length,
the user must specify the size; otherwise the sizes are assumed.
_InitHex
is the same as _Init
except the key and IV are specified
as hexadecimal strings. Note that _InitHex
and _Init
both
achieve the same result.
_Update
processes another block of data using the parameters set up by
_Init
or _InitHex
. The _Update
function overwrites
the input data with the output. In CBC mode, the IV is updated.
The data is passed as an array of bytes and the user
must specify how many bytes are in the array. The data may be any multiple of the block size.
_UpdateHex
is the same as _Update
except the data is passed
as a hexadecimal string. There is no need to specify its size. Note that
_UpdateHex
and _Update
may both use the same
context set by an earlier call to either _Init
or _InitHex
.
_Final
closes the context parameters set up by an earlier
call to _Init
or _InitHex
. All traces of the key schedule
are cleared.
_Ecb
allows the user to "sneak in" and encrypt or decrypt a block of data using
the key schedule already set up by an earlier call to
_Init
or _InitHex
. The _Ecb
operation is carried out
in ECB mode regardless of the parameters used in the Init call. The state of the initialisation
vector is unaffected.
_EcbHex
is the same as _Ecb
except the data is specified
as a hexadecimal string instead of an array of bytes.
Blowfish can have a key of variable length from one to 56 bytes.
The Byte
variants need you to specify the key length, but the
Hex
variants will use whatever length is provided in the key hex
string.
The block and IV are always 8 bytes long.
BLF_Hex
BLF_HexMode
BLF_Bytes
BLF_BytesMode
BLF_File
BLF_FileHex
BLF_Init
BLF_InitHex
BLF_Update
BLF_UpdateHex
BLF_Ecb
BLF_EcbHex
BLF_Final
Functions included for compatibilty with di_Blowfish.dll:
bf_StringEnc
- see BLF_Bytes
bf_StringDec
- see BLF_Bytes
bf_FileEnc
- see BLF_File
bf_FileDec
- see BLF_File
bf_Init
- see BLF_Init
bf_BlockEnc
- see BLF_Ecb
bf_BlockDec
- see BLF_Ecb
bf_Final
- see BLF_Final
The key for DES is always 8 bytes long; the block length is 8 bytes.
There is no need to specify the key length when calling
the DES functions, but the input variables must be long enough.
The parity bits in the key are ignored.
There are no _Ecb
functions provided for DES or TDEA because the
key schedule is set up differently for encrypt and decrypt modes.
DES_Hex
DES_HexMode
DES_Bytes
DES_BytesMode
DES_File
DES_FileHex
DES_Init
DES_InitHex
DES_Update
DES_UpdateHex
DES_Final
The key for TDEA is always 24 bytes long; the block length is 8 bytes.
There is no need to specify the key length when calling
the TDEA functions, but the input variables must be long enough.
The parity bits in the key are ignored.
There are no _Ecb
functions provided for TDEA.
TDEA_Hex
TDEA_HexMode
TDEA_Bytes
TDEA_BytesMode
TDEA_File
TDEA_FileHex
TDEA_Init
TDEA_InitHex
TDEA_Update
TDEA_UpdateHex
TDEA_Final
The AES functions, for consistency, all require the user to specify both the key length and the block length in bits. All combinations of 128 or 192 or 256 bits for the key length and block length are permitted. The data must be provided in sufficient length to match the key and block lengths specified.
AES_Hex
AES_HexMode
AES_Bytes
AES_BytesMode
AES_File
AES_FileHex
AES_Init
AES_InitHex
AES_Update
AES_UpdateHex
AES_Ecb
AES_EcbHex
AES_Final
All the SHA-1 functions output the message digest as a String
in hexadecimal format. You must set the length of the output string to a minimum of
40 characters before calling the digest functions.
See Calling the DLL Functions
for instructions on how to do this.
SHA1_StringHexHash
SHA1_BytesHexHash
SHA1_FileHexHash
SHA1_Init
SHA1_AddBytes
SHA1_AddString
SHA1_HexDigest
SHA1_Reset
SHA1_Hmac
The SHA-256 functions are identical in syntax and usage to their SHA-1 equivalents, except the string to receive the message digest must be set to a minimum of 64 characters. See Calling the DLL Functions.
SHA2_StringHexHash
SHA2_BytesHexHash
SHA2_FileHexHash
SHA2_Init
SHA2_AddBytes
SHA2_AddString
SHA2_HexDigest
SHA2_Reset
SHA2_Hmac
RAN_Seed
function, but cannot
control other seeding, mixing and hiding of the pool carried out automatically by the system.
The DES and TDEA key generators check for weak and semi-weak keys and set the parity bits
(which are subsequently ignored anyway by the DES block cipher functions!).
RAN_KeyGenerate
RAN_KeyGenHex
RAN_DESKeyGenerate
RAN_DESKeyGenHex
RAN_TDEAKeyGenerate
RAN_TDEAKeyGenHex
RAN_Seed
RAN_Test
RAN_Nonce
RAN_NonceHex
RAN_Long
AES_Bytes
AES_BytesMode
AES_Ecb
AES_EcbHex
AES_File
AES_FileHex
AES_Final
AES_Hex
AES_HexMode
AES_Init
AES_InitHex
AES_Update
AES_UpdateHex
BLF_Bytes
BLF_BytesMode
BLF_Ecb
BLF_EcbHex
BLF_File
BLF_FileHex
BLF_Final
BLF_Hex
BLF_HexMode
BLF_Init
BLF_InitHex
BLF_Update
BLF_UpdateHex
DES_Bytes
DES_BytesMode
DES_File
DES_FileHex
DES_Final
DES_Hex
DES_HexMode
DES_Init
DES_InitHex
DES_Update
DES_UpdateHex
RAN_DESKeyGenerate
RAN_DESKeyGenHex
RAN_KeyGenerate
RAN_KeyGenHex
RAN_Long
RAN_Nonce
RAN_NonceHex
RAN_Seed
RAN_TDEAKeyGenerate
RAN_TDEAKeyGenHex
RAN_Test
SHA1_AddBytes
SHA1_AddString
SHA1_BytesHexHash
SHA1_FileHexHash
SHA1_HexDigest
SHA1_Hmac
SHA1_Init
SHA1_Reset
SHA1_StringHexHash
SHA2_AddBytes
SHA2_AddString
SHA2_BytesHexHash
SHA2_FileHexHash
SHA2_HexDigest
SHA2_Hmac
SHA2_Init
SHA2_Reset
SHA2_StringHexHash
TDEA_Bytes
TDEA_BytesMode
TDEA_File
TDEA_FileHex
TDEA_Final
TDEA_Hex
TDEA_HexMode
TDEA_Init
TDEA_InitHex
TDEA_Update
TDEA_UpdateHex
BLF_Hex
encrypts or decrypts data
represented as a hexadecimal string
using a key represented in hexadecimal notation.
The process is carried out in one step in Electronic Codebook (EBC) mode.
Public Declare Function BLF_Hex Lib "diCryptoSys.dll"
(ByVal sOutput As String, ByVal sInput As String, ByVal sKey As String,
ByVal bEncrypt As Boolean) As Long
lngRet = BLF_Hex(sOutput, sInput, sKey, bEncrypt)
sOutput | String of sufficient length to receive the output. |
sInput | String containing the input data in hexadecimal. |
sKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sKey As String Dim sCorrect As String sInput = "0123456789ABCDEF" sKey = "FEDCBA9876543210" sCorrect = "0ACEAB0FC6A0A28D" ' Set sOutput to be same length as sInput sOutput = String(Len(sInput), " ") Debug.Print "KY="; sKey Debug.Print "PT="; sInput ' Encrypt in one-off process lngRet = BLF_Hex(sOutput, sInput, sKey, True) Debug.Print "CT="; sOutput Debug.Print "OK="; sCorrect ' Now decrypt back to plain text sInput = sOutput lngRet = BLF_Hex(sOutput, sInput, sKey, False) Debug.Print "P'="; sOutputThis should result in output as follows:
KY=FEDCBA9876543210 PT=0123456789ABCDEF CT=0ACEAB0FC6A0A28D OK=0ACEAB0FC6A0A28D P'=0123456789ABCDEFThe following example uses the same string for the input and output and removes the need to "prepare" the size of the output string.
Dim lngRet As Long Dim sHexData As String sHexData = "0001020304050607f8f9fafbfcfdfeff" Debug.Print "PT="; sHexData ' Encrypt in one-off process lngRet = BLF_Hex(sHexData, sHexData, "FEDCBA9876543210", True) Debug.Print "CT="; sHexData, lngRet Debug.Print "OK="; "7E9BD79A5E67E207CBBB647B9AE18273" ' Now decrypt back to plain text lngRet = BLF_Hex(sHexData, sHexData, "FEDCBA9876543210", False) Debug.Print "P'="; sHexData, lngRetThis should result in output as follows:
PT=0001020304050607f8f9fafbfcfdfeff CT=7E9BD79A5E67E207CBBB647B9AE18273 0 OK=7E9BD79A5E67E207CBBB647B9AE18273 P'=0001020304050607F8F9FAFBFCFDFEFF 0
BLF_HexMode
encrypts or decrypts data
represented as a hexadecimal string
using a specified mode. The key and initialisation vector
are represented as a hexadecimal string.
Public Declare Function BLF_HexMode Lib "diCryptoSys.dll"
(ByVal sOutput As String, ByVal sInput As String,
ByVal sHexKey As String, ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sHexIV As String) As Long
lngRet = BLF_HexMode(sOutput, sInput, sHexKey, bEncrypt, sMode, sHexIV)
sOutput | String of sufficient length to receive the output. |
sInput | String containing the input data in hexadecimal. |
sHexKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector (IV)
in hexadecimal, or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sHexKey As String Dim sHexIV As String Dim sCorrect As String ' "7654321 Now is the time for " padded to 32 bytes with 4 nulls sInput = "37363534333231204E6F77206973207468652074696D6520666F722000000000" sCorrect = "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC" sHexKey = "0123456789ABCDEFF0E1D2C3B4A59687" sHexIV = "FEDCBA9876543210" ' Set sOutput to be same length as sInput sOutput = String(Len(sInput), " ") Debug.Print "KY="; sHexKey Debug.Print "IV="; sHexIV Debug.Print "PT="; sInput ' Encrypt in one-off process lngRet = BLF_HexMode(sOutput, sInput, sHexKey, True, "CBC", sHexIV) Debug.Print "CT="; sOutput, lngRet Debug.Print "OK="; sCorrect ' Now decrypt back to plain text sInput = sOutput lngRet = BLF_HexMode(sOutput, sInput, sHexKey, False, "CBC", sHexIV) Debug.Print "P'="; sOutput, lngRetThis should result in output as follows:
KY=0123456789ABCDEFF0E1D2C3B4A59687 IV=FEDCBA9876543210 PT=37363534333231204E6F77206973207468652074696D6520666F722000000000 CT=6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC 0 OK=6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC P'=37363534333231204E6F77206973207468652074696D6520666F722000000000 0
BLF_Bytes
encrypts or decrypts an array of Bytes
in one step in Electronic Codebook (EBC) mode.
Public Declare Function BLF_Bytes Lib "diCryptoSys.dll"
(aResult As Any, aData As Any, ByVal lngDataLen As Long,
aKey As Any, ByVal lngKeyLen As Long, ByVal bEncrypt As Boolean) As Long
lngRet = BLF_Bytes(aResult(0), aData(0), lngDataLen, aKey(0), lngKeyLen, bEncrypt)
aResult | Byte array of sufficient length to receive the output. |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
aKey | Byte array containing the key. |
lngKeyLen | Long equal to length of the key in bytes. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim aData(15) As Byte ' NB zero-based = 16 bytes Dim aResult(15) As Byte Dim aKey(7) As Byte Dim lngRet As Long Dim lngKeyLen As Long Dim lngDataLen As Long Dim sCorrect As String ' Convert hex strings to byte arrays lngKeyLen = bu_HexStr2Bytes("FEDCBA9876543210", aKey) lngDataLen = bu_HexStr2Bytes("0123456789ABCDEF0123456789ABCDEF", aData) sCorrect = "0ACEAB0FC6A0A28D0ACEAB0FC6A0A28D" Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "PT="; bu_Bytes2HexStr(aData, lngDataLen) ' Encrypt in one-off process lngRet = BLF_Bytes(aResult(0), aData(0), lngDataLen, aKey(0), lngKeyLen, True) Debug.Print "CT="; bu_Bytes2HexStr(aResult, lngDataLen) Debug.Print "OK="; sCorrect ' Now decrypt back to plain text lngRet = BLF_Bytes(aData(0), aResult(0), lngDataLen, aKey(0), lngKeyLen, False) Debug.Print "P'="; bu_Bytes2HexStr(aData, lngDataLen)This should result in output as follows:
KY=FEDCBA9876543210 PT=0123456789ABCDEF0123456789ABCDEF CT=0ACEAB0FC6A0A28D0ACEAB0FC6A0A28D OK=0ACEAB0FC6A0A28D0ACEAB0FC6A0A28D P'=0123456789ABCDEF0123456789ABCDEF
BLF_BytesMode
encrypts or decrypts an array of Bytes
using a specified mode.
Public Declare Function BLF_BytesMode Lib "diCryptoSys.dll"
(aResult As Any, aData As Any, ByVal lngDataLen As Long,
aKey As Any, ByVal lngKeyLen As Long, ByVal bEncrypt As Boolean,
ByVal sMode As String, aInitV As Any) As Long
lngRet = BLF_BytesMode(aResult(0), aData(0), lngDataLen, aKey(0),
lngKeyLen, bEncrypt, sMode, aInitV(0))
aResult | Byte array of sufficient length to receive the output. |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
aKey | Byte array containing the key. |
lngKeyLen | Long equal to length of the key in bytes. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte containing the
initialisation vector (IV), or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
bu_HexStr2Bytes
and bu_Bytes2HexStr
utilities
to convert test hex strings into byte arrays and display the results.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sKey As String Dim sHexIV As String Dim sCorrect As String Dim lngKeyLen As Long Dim aKey() As Byte Dim aResult() As Byte Dim aData() As Byte Dim lngDataLen As Long Dim aInitV() As Byte Dim lngIVLen As Long sKey = "0123456789ABCDEFF0E1D2C3B4A59687" sHexIV = "FEDCBA9876543210" sInput = _ "37363534333231204E6F77206973207468652074696D6520666F722000000000" sCorrect = _ "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC" ' Convert to byte arrays lngKeyLen = Len(sKey) \ 2 ReDim aKey(lngKeyLen) Call bu_HexStr2Bytes(sKey, aKey) lngDataLen = Len(sInput) \ 2 ReDim aData(lngDataLen) Call bu_HexStr2Bytes(sInput, aData) ReDim aResult(lngDataLen) lngIVLen = Len(sHexIV) \ 2 ReDim aInitV(lngIVLen) Call bu_HexStr2Bytes(sHexIV, aInitV) Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "IV="; bu_Bytes2HexStr(aInitV, lngIVLen) Debug.Print "PT="; bu_Bytes2HexStr(aData, lngDataLen) ' Encrypt in one-off process lngRet = BLF_BytesMode(aResult(0), aData(0), lngDataLen, aKey(0), _ lngKeyLen, True, "CBC", aInitV(0)) Debug.Print "CT="; bu_Bytes2HexStr(aResult, lngDataLen), lngRet Debug.Print "OK="; sCorrect ' Now decrypt back lngRet = BLF_BytesMode(aData(0), aResult(0), lngDataLen, aKey(0), _ lngKeyLen, False, "cbc", aInitV(0)) Debug.Print "P'="; bu_Bytes2HexStr(aData, lngDataLen), lngRetThis should result in output as follows:
KY=0123456789ABCDEFF0E1D2C3B4A59687 IV=FEDCBA9876543210 PT=37363534333231204E6F77206973207468652074696D6520666F722000000000 CT=6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC 0 OK=6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC P'=37363534333231204E6F77206973207468652074696D6520666F722000000000 0
BLF_File
encrypts or decrypts a file
using a specified mode. The key and initialisation vector
are passed as arrays of bytes.
Public Declare Function BLF_File Lib "diCryptoSys.dll"
(ByVal sFileOut As String, ByVal sFileIn As String,
aKey As Any, ByVal lngKeyLen As Long, ByVal bEncrypt As Boolean,
ByVal sMode As String, aInitV As Any) As Long
iRet = BLF_File(sFileOut, sFileIn, aKey(0), lngKeyLen, bEncrypt, sMode, aInitV(0))
sFileOut | String with the full path name of the output
file to be created. |
sFileIn | String with the full path name of the input file
to be processed. |
aKey | Byte array containing the key. |
lngKeyLen | Long containing the length of the key in bytes. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte array containing the initialisation vector (IV),
or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Const MY_PATH As String = "C:\Test\" Dim aKey(7) As Byte Dim strFileOut As String, strFileIn As String, strFileChk As String Dim iRet As Long, nBytes As Long ' Construct full path names to files strFileIn = MY_PATH & "bigfile.dat" strFileOut = MY_PATH & "bigfile.ecb" strFileChk = MY_PATH & "bigfile.chk" ' Create the key as an array of bytes ' This creates an array of 8 bytes {&HFE, &HDC, ... &H10} nBytes = bu_HexStr2Bytes("fedcba9876543210", aKey) ' Encrypt plaintext file to cipher iRet = BLF_File(strFileOut, strFileIn, aKey(0), nBytes, _ True, "ECB", vbNullString) Debug.Print iRet ' Now decrypt it iRet = BLF_File(strFileChk, strFileOut, aKey(0), nBytes, _ False, "ECB", vbNullString) Debug.Print iRet
BLF_FileHex
encrypts or decrypts a file
using a specified mode. The key and initialisation vector
are passed as hexadecimal strings.
Public Declare Function BLF_FileHex Lib "diCryptoSys.dll"
(ByVal sFileOut As String, ByVal sFileIn As String,
ByVal sHexKey As String, ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sHexIV As String) As Long
lngRet = BLF_FileHex(sFileOut, sFileIn, sHexKey, bEncrypt, sMode, sHexIV)
sFileOut | String with the full path name of the output
file to be created. |
sFileIn | String with the full path name of the input file
to be processed. |
sHexKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector (IV)
in hexadecimal,
or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long ' Encrypt plaintext file to cipher lngRet = BLF_FileHex("bigfile.cbc", "bigfile.dat", _ "fedcba9876543210fedcba9876543210", True, "CBC", "0123456789abcdef") Debug.Print lngRet
BLF_InitHex
initialises the context with the key, direction and mode
ready for repeated
operations of the BLF_Update
or BLF_UpdateHex
functions.
The key and IV data are in hexadecimal format.
Public Declare Function BLF_InitHex Lib "diCryptoSys.dll"
(ByVal sKey As String, ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sInitV As String) As Long
hContext = BLF_InitHex(sKey, bEncrypt, sMode, sInitV)
sKey | String containing the key in hexadecimal
representation. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sInitV | String containing the initialisation vector
in hexadecimal. Set as vbNullString for ECB mode. |
Long
: non-zero handle of the context hContext
to be used in subsequent
calls to the BLF function. Returns zero if an error occurs.
BLF_InitHex
returns zero if an error occurs.
It is important to check that the value of hContext returned is
not equal to zero before calling BLF_Update
or BLF_UpdateHex
.
See BLF_UpdateHex
.
BLF_Init
BLF_UpdateHex
BLF_Update
BLF_Final
[Back to Top]
BLF_Init
initialises the context with the key, direction and mode
ready for repeated
operations of the BLF_Update
or BLF_UpdateHex
functions.
The key and IV data are provided in byte arrays.
Public Declare Function BLF_Init Lib "diCryptoSys.dll"
(aKey As Any, ByVal lngKeyLen As Long, ByVal bEncrypt As Boolean,
ByVal sMode As String, aInitV As Any) As Long
hContext = BLF_Init(aKey(0), lngKeyLen, bEncrypt, sMode, aInitV(0))
aKey | Byte array containing the key. |
lngKeyLen | Long containing the length of the key in bytes. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte array containing the initialisation vector (IV),
or vbNullString for ECB mode. |
Long
: non-zero handle of the context hContext
to be used in subsequent
calls to the BLF function. Returns zero if an error occurs.
BLF_InitHex
returns zero if an error occurs.
It is important to check that the value of hContext returned is
not equal to zero before calling BLF_Update
or BLF_UpdateHex
.
See BLF_Update
.
BLF_InitHex
BLF_UpdateHex
BLF_Update
BLF_Final
[Back to Top]
BLF_UpdateHex
carries out the Blowfish transformation function on a hexadecimal string
according to the direction
and mode set up by an earlier call to BLF_Init
or BLF_InitHex
.
Public Declare Function BLF_UpdateHex Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByVal sHexString As String) As Long
lngRet = BLF_UpdateHex(hContext, sHexString)
hContext | Long handle to the BLF context set up by an earlier call to
BLF_Init or BLF_InitHex . |
sHexString | String containing input in hexadecimal format
to be processed by the BLF function and to receive the output.
|
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim hContext As Long Dim sKey As String Dim sHexString As String Dim sCorrect As String sKey = "0123456789abcdef" Debug.Print "KY="; sKey ' Initialise the context hContext = BLF_InitHex(sKey, True, "ECB", vbNullString) If hContext = 0 Then ' Always check for error MsgBox "Unable to initialise BLF context", vbCritical Exit Function End If sHexString = "4e6f772069732074" Debug.Print "PT="; sHexString lngRet = BLF_UpdateHex(hContext, sHexString) Debug.Print "CT="; sHexString Debug.Print "OK="; "cb08e682c67e32e2" sHexString = "68652074696d6520666f7220616c6c20" Debug.Print "PT="; sHexString lngRet = BLF_UpdateHex(hContext, sHexString) Debug.Print "CT="; sHexString Debug.Print "OK="; "8fcb010ac2ce9b1d9c4538762e33b52f" lngRet = BLF_Final(hContext)This should result in output as follows:
KY=0123456789abcdef PT=4e6f772069732074 CT=CB08E682C67E32E2 OK=cb08e682c67e32e2 PT=68652074696d6520666f7220616c6c20 CT=8FCB010AC2CE9B1D9C4538762E33B52F OK=8fcb010ac2ce9b1d9c4538762e33b52f
BLF_Init
BLF_InitHex
BLF_Update
BLF_Final
[Back to Top]
BLF_Update
carries out the Blowfish transformation function on a byte array
according to the direction
and mode set up by an earlier call to BLF_Init
or BLF_InitHex
.
Public Declare Function BLF_Update Lib "diCryptoSys.dll"
(ByVal hContext As Long, aData As Any, ByVal lngDataLen As Long) As Long
lngRet = BLF_Update(hContext, aData(0), lngDataLen)
hContext | Long handle to the BLF context set up by an earlier call to
BLF_Init or BLF_InitHex . |
aData | Byte array containing the input
to be processed by the BLF function and to receive the output.
|
lngDataLen | Long containing length of the data in bytes.
|
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Const ncBLKLEN As Integer = 8 Dim lngRet As Long Dim hContext As Long Dim aKey(15) As Byte Dim aData(7) As Byte Dim lngDataLen As Long Dim aInitV(7) As Byte Dim sInput As String Dim sOutput As String Dim sCorrect As String Dim lngKeyLen As Long Dim nBlocks As Integer, i As Integer ' Input is an ascii string padded with zeroes sInput = "7654321 Now is the time for " & String(4, Chr(0)) ' Set key and IV in byte arrays lngKeyLen = bu_HexStr2Bytes("0123456789ABCDEFF0E1D2C3B4A59687", aKey) Call bu_HexStr2Bytes("FEDCBA9876543210", aInitV) sCorrect = "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC" Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "IV="; bu_Bytes2HexStr(aInitV, 8) ' Initialise context hContext = BLF_Init(aKey(0), lngKeyLen, True, "CBC", aInitV(0)) If hContext = 0 Then ' Always check for error MsgBox "Unable to initialise BLF context", vbCritical Exit Function End If ' Encrypt in 8-byte blocks sOutput = "" nBlocks = Len(sInput) \ ncBLKLEN For i = 0 To nBlocks - 1 lngDataLen = bu_String2Bytes(Mid(sInput, _ (i * ncBLKLEN) + 1, ncBLKLEN), aData) Debug.Print "PT="; bu_Bytes2HexStr(aData, lngDataLen) lngRet = BLF_Update(hContext, aData(0), lngDataLen) Debug.Print "CT="; bu_Bytes2HexStr(aData, lngDataLen); lngRet sOutput = sOutput & bu_Bytes2String(aData, lngDataLen) Next lngRet = BLF_Final(hContext) Debug.Print "OU="; bu_Str2Hex(sOutput) Debug.Print "OK="; sCorrectThis should result in output as follows:
KY=0123456789ABCDEFF0E1D2C3B4A59687 IV=FEDCBA9876543210 PT=3736353433323120 CT=6B77B4D63006DEE6 0 PT=4E6F772069732074 CT=05B156E274039793 0 PT=68652074696D6520 CT=58DEB9E7154616D9 0 PT=666F722000000000 CT=59F1652BD5FF92CC 0 OU=6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC OK=6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC
BLF_Init
BLF_InitHex
BLF_UpdateHex
BLF_Final
[Back to Top]
BLF_Ecb
carries out the Blowfish transformation function in ECB mode
on a byte array
using the key schedule set up by an earlier call to BLF_Init
or BLF_InitHex
.
The user can set the direction of encryption independently.
Public Declare Function BLF_Ecb Lib "diCryptoSys.dll"
(ByVal hContext As Long, aData As Any, ByVal lngDataLen As Long,
ByVal bEncrypt As Boolean) As Long
lngRet = BLF_Ecb(hContext, aData(0), lngDataLen, bEncrypt)
hContext | Long handle to the BLF context set up by an earlier call to
BLF_Init or BLF_InitHex . |
aData | Byte array containing the input
to be processed by the BLF function and to receive the output.
|
lngDataLen | Long containing length of the data in bytes.
|
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
BLF_Ecb
only uses the key schedule;
it does not affect subsequent calls to BLF_Update
.
BLF_Ecb
independently of
BLF_Update
.
Dim lngRet As Long Dim hContext As Long Dim aKey(15) As Byte Dim aData(7) As Byte Dim lngDataLen As Long Dim aInitV(7) As Byte Dim lngKeyLen As Long Dim nBlocks As Integer, i As Integer Dim aECBWork(15) As Byte ' Set up key and IV arrays ' ... hContext = BLF_Init(aKey(0), lngKeyLen, True, "CBC", aInitV(0)) For i = 0 To nBlocks - 1 ' ... lngRet = BLF_Update(hContext, aData(0), lngDataLen) ' Sneak in here and use ECB mode in both directions lngRet = BLF_Ecb(hContext, aECBWork(0), 16&, True) lngRet = BLF_Ecb(hContext, aECBWork(0), 16&, False) Next lngRet = BLF_Final(hContext)
BLF_EcbHex
carries out the Blowfish transformation function in ECB mode
on a hexadecimal string
using the key schedule set up by an earlier call to BLF_Init
or BLF_InitHex
.
The user can set the direction of encryption independently.
Public Declare Function BLF_EcbHex Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByVal sHexBlock As String,
ByVal bEncrypt As Boolean) As Long
lngRet = BLF_EcbHex(hContext, sHexBlock, bEncrypt)
hContext | Long handle to the BLF context set up by an earlier call to
BLF_Init or BLF_InitHex . |
sHexBlock | String containing the input
in hexadecimal characters
to be processed by the BLF function and to receive the output.
|
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
BLF_EcbHex
only uses the key schedule;
it does not affect subsequent calls to BLF_Update
.
The length of sHexBlock should be a multiple of 16 characters long
(i.e. representing a multiple of 8 bytes).
If not, odd trailing bytes will be ignored.
Note that the output overwrites the input.
sHexBlock must be a variable that can recive the output, not a constant.
BLF_Final
closes and clears the BLF context.
Public Declare Function BLF_Final Lib "diCryptoSys.dll"
(ByVal hContext As Long) As Long
lngRet = BLF_Final(hContext)
hContext | Long containing the handle to the BLF context. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
The handle hContext must have been set up by an earlier call
to BLF_Init
or BLF_InitHex
.
See BLF_UpdateHex
.
BLF_Init
BLF_InitHex
BLF_UpdateHex
BLF_Update
[Back to Top]
DES_Hex
encrypts or decrypts data
represented as a hexadecimal string
using a key represented in hexadecimal notation.
The process is carried out in one step in Electronic Codebook (EBC) mode.
Public Declare Function DES_Hex Lib "diCryptoSys.dll"
(ByVal sOutput As String, ByVal sInput As String, ByVal sKey As String,
ByVal bEncrypt As Boolean) As Long
lngRet = DES_Hex(sOutput, sInput, sKey, bEncrypt)
sOutput | String of sufficient length to receive the output. |
sInput | String containing the input data in hexadecimal. |
sKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sKey As String Dim sCorrect As String ' "Now is t" in hex sInput = "4e6f772069732074" sKey = "0123456789abcdef" sCorrect = "3fa40e8a984d4815" ' Set sOutput to be same length as sInput sOutput = String(Len(sInput), " ") Debug.Print "KY="; sKey Debug.Print "PT="; sInput ' Encrypt in one-off process lngRet = DES_Hex(sOutput, sInput, sKey, True) Debug.Print "CT="; sOutput Debug.Print "OK="; sCorrect ' Now decrypt back to plain text sInput = sOutput lngRet = DES_Hex(sOutput, sInput, sKey, False) Debug.Print "P'="; sOutputThis should result in output as follows:
KY=0123456789abcdef PT=4e6f772069732074 CT=3FA40E8A984D4815 OK=3fa40e8a984d4815 P'=4E6F772069732074
DES_HexMode
RAN_DESKeyGenHex
[Back to Top]
DES_HexMode
encrypts or decrypts data
represented as a hexadecimal string
using a specified mode. The key and initialisation vector
are represented as a hexadecimal string.
Public Declare Function DES_HexMode Lib "diCryptoSys.dll"
(ByVal sOutput As String, ByVal sInput As String, _
ByVal sHexKey As String, ByVal bEncrypt As Boolean, _
ByVal sMode As String, ByVal sHexIV As String) As Long
lngRet = DES_HexMode(sOutput, sInput, sHexKey, bEncrypt, sMode, sHexIV)
sOutput | String of sufficient length to receive the output. |
sInput | String containing the input data in hexadecimal. |
sHexKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector (IV)
in hexadecimal, or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sKey As String Dim sInitV As String Dim sCorrect As String ' "Now is the time for all " in hex sInput = "4e6f77206973207468652074696d6520666f7220616c6c20" sKey = "0123456789abcdef" sInitV = "1234567890abcdef" sCorrect = "e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6" ' Set sOutput to be same length as sInput sOutput = String(Len(sInput), " ") Debug.Print "KY="; sKey Debug.Print "IV="; sInitV Debug.Print "PT="; sInput ' Encrypt in one-off process lngRet = DES_HexMode(sOutput, sInput, sKey, True, "CBC", sInitV) Debug.Print "CT="; sOutput Debug.Print "OK="; sCorrect ' Now decrypt back to plain text sInput = sOutput lngRet = DES_HexMode(sOutput, sInput, sKey, False, "CBC", sInitV) Debug.Print "P'="; sOutputThis should result in output as follows:
KY=0123456789abcdef IV=1234567890abcdef PT=4e6f77206973207468652074696d6520666f7220616c6c20 CT=E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6 OK=e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6 P'=4E6F77206973207468652074696D6520666F7220616C6C20
DES_Hex
RAN_DESKeyGenHex
[Back to Top]
DES_Bytes
encrypts or decrypts an array of Bytes
in one step in Electronic Codebook (EBC) mode.
Public Declare Function DES_Bytes Lib "diCryptoSys.dll"
(aResult As Any, aData As Any, ByVal lngDataLen As Long,
aKey As Any, ByVal bEncrypt As Boolean) As Long
lngRet = DES_Bytes(aResult(0), aData(0), lngDataLen, aKey(0), bEncrypt)
aResult | Byte array of sufficient length to receive the output. |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
aKey | Byte array containing the key. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
bu_HexStr2Bytes
and bu_Bytes2HexStr
to convert between bytes arrays and hex strings.
Dim lngRet As Long Dim sInput As String Dim sKey As String Dim sCorrect As String Dim lngKeyLen As Long Dim aKey() As Byte Dim aResult() As Byte Dim aData() As Byte Dim lngDataLen As Long ' Define test vectors in hex (from FIPS 81) sKey = "0123456789abcdef" sInput = "4e6f77206973207468652074696d6520666f7220616c6c20" sCorrect = "3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53" ' Convert to byte arrays lngKeyLen = Len(sKey) \ 2 ReDim aKey(lngKeyLen) Call bu_HexStr2Bytes(sKey, aKey) lngDataLen = Len(sInput) \ 2 ReDim aData(lngDataLen) Call bu_HexStr2Bytes(sInput, aData) ReDim aResult(lngDataLen) Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "PT="; bu_Bytes2HexStr(aData, lngDataLen) ' Encrypt in one-off process lngRet = DES_Bytes(aResult(0), aData(0), lngDataLen, aKey(0), True) Debug.Print "CT="; bu_Bytes2HexStr(aResult, lngDataLen); lngRet Debug.Print "OK="; sCorrect ' Now decrypt back lngRet = DES_Bytes(aData(0), aResult(0), lngDataLen, aKey(0), False) Debug.Print "P'="; bu_Bytes2HexStr(aData, lngDataLen); lngRetThis should result in output as follows:
KY=0123456789ABCDEF PT=4E6F77206973207468652074696D6520666F7220616C6C20 CT=3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53 0 OK=3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53 P'=4E6F77206973207468652074696D6520666F7220616C6C20 0
DES_BytesMode
encrypts or decrypts an array of Bytes
in one step
using a specified mode.
Public Declare Function DES_BytesMode Lib "diCryptoSys.dll"
(aResult As Any, aData As Any, ByVal lngDataLen As Long,
aKey As Any, ByVal bEncrypt As Boolean,
ByVal sMode As String, aInitV As Any) As Long
lngRet = DES_BytesMode(aResult(0), aData(0), lngDataLen,
aKey(0), bEncrypt, sMode, aInitV(0))
aResult | Byte array of sufficient length to receive the output. |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
aKey | Byte array containing the key. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte containing the
initialisation vector (IV), or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
bu_HexStr2Bytes
and bu_Bytes2HexStr
utilities
to convert test hex strings into byte arrays and display the results.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sKey As String Dim sHexIV As String Dim sCorrect As String Dim lngKeyLen As Long Dim lngDataLen As Long Dim lngIVLen As Long Dim aKey() As Byte Dim aResult() As Byte Dim aData() As Byte Dim aInitV() As Byte sKey = "0123456789abcdef" sHexIV = "1234567890abcdef" sInput = "Now is the time for all " sCorrect = "e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6" ' Convert hex strings to byte arrays lngKeyLen = Len(sKey) \ 2 ReDim aKey(lngKeyLen) Call bu_HexStr2Bytes(sKey, aKey) lngIVLen = Len(sHexIV) \ 2 ReDim aInitV(lngIVLen) Call bu_HexStr2Bytes(sHexIV, aInitV) ' Convert string to byte array and dimension aResult array aData() = StrConv(sInput, vbFromUnicode) lngDataLen = UBound(aData) + 1 ReDim aResult(lngDataLen - 1) Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "IV="; bu_Bytes2HexStr(aInitV, lngIVLen) Debug.Print "PT="; "["; sInput; "]" ' Encrypt in one-off process lngRet = DES_BytesMode(aResult(0), aData(0), lngDataLen, _ aKey(0), True, "CBC", aInitV(0)) Debug.Print "CT="; bu_Bytes2HexStr(aResult, lngDataLen); lngRet Debug.Print "OK="; sCorrect ' Now decrypt back lngRet = DES_BytesMode(aData(0), aResult(0), lngDataLen, _ aKey(0), False, "cbc", aInitV(0)) sOutput = StrConv(aData(), vbUnicode) Debug.Print "P'="; "["; sOutput; "]"; lngRetThis should result in output as follows:
KY=0123456789ABCDEF IV=1234567890ABCDEF PT=[Now is the time for all ] CT=E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6 0 OK=e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6 P'=[Now is the time for all ] 0
DES_File
encrypts or decrypts a file
using a specified mode. The key and initialisation vector
are passed as arrays of bytes.
Public Declare Function DES_File Lib "diCryptoSys.dll"
(ByVal sFileOut As String, ByVal sFileIn As String,
aKey As Any, ByVal bEncrypt As Boolean,
ByVal sMode As String, aInitV As Any) As Long
iRet = DES_File(sFileOut, sFileIn, aKey(0), bEncrypt, sMode, aInitV(0))
sFileOut | String with the full path name of the output
file to be created. |
sFileIn | String with the full path name of the input file
to be processed. |
aKey | Byte array containing the key. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte array containing the initialisation vector (IV),
or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Const MY_PATH As String = "C:\Test\" Dim aKey(7) As Byte Dim strFileOut As String, strFileIn As String, strFileChk As String Dim lngRet As Long, nBytes As Long ' Construct full path names to files strFileIn = MY_PATH & "hello.txt" strFileOut = MY_PATH & "hello.enc" strFileChk = MY_PATH & "hello.chk" ' Create the key as an array of bytes ' This creates an array of 8 bytes {&HFE, &HDC, ... &H10} nBytes = bu_HexStr2Bytes("fedcba9876543210", aKey) ' Encrypt plaintext file to cipher ' Output file = 16-byte ciphertext file hello.enc lngRet = DES_File(strFileOut, strFileIn, aKey(0), _ True, "ECB", vbNullString) Debug.Print lngRet ' Now decrypt it lngRet = DES_File(strFileChk, strFileOut, aKey(0), _ False, "ECB", vbNullString) Debug.Print lngRet
DES_FileHex
encrypts or decrypts a file
using a specified mode. The key and initialisation vector
are passed as hexadecimal strings.
Public Declare Function DES_FileHex Lib "diCryptoSys.dll"
(ByVal sFileOut As String, ByVal sFileIn As String,
ByVal sHexKey As String, ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sHexIV As String) As Long
lngRet = DES_FileHex(sFileOut, sFileIn, sHexKey, bEncrypt, sMode, sHexIV)
sFileOut | String with the full path name of the output
file to be created. |
sFileIn | String with the full path name of the input file
to be processed. |
sHexKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector (IV)
in hexadecimal,
or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long lngRet = DES_FileHex("bigfile.cbc", "bigfile.dat", _ "fedcba9876543210", True, "CBC", "0123456789abcdef") Debug.Print lngRet ' and decrypt it as a check lngRet = DES_FileHex("bigfile.chk", "bigfile.cbc", _ "fedcba9876543210fedcba9876543210", False, "CBC", "0123456789abcdef") Debug.Print lngRet
DES_InitHex
initialises the context with the key, direction and mode
ready for repeated
operations of the DES function.
The key and IV data are provided in hexadecimal format.
Public Declare Function DES_InitHex Lib "diCryptoSys.dll"
(ByVal sHexKey As String, ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sHexIV As String) As Long
hContext = DES_InitHex(sHexKey, bEncrypt, sMode, sHexIV)
sHexKey | String containing the key in hexadecimal
representation. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector
in hexadecimal. Set as vbNullString for ECB mode. |
Long
: non-zero handle of the context hContext
to be used in subsequent
calls to the DES function. Returns zero if an error occurs.
DES_InitHex
returns zero if an error occurs.
It is important to check that the value of hContext returned is
not equal to zero before calling a DES function.
See DES_UpdateHex
.
DES_Init
DES_UpdateHex
DES_Update
DES_Final
[Back to Top]
DES_Init
initialises the context with the key, direction and mode
ready for repeated
operations of the DES function.
The key and IV data are provided in byte arrays.
Public Declare Function DES_Init Lib "diCryptoSys.dll"
(ByVal sHexKey As String, ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sHexIV As String) As Long
hContext = DES_Init(sHexKey, bEncrypt, sMode, sHexIV)
sHexKey | String containing the key in hexadecimal
representation. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector
in hexadecimal. Set as vbNullString for ECB mode. |
Long
: non-zero handle of the context hContext
to be used in subsequent
calls to the DES function. Returns zero if an error occurs.
DES_Init
returns zero if an error occurs.
It is important to check that the value of hContext returned is
not equal to zero before calling a DES function.
See DES_Update
.
DES_InitHex
DES_UpdateHex
DES_Update
DES_Final
[Back to Top]
DES_Update
carries out the DES transformation function on a byte array
according to the direction
and mode set up by an earlier call to DES_Init
or DES_InitHex
.
Public Declare Function DES_Update Lib "diCryptoSys.dll"
(ByVal hContext As Long, aData As Any, ByVal lngDataLen As Long) As Long
lngRet = DES_Update(hContext, aData(0), lngDataLen)
hContext | Long handle to the DES context set up by an earlier call to
DES_Init or DES_InitHex . |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim hContext As Long Dim sInput As String Dim sCorrect As String Dim lngKeyLen As Long Dim aKey() As Byte Dim aData() As Byte Dim lngDataLen As Long sInput = "Now is the time for all " sCorrect = "3fa40e8a984d43156a271787ab8883f9893d51ec4b563b53" ' Setup byte arrays and lengths ReDim aKey(7) lngKeyLen = bu_HexStr2Bytes("0123456789abcdef", aKey()) aData() = StrConv(sInput, vbFromUnicode) lngDataLen = UBound(aData) + 1 Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "PT="; bu_Bytes2HexStr(aData, lngDataLen) ' Initialise context hContext = DES_Init(aKey(0), True, "ECB", vbNullString) ' Process the data lngRet = DES_Update(hContext, aData(0), lngDataLen) Debug.Print "CT="; bu_Bytes2HexStr(aData, lngDataLen), lngRet Debug.Print "OK="; sCorrect lngRet = DES_Final(hContext) ' Now decrypt it using Init-Update-Final hContext = DES_Init(aKey(0), False, "ECB", vbNullString) lngRet = DES_Update(hContext, aData(0), lngDataLen) Debug.Print "P'="; bu_Bytes2HexStr(aData, lngDataLen), lngRet Debug.Print "P'=["; StrConv(aData(), vbUnicode); "]" lngRet = DES_Final(hContext)This should result in output as follows:
KY=0123456789ABCDEF PT=4E6F77206973207468652074696D6520666F7220616C6C20 CT=3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53 0 OK=3fa40e8a984d43156a271787ab8883f9893d51ec4b563b53 P'=4E6F77206973207468652074696D6520666F7220616C6C20 0 P'=[Now is the time for all ]
DES_Init
DES_InitHex
DES_UpdateHex
DES_Final
[Back to Top]
DES_UpdateHex
carries out the DES transformation function on a hexadecimal string
according to the direction
and mode set up by an earlier call to DES_Init
or DES_InitHex
.
Public Declare Function DES_UpdateHex Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByVal sHexString As String) As Long
lngRet = DES_UpdateHex(hContext, sHexString)
hContext | Long handle to the DES context set up by an earlier call to
DES_Init or DES_InitHex . |
sHexString | String containing input in hexadecimal format
to be processed by the DES function and to receive the output.
|
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim hContext As Long Dim sKey As String Dim sHexString As String Dim sCorrect As String sKey = "0123456789abcdef" Debug.Print "KY="; sKey ' Initialise the context hContext = DES_InitHex(sKey, True, "ECB", vbNullString) If hContext = 0 Then ' Always check for error MsgBox "Unable to initialise DES context", vbCritical Exit Function End If sHexString = "4e6f772069732074" Debug.Print "PT="; sHexString lngRet = DES_UpdateHex(hContext, sHexString) Debug.Print "CT="; sHexString Debug.Print "OK="; "3fa40e8a984d4815" sHexString = "68652074696d6520666f7220616c6c20" Debug.Print "PT="; sHexString lngRet = DES_UpdateHex(hContext, sHexString) Debug.Print "CT="; sHexString Debug.Print "OK="; "6a271787ab8883f9893d51ec4b563b53" lngRet = DES_Final(hContext)This should result in output as follows:
KY=0123456789abcdef PT=4e6f772069732074 CT=3FA40E8A984D4815 OK=3fa40e8a984d4815 PT=68652074696d6520666f7220616c6c20 CT=6A271787AB8883F9893D51EC4B563B53 OK=6a271787ab8883f9893d51ec4b563b53
DES_Init
DES_InitHex
DES_Update
DES_Final
[Back to Top]
DES_Final
closes and clears the DES context.
Public Declare Function DES_Final Lib "diCryptoSys.dll"
(ByVal hContext As Long) As Long
lngRet = DES_Final(hContext)
hContext | Long containing the handle to the DES context. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
See DES_UpdateHex
.
DES_Init
DES_InitHex
DES_UpdateHex
DES_Update
[Back to Top]
TDEA_Hex
encrypts or decrypts data
represented as a hexadecimal string
using a key represented in hexadecimal notation.
The process is carried out in one step in Electronic Codebook (EBC) mode.
Public Declare Function TDEA_Hex Lib "diCryptoSys.dll"
(ByVal sOutput As String, ByVal sInput As String, ByVal sKey As String,
ByVal bEncrypt As Boolean) As Long
lngRet = TDEA_Hex(sOutput, sInput, sKey, bEncrypt)
sOutput | String of sufficient length to receive the output. |
sInput | String containing the input data in hexadecimal. |
sKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sKey As String Dim bEncrypt As Boolean Dim sCorrect As String sInput = "8000000000000000" sKey = "010101010101010101010101010101010101010101010101" sCorrect = "95F8A5E5DD31D900" ' Set sOutput to be same length as sInput sOutput = String(Len(sInput), " ") Debug.Print "KY="; sKey Debug.Print "PT="; sInput ' Encrypt lngRet = TDEA_Hex(sOutput, sInput, sKey, bEncrypt:=True) Debug.Print "CT="; sOutput, "Return value="; lngRet Debug.Print "OK="; sCorrect ' Decrypt back sInput = sOutput lngRet = TDEA_Hex(sOutput, sInput, sKey, bEncrypt:=False) Debug.Print "P'="; sOutput, "Return value="; lngRetThis should result in output as follows:
KY=010101010101010101010101010101010101010101010101 PT=8000000000000000 CT=95F8A5E5DD31D900 Return value= 0 OK=95F8A5E5DD31D900 P'=8000000000000000 Return value= 0This example carries out one of the Monte Carlo tests from NIST 800-20 [TMOVS].
Dim lngRet As Long Dim sBlock As String Dim sKey As String Dim sCorrect As String Dim i As Integer sBlock = "4e6f772069732074" sKey = "0123456789abcdef" & _ "23456789abcdef01" & _ "456789abcdef0123" sCorrect = "dd17e8b8b437d232" Debug.Print "TDEA Monte Carlo TECB Mode Encrypt:" Debug.Print "KY="; sKey Debug.Print "PT="; sBlock ' Do 10,000 times For i = 0 To 9999 lngRet = TDEA_Hex(sBlock, sBlock, sKey, bEncrypt:=True) Next Debug.Print "CT="; sBlock Debug.Print "OK="; sCorrectThis should result in output as follows:
TDEA Monte Carlo TECB Mode Encrypt: KY=0123456789abcdef23456789abcdef01456789abcdef0123 PT=4e6f772069732074 CT=DD17E8B8B437D232 OK=dd17e8b8b437d232
TDEA_HexMode
RAN_TDEAKeyGenHex
[Back to Top]
TDEA_HexMode
encrypts or decrypts data
represented as a hexadecimal string
using a specified mode. The key and initialisation vector
are represented as a hexadecimal string.
Public Declare Function TDEA_HexMode Lib "diCryptoSys.dll"
(ByVal sOutput As String, ByVal sInput As String, _
ByVal sHexKey As String, ByVal bEncrypt As Boolean, _
ByVal sMode As String, ByVal sHexIV As String) As Long
lngRet = TDEA_HexMode(sOutput, sInput, sHexKey, bEncrypt, sMode, sHexIV)
sOutput | String of sufficient length to receive the output. |
sInput | String containing the input data in hexadecimal. |
sHexKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector (IV)
in hexadecimal, or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sKey As String Dim sInitV As String Dim bEncrypt As Boolean Dim sCorrect As String sInput = "5468697320736F6D652073616D706520636F6E74656E742E0808080808080808" sKey = "737C791F25EAD0E04629254352F7DC6291E5CB26917ADA32" sInitV = "B36B6BFB6231084E" sCorrect = "d76fd1178fbd02f84231f5c1d2a2f74a4159482964f675248254223daf9af8e4" ' Set sOutput to be same length as sInput sOutput = String(Len(sInput), " ") Debug.Print "KY="; sKey Debug.Print "PT="; sInput lngRet = TDEA_HexMode(sOutput, sInput, sKey, True, "CBC", sInitV) Debug.Print "CT="; sOutput; lngRet Debug.Print "OK="; sCorrect sInput = sOutput lngRet = TDEA_HexMode(sOutput, sInput, sKey, False, "CBC", sInitV) Debug.Print "P'="; sOutput; lngRetThis should result in output as follows:
KY=737C791F25EAD0E04629254352F7DC6291E5CB26917ADA32 PT=5468697320736F6D652073616D706520636F6E74656E742E0808080808080808 CT=D76FD1178FBD02F84231F5C1D2A2F74A4159482964F675248254223DAF9AF8E4 0 OK=d76fd1178fbd02f84231f5c1d2a2f74a4159482964f675248254223daf9af8e4 P'=5468697320736F6D652073616D706520636F6E74656E742E0808080808080808 0
TDEA_Hex
RAN_TDEAKeyGenHex
[Back to Top]
TDEA_Bytes
encrypts or decrypts an array of Bytes
in one step in Electronic Codebook (EBC) mode.
Public Declare Function TDEA_Bytes Lib "diCryptoSys.dll"
(aResult As Any, aData As Any, ByVal lngDataLen As Long,
aKey As Any, ByVal bEncrypt As Boolean) As Long
lngRet = TDEA_Bytes(aResult(0), aData(0), lngDataLen, aKey(0), bEncrypt)
aResult | Byte array of sufficient length to receive the output. |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
aKey | Byte array containing the key. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
bu_HexStr2Bytes
and bu_Bytes2HexStr
to convert between bytes arrays and hex strings.
Dim lngRet As Long Dim sInput As String Dim sKey As String Dim sCorrect As String Dim lngKeyLen As Long Dim aKey() As Byte Dim aResult() As Byte Dim aData() As Byte Dim lngDataLen As Long ' Define test vectors in hex sKey = "0123456789abcdeffedcba987654321089abcdef01234567" sInput = "0123456789abcde70123456789abcde7" sCorrect = "de0b7c06ae5e0ed5de0b7c06ae5e0ed5" ' Convert to byte arrays lngKeyLen = Len(sKey) \ 2 ReDim aKey(lngKeyLen) Call bu_HexStr2Bytes(sKey, aKey) lngDataLen = Len(sInput) \ 2 ReDim aData(lngDataLen) Call bu_HexStr2Bytes(sInput, aData) ReDim aResult(lngDataLen) Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "PT="; bu_Bytes2HexStr(aData, lngDataLen) ' Encrypt in one-off process lngRet = TDEA_Bytes(aResult(0), aData(0), lngDataLen, aKey(0), True) Debug.Print "CT="; bu_Bytes2HexStr(aResult, lngDataLen); lngRet Debug.Print "OK="; sCorrect ' Now decrypt back lngRet = TDEA_Bytes(aData(0), aResult(0), lngDataLen, aKey(0), False) Debug.Print "P'="; bu_Bytes2HexStr(aData, lngDataLen); lngRetThis should result in output as follows:
KY=0123456789ABCDEFFEDCBA987654321089ABCDEF01234567 PT=0123456789ABCDE70123456789ABCDE7 CT=DE0B7C06AE5E0ED5DE0B7C06AE5E0ED5 0 OK=de0b7c06ae5e0ed5de0b7c06ae5e0ed5 P'=0123456789ABCDE70123456789ABCDE7 0
TDEA_BytesMode
encrypts or decrypts an array of Bytes
in one step
using a specified mode.
Public Declare Function TDEA_BytesMode Lib "diCryptoSys.dll"
(aResult As Any, aData As Any, ByVal lngDataLen As Long,
aKey As Any, ByVal bEncrypt As Boolean,
ByVal sMode As String, aInitV As Any) As Long
lngRet = TDEA_BytesMode(aResult(0), aData(0), lngDataLen,
aKey(0), bEncrypt, sMode, aInitV(0))
aResult | Byte array of sufficient length to receive the output. |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
aKey | Byte array containing the key. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte containing the
initialisation vector (IV), or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
bu_HexStr2Bytes
and bu_Bytes2HexStr
utilities
to convert test hex strings into byte arrays and display the results.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sKey As String Dim sHexIV As String Dim sCorrect As String Dim lngKeyLen As Long Dim lngDataLen As Long Dim lngIVLen As Long Dim aKey() As Byte Dim aResult() As Byte Dim aData() As Byte Dim aInitV() As Byte sKey = "0123456789abcdeffedcba987654321089abcdef01234567" sHexIV = "1234567890abcdef" sInput = "Now is the time for all " sCorrect = "204011f986e35647199e47af391620c5bb9a5bcfc86db0bb" ' Convert hex strings to byte arrays lngKeyLen = Len(sKey) \ 2 ReDim aKey(lngKeyLen) Call bu_HexStr2Bytes(sKey, aKey) lngIVLen = Len(sHexIV) \ 2 ReDim aInitV(lngIVLen) Call bu_HexStr2Bytes(sHexIV, aInitV) ' Convert string to byte array and dimension aResult array aData() = StrConv(sInput, vbFromUnicode) lngDataLen = UBound(aData) + 1 ReDim aResult(lngDataLen - 1) Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "IV="; bu_Bytes2HexStr(aInitV, lngIVLen) Debug.Print "PT="; "["; sInput; "]" ' Encrypt in one-off process lngRet = TDEA_BytesMode(aResult(0), aData(0), lngDataLen, _ aKey(0), True, "CBC", aInitV(0)) Debug.Print "CT="; bu_Bytes2HexStr(aResult, lngDataLen); lngRet Debug.Print "OK="; sCorrect ' Now decrypt back lngRet = TDEA_BytesMode(aData(0), aResult(0), lngDataLen, _ aKey(0), False, "cbc", aInitV(0)) sOutput = StrConv(aData(), vbUnicode) Debug.Print "P'="; "["; sOutput; "]"; lngRetThis should result in output as follows:
KY=0123456789ABCDEFFEDCBA987654321089ABCDEF01234567 IV=1234567890ABCDEF PT=[Now is the time for all ] CT=204011F986E35647199E47AF391620C5BB9A5BCFC86DB0BB 0 OK=204011f986e35647199e47af391620c5bb9a5bcfc86db0bb P'=[Now is the time for all ] 0
TDEA_File
encrypts or decrypts a file
using a specified mode. The key and initialisation vector
are passed as arrays of bytes.
Public Declare Function TDEA_File Lib "diCryptoSys.dll"
(ByVal sFileOut As String, ByVal sFileIn As String,
aKey As Any, ByVal bEncrypt As Boolean,
ByVal sMode As String, aInitV As Any) As Long
iRet = TDEA_File(sFileOut, sFileIn, aKey(0), bEncrypt, sMode, aInitV(0))
sFileOut | String with the full path name of the output
file to be created. |
sFileIn | String with the full path name of the input file
to be processed. |
aKey | Byte array containing the key. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte array containing the initialisation vector (IV),
or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Const MY_PATH As String = "C:\Test\" Dim aKey(23) As Byte Dim strFileOut As String, strFileIn As String, strFileChk As String Dim lngRet As Long, nBytes As Long ' Construct full path names to files strFileIn = MY_PATH & "hello.txt" strFileOut = MY_PATH & "hello.enc" strFileChk = MY_PATH & "hello.chk" ' Create the key as an array of bytes ' This creates an array of 24 bytes {&HFE, &HDC, ... &H10} nBytes = bu_HexStr2Bytes("fedcba9876543210fedcba9876543210fedcba9876543210", aKey) ' Encrypt plaintext file to cipher ' Output file = 16-byte ciphertext file hello.enc lngRet = TDEA_File(strFileOut, strFileIn, aKey(0), _ True, "ECB", vbNullString) Debug.Print lngRet ' Now decrypt it lngRet = TDEA_File(strFileChk, strFileOut, aKey(0), _ False, "ECB", vbNullString) Debug.Print lngRet
TDEA_FileHex
encrypts or decrypts a file
using a specified mode. The key and initialisation vector
are passed as hexadecimal strings.
Public Declare Function TDEA_FileHex Lib "diCryptoSys.dll"
(ByVal sFileOut As String, ByVal sFileIn As String,
ByVal sHexKey As String, ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sHexIV As String) As Long
lngRet = TDEA_FileHex(sFileOut, sFileIn, sHexKey, bEncrypt, sMode, sHexIV)
sFileOut | String with the full path name of the output
file to be created. |
sFileIn | String with the full path name of the input file
to be processed. |
sHexKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector (IV)
in hexadecimal,
or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long lngRet = TDEA_FileHex("bigfile.cbc", "bigfile.dat", _ "fedcba9876543210fedcba9876543210fedcba9876543210", _ True, "CBC", "0123456789abcdef") Debug.Print lngRet ' and decrypt it as a check lngRet = TDEA_FileHex("bigfile.chk", "bigfile.cbc", _ "fedcba9876543210fedcba9876543210fedcba9876543210", _ False, "CBC", "0123456789abcdef") Debug.Print lngRet
TDEA_InitHex
initialises the context with the key, direction and mode
ready for repeated
operations of the TDEA function.
The key and IV data are provided in hexadecimal format.
Public Declare Function TDEA_InitHex Lib "diCryptoSys.dll"
(ByVal sHexKey As String, ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sHexIV As String) As Long
hContext = TDEA_InitHex(sHexKey, bEncrypt, sMode, sHexIV)
sHexKey | String containing the key in hexadecimal
representation. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector
in hexadecimal. Set as vbNullString for ECB mode. |
Long
: non-zero handle of the context hContext
to be used in subsequent
calls to the TDEA function. Returns zero if an error occurs.
TDEA_InitHex
returns zero if an error occurs.
It is important to check that the value of hContext returned is
not equal to zero before calling a TDEA function.
See TDEA_UpdateHex
.
TDEA_Init
TDEA_UpdateHex
TDEA_Update
TDEA_Final
[Back to Top]
TDEA_Init
initialises the context with the key, direction and mode
ready for repeated
operations of the TDEA function.
The key and IV data are provided in byte arrays.
Public Declare Function TDEA_Init Lib "diCryptoSys.dll"
(ByVal sHexKey As String, ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sHexIV As String) As Long
hContext = TDEA_Init(sHexKey, bEncrypt, sMode, sHexIV)
sHexKey | String containing the key in hexadecimal
representation. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector
in hexadecimal. Set as vbNullString for ECB mode. |
Long
: non-zero handle of the context hContext
to be used in subsequent
calls to the TDEA function. Returns zero if an error occurs.
TDEA_Init
returns zero if an error occurs.
It is important to check that the value of hContext returned is
not equal to zero before calling a TDEA function.
See TDEA_Update
.
TDEA_InitHex
TDEA_UpdateHex
TDEA_Update
TDEA_Final
[Back to Top]
TDEA_Update
carries out the TDEA transformation function on a byte array
according to the direction
and mode set up by an earlier call to TDEA_Init
or TDEA_InitHex
.
Public Declare Function TDEA_Update Lib "diCryptoSys.dll"
(ByVal hContext As Long, aData As Any, ByVal lngDataLen As Long) As Long
lngRet = TDEA_Update(hContext, aData(0), lngDataLen)
hContext | Long handle to the TDEA context set up by an earlier call to
TDEA_Init or TDEA_InitHex . |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim hContext As Long Dim sCorrect As String Dim j As Integer Dim aKey(23) As Byte Dim aInitV(7) As Byte Dim aBlock() As Byte Dim lngKeyLen As Long Dim lngBlkLen As Long Dim aNext() As Byte Dim aLast() As Byte aBlock() = StrConv("Now is t", vbFromUnicode) lngKeyLen = bu_HexStr2Bytes( _ "0123456789abcdef23456789abcdef01456789abcdef0123", aKey()) Call bu_HexStr2Bytes("1234567890abcdef", aInitV()) sCorrect = "cb191f85d1ed8439" Debug.Print "TDEA Monte Carlo TCBC Mode Encrypt:" Debug.Print "KY="; bu_Bytes2HexStr(aKey(), lngKeyLen) Debug.Print "IV="; bu_Bytes2HexStr(aInitV(), 8) Debug.Print "PT="; bu_Bytes2HexStr(aBlock(), 8) hContext = TDEA_Init(aKey(0), True, "CBC", aInitV(0)) If hContext = 0 Then Exit Function End If ' Do 10,000 times aNext() = aBlock() For j = 0 To 9999 aBlock() = aNext() lngRet = TDEA_Update(hContext, aBlock(0), 8&) If j = 0 Then aNext() = aInitV() Else aNext() = aLast() End If aLast() = aBlock() Next Debug.Print "CT="; bu_Bytes2HexStr(aBlock(), 8) Debug.Print "OK="; sCorrect lngRet = TDEA_Final(hContext)This should result in output as follows:
TDEA Monte Carlo TCBC Mode Encrypt: KY=0123456789ABCDEF23456789ABCDEF01456789ABCDEF0123 IV=1234567890ABCDEF PT=4E6F772069732074 CT=CB191F85D1ED8439 OK=cb191f85d1ed8439
TDEA_Init
TDEA_InitHex
TDEA_UpdateHex
TDEA_Final
[Back to Top]
TDEA_UpdateHex
carries out the TDEA transformation function on a hexadecimal string
according to the direction
and mode set up by an earlier call to TDEA_Init
or TDEA_InitHex
.
Public Declare Function TDEA_UpdateHex Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByVal sHexString As String) As Long
lngRet = TDEA_UpdateHex(hContext, sHexString)
hContext | Long handle to the TDEA context set up by an earlier call to
TDEA_Init or TDEA_InitHex . |
sHexString | String containing input in hexadecimal format
to be processed by the TDEA function and to receive the output.
|
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim hContext As Long Dim sBlock As String Dim sKey As String Dim sInitV As String Dim sNext As String Dim sLast As String Dim sCorrect As String Dim j As Integer sBlock = "4e6f772069732074" sKey = "0123456789abcdef" & _ "23456789abcdef01" & _ "456789abcdef0123" sInitV = "1234567890abcdef" sCorrect = "cb191f85d1ed8439" Debug.Print "TDEA Monte Carlo TCBC Mode Encrypt:" Debug.Print "KY="; sKey Debug.Print "IV="; sInitV Debug.Print "PT="; sBlock hContext = TDEA_InitHex(sKey, True, "CBC", sInitV) If hContext = 0 Then Exit Function End If ' Do 10,000 times sNext = sBlock For j = 0 To 9999 sBlock = sNext lngRet = TDEA_UpdateHex(hContext, sBlock) If j = 0 Then sNext = sInitV Else sNext = sLast End If sLast = sBlock Next Debug.Print "CT="; sBlock Debug.Print "OK="; sCorrect lngRet = TDEA_Final(hContext)This should result in output as follows:
TDEA Monte Carlo TCBC Mode Encrypt: KY=0123456789abcdef23456789abcdef01456789abcdef0123 IV=1234567890abcdef PT=4e6f772069732074 CT=CB191F85D1ED8439 OK=cb191f85d1ed8439
TDEA_Init
TDEA_InitHex
TDEA_Update
TDEA_Final
[Back to Top]
TDEA_Final
closes and clears the TDEA context.
Public Declare Function TDEA_Final Lib "diCryptoSys.dll"
(ByVal hContext As Long) As Long
lngRet = TDEA_Final(hContext)
hContext | Long containing the handle to the TDEA context. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
See TDEA_UpdateHex
.
TDEA_Init
TDEA_InitHex
TDEA_UpdateHex
TDEA_Update
[Back to Top]
AES_Hex
encrypts or decrypts data
represented as a hexadecimal string
using a key represented in hexadecimal notation.
The process is carried out in one step in Electronic Codebook (EBC) mode.
Public Declare Function AES_Hex Lib "diCryptoSys.dll"
(ByVal sOutput As String, ByVal sInput As String, ByVal sHexKey As String,
ByVal lngKeyBits As Long, ByVal lngBlockBits As Long,
ByVal bEncrypt As Boolean) As Long
lngRet = AES_Hex(sOutput, sInput, sHexKey, lngKeyBits, lngBlockBits, bEncrypt)
sOutput | String of sufficient length to receive the output. |
sInput | String containing the input data in hexadecimal. |
sHexKey | String containing the key in hexadecimal. |
lngKeyBits | Long specifying the number of
bits in the key. Select from 128, 192, or 256. |
lngBlockBits | Long specifying the number of
bits in the block. Select from 128, 192, or 256. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sHexKey As String Dim sCorrect As String sHexKey = "00000000000000000000000000000000" sInput = "80000000000000000000000000000000" sCorrect = "3AD78E726C1EC02B7EBFE92B23D9EC34" ' Set sOutput to be same length as sInput sOutput = String(Len(sInput), " ") Debug.Print "KY="; sHexKey Debug.Print "PT="; sInput ' Encrypt in one-off process lngRet = AES_Hex(sOutput, sInput, sHexKey, 128&, 128&, True) Debug.Print "CT="; sOutput; lngRet Debug.Print "OK="; sCorrect ' Now decrypt back to plain text sInput = sOutput lngRet = AES_Hex(sOutput, sInput, sHexKey, 128&, 128&, False) Debug.Print "P'="; sOutput; lngRetThis should result in output as follows:
KY=00000000000000000000000000000000 PT=80000000000000000000000000000000 CT=3AD78E726C1EC02B7EBFE92B23D9EC34 0 OK=3AD78E726C1EC02B7EBFE92B23D9EC34 P'=80000000000000000000000000000000 0
AES_HexMode
encrypts or decrypts data
represented as a hexadecimal string
using a specified mode. The key and initialisation vector
are represented as a hexadecimal string.
Public Declare Function AES_HexMode Lib "diCryptoSys.dll"
(ByVal sOutput As String, ByVal sInput As String, ByVal sHexKey As String,
ByVal lngKeyBits As Long, ByVal lngBlockBits As Long,
ByVal bEncrypt As Boolean,
ByVal sMode As String, ByVal sHexIV As String) As Long
AES_HexMode(sOutput, sInput, sHexKey,lngKeyBits, lngBlockBits, bEncrypt, sMode, sHexIV)
sOutput | String of sufficient length to receive the output. |
sInput | String containing the input data in hexadecimal. |
sHexKey | String containing the key in hexadecimal. |
lngKeyBits | Long specifying the number of
bits in the key. Select from 128, 192, or 256. |
lngBlockBits | Long specifying the number of
bits in the block. Select from 128, 192, or 256. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector (IV)
in hexadecimal, or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sHexKey As String Dim sHexIV As String Dim sCorrect As String sHexKey = "0123456789ABCDEFF0E1D2C3B4A59687" sHexIV = "FEDCBA9876543210FEDCBA9876543210" ' "Now is the time for all good men" sInput = "4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E" sCorrect = "C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177" ' Set sOutput to be same length as sInput sOutput = String(Len(sInput), " ") Debug.Print "KY="; sHexKey Debug.Print "IV="; sHexIV Debug.Print "PT="; sInput ' Encrypt in one-off process lngRet = AES_HexMode(sOutput, sInput, sHexKey, 128&, 128&, True, "CBC", sHexIV) Debug.Print "CT="; sOutput, lngRet Debug.Print "OK="; sCorrect ' Now decrypt back to plain text sInput = sOutput lngRet = AES_HexMode(sOutput, sInput, sHexKey, 128, 128, False, "cbc", sHexIV) Debug.Print "P'="; sOutput, lngRetThis should result in output as follows:
KY=0123456789ABCDEFF0E1D2C3B4A59687 IV=FEDCBA9876543210FEDCBA9876543210 PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E CT=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177 0 OK=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177 P'=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E 0
AES_Bytes
encrypts or decrypts an array of Bytes
in one step in Electronic Codebook (EBC) mode.
Public Declare Function AES_Bytes Lib "diCryptoSys.dll"
(aResult As Any, aData As Any, ByVal lngDataLen As Long, aKey As Any,
ByVal lngKeyBits As Long, ByVal lngBlockBits As Long,
ByVal bEncrypt As Boolean) As Long
lngRet = AES_Bytes(aResult(0), aData(0), lngDataLen, aKey(0), lngKeyBits, lngBlockBits, bEncrypt)
aResult | Byte array of sufficient length to receive the output. |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
aKey | Byte array containing the key. |
lngKeyBits | Long specifying the number of
bits in the key. Select from 128, 192, or 256. |
lngBlockBits | Long specifying the number of
bits in the block. Select from 128, 192, or 256. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sInput As String Dim sHexKey As String Dim sCorrect As String Dim lngKeyLen As Long Dim aKey() As Byte Dim aResult() As Byte Dim aData() As Byte Dim lngDataLen As Long ' Define test vectors in hex sHexKey = "00000000000000000000000000000000" sInput = "80000000000000000000000000000000" sCorrect = "3AD78E726C1EC02B7EBFE92B23D9EC34" ' Convert to byte arrays lngKeyLen = Len(sHexKey) \ 2 ReDim aKey(lngKeyLen) Call bu_HexStr2Bytes(sHexKey, aKey) lngDataLen = Len(sInput) \ 2 ReDim aData(lngDataLen) Call bu_HexStr2Bytes(sInput, aData) ReDim aResult(lngDataLen) Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "PT="; bu_Bytes2HexStr(aData, lngDataLen) ' Encrypt in one-off process lngRet = AES_Bytes(aResult(0), aData(0), lngDataLen, aKey(0), 128&, 128&, True) Debug.Print "CT="; bu_Bytes2HexStr(aResult, lngDataLen), lngRet Debug.Print "OK="; sCorrect ' Now decrypt back lngRet = AES_Bytes(aData(0), aResult(0), lngDataLen, aKey(0), 128&, 128&, False) Debug.Print "P'="; bu_Bytes2HexStr(aData, lngDataLen), lngRetThis should result in output as follows:
KY=00000000000000000000000000000000 PT=80000000000000000000000000000000 CT=3AD78E726C1EC02B7EBFE92B23D9EC34 0 OK=3AD78E726C1EC02B7EBFE92B23D9EC34 P'=80000000000000000000000000000000 0
AES_BytesMode
encrypts or decrypts an array of Bytes
using a specified mode.
The key and IV data are in byte arrays.
Public Declare Function AES_BytesMode Lib "diCryptoSys.dll"
(aResult As Any, aData As Any, ByVal lngDataLen As Long, aKey As Any,
ByVal lngKeyBits As Long, ByVal lngBlockBits As Long,
ByVal bEncrypt As Boolean) As Long
lngRet = AES_Bytes(aResult(0), aData(0), lngDataLen, aKey(0),
lngKeyBits, lngBlockBits, bEncrypt, sMode, aInitV(0))
aResult | Byte array of sufficient length to receive the output. |
aData | Byte array containing the input data. |
lngDataLen | Long equal to length of the input data in bytes. |
aKey | Byte array containing the key. |
lngKeyBits | Long specifying the number of
bits in the key. Select from 128, 192, or 256. |
lngBlockBits | Long specifying the number of
bits in the block. Select from 128, 192, or 256. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte containing the
initialisation vector (IV), or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sHexKey As String Dim sHexIV As String Dim sCorrect As String Dim lngKeyLen As Long Dim aKey(15) As Byte Dim aInitV(15) As Byte Dim aResult() As Byte Dim aData() As Byte Dim lngDataLen As Long Dim lngIVLen As Long sInput = "Now is the time for all good men" sCorrect = "C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177" sHexKey = "0123456789ABCDEFF0E1D2C3B4A59687" sHexIV = "FEDCBA9876543210FEDCBA9876543210" ' Convert to byte arrays aData() = StrConv(sInput, vbFromUnicode) lngDataLen = UBound(aData) + 1 ReDim aResult(lngDataLen) lngKeyLen = bu_HexStr2Bytes(sHexKey, aKey) lngIVLen = bu_HexStr2Bytes(sHexIV, aInitV) Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "IV="; bu_Bytes2HexStr(aInitV, lngIVLen) Debug.Print "PT="; bu_Bytes2HexStr(aData, lngDataLen) ' Encrypt in one-off process lngRet = AES_BytesMode(aResult(0), aData(0), lngDataLen, aKey(0), _ 128&, 128&, True, "CBC", aInitV(0)) Debug.Print "CT="; bu_Bytes2HexStr(aResult, lngDataLen) Debug.Print "OK="; sCorrect ' Now decrypt back lngRet = AES_BytesMode(aData(0), aResult(0), lngDataLen, aKey(0), _ 128&, 128&, False, "cbc", aInitV(0)) sOutput = StrConv(aData(), vbUnicode) Debug.Print "P'="; sOutputThis should result in output as follows:
KY=0123456789ABCDEFF0E1D2C3B4A59687 IV=FEDCBA9876543210FEDCBA9876543210 PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E CT=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177 OK=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177 P'=Now is the time for all good men
AES_FileHex
encrypts or decrypts a file
using a specified mode. The key and initialisation vector
are passed as hexadecimal strings.
Public Declare Function AES_FileHex Lib "diCryptoSys.dll"
(ByVal sFileOut As String, ByVal sFileIn As String,
ByVal sHexKey As String, ByVal lngKeyBits As Long, ByVal lngBlockBits As Long,
ByVal bEncrypt As Boolean, ByVal sMode As String, ByVal sHexIV As String) As Long
lngRet = AES_FileHex(sFileOut, sFileIn, sHexKey, bEncrypt, lngKeyBits, lngBlockBits, sMode, sHexIV)
sFileOut | String with the full path name of the output
file to be created. |
sFileIn | String with the full path name of the input file
to be processed. |
sHexKey | String containing the key in hexadecimal. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
lngKeyBits | Long specifying the number of
bits in the key. Select from 128, 192, or 256. |
lngBlockBits | Long specifying the number of
bits in the block. Select from 128, 192, or 256. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sHexIV | String containing the initialisation vector (IV)
in hexadecimal,
or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long
lngRet = AES_FileHex("bigfile.cbc", "bigfile.dat", _
"fedcba9876543210fedcba9876543210", 128&, 128&, True, _
"CBC", "0123456789abcdef0123456789abcdef")
AES_File
encrypts or decrypts a file
using a specified mode. The key and initialisation vector
are passed as arrays of bytes.
Public Declare Function AES_File Lib "diCryptoSys.dll"
(ByVal sFileOut As String, ByVal sFileIn As String,
aKey As Any, ByVal lngKeyBits As Long, ByVal lngBlockBits As Long,
ByVal bEncrypt As Boolean, ByVal sMode As String, aInitV As Any) As Long
iRet = AES_File(sFileOut, sFileIn, aKey(0), lngKeyBits, lngBlockBits, bEncrypt, sMode, aInitV(0))
sFileOut | String with the full path name of the output
file to be created. |
sFileIn | String with the full path name of the input file
to be processed. |
aKey | Byte array containing the key. |
lngKeyBits | Long specifying the number of
bits in the key. Select from 128, 192, or 256. |
lngBlockBits | Long specifying the number of
bits in the block. Select from 128, 192, or 256. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte array containing the initialisation vector (IV),
or vbNullString for ECB mode. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Const MY_PATH As String = "C:\Test\" Dim strFileOut As String, strFileIn As String, strFileChk As String Dim lngRet As Long Dim aKey(15) As Byte ' Construct full path names to files strFileIn = MY_PATH & "now.txt" strFileOut = MY_PATH & "aesnow.enc" strFileChk = MY_PATH & "aesnow.chk" ' Convert key to byte array Call bu_HexStr2Bytes("0123456789ABCDEFF0E1D2C3B4A59687", aKey) ' Encrypt plaintext file to cipher lngRet = AES_File(strFileOut, strFileIn, aKey(0), _ 128&, 128&, True, "ECB", vbNullString) Debug.Print lngRet ' Now decrypt it lngRet = AES_File(strFileChk, strFileOut, aKey(0), _ 128&, 128&, False, "ECB", vbNullString) Debug.Print lngRet
AES_InitHex
initialises the context with the key, direction and mode
ready for repeated
operations of the AES function.
The key and IV data are in hexadecimal format.
Public Declare Function AES_InitHex Lib "diCryptoSys.dll"
(ByVal sHexKey As String, _
ByVal lngKeyBits As Long, ByVal lngBlockBits As Long, _
ByVal bEncrypt As Boolean, _
ByVal sMode As String, ByVal sHexIV As String) As Long
hContext = AES_InitHex(sHexKey, lngKeyBits, lngBlockBits, bEncrypt, sMode, sHexIV)
sHexKey | String containing the key in hexadecimal
representation. |
lngKeyBits | Long specifying the number of
bits in the key. Select from 128, 192, or 256. |
lngBlockBits | Long specifying the number of
bits in the block. Select from 128, 192, or 256. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
sInitV | String containing the initialisation vector
in hexadecimal. Set as vbNullString for ECB mode. |
Long
: non-zero handle of the context hContext
to be used in subsequent
calls to the AES function. Returns zero if an error occurs.
AES_InitHex
returns zero if an error occurs.
It is important to check that the value of hContext returned is
not equal to zero before calling a AES function.
See AES_UpdateHex
.
AES_Init
AES_UpdateHex
AES_Update
AES_Final
[Back to Top]
AES_Init
initialises the context with the key, direction and mode
ready for repeated
operations of the AES function.
The key and IV data are in byte arrays.
Public Declare Function AES_Init Lib "diCryptoSys.dll"
(aKey As Any, ByVal lngKeyBits As Long, ByVal lngBlockBits As Long,
ByVal bEncrypt As Boolean, ByVal sMode As String, aInitV As Any) As Long
hContext = AES_Init(aKey(0), lngKeyBits, lngBlockBits, bEncrypt, sMode, aInitV(0))
aKey | Byte array containing the key. |
lngKeyBits | Long specifying the number of
bits in the key. Select from 128, 192, or 256. |
lngBlockBits | Long specifying the number of
bits in the block. Select from 128, 192, or 256. |
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
sMode | String indicating mode
to process subsequent input: either "ECB"
for Electronic Codebook mode or "CBC" for Cipher Block Chaining mode. |
aInitV | Byte array containing the initialisation vector,
or vbNullString for ECB mode. |
Long
: non-zero handle of the context hContext
to be used in subsequent
calls to the AES function. Returns zero if an error occurs.
AES_Init
returns zero if an error occurs.
It is important to check that the value of hContext returned is
not equal to zero before calling a AES function.
See AES_Update
.
AES_InitHex
AES_UpdateHex
AES_Update
AES_Final
[Back to Top]
AES_UpdateHex
carries out the AES transformation function on a hexadecimal string
according to the direction
and mode set up by an earlier call to AES_Init
or AES_InitHex
.
Public Declare Function AES_UpdateHex Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByVal sHexString As String) As Long
lngRet = AES_UpdateHex(hContext, sHexString)
hContext | Long handle to the AES context set up by an earlier call to
AES_Init or AES_InitHex . |
sHexString | String containing input in hexadecimal format
to be processed by the AES function and to receive the output.
|
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim hContext As Long Dim sBlock As String Dim sHexKey As String Dim sInitV As String Dim sNext As String Dim sLast As String Dim sCorrect As String Dim j As Integer sBlock = "204F17E2444381F6114FF53934C0BCD3" sHexKey = "8A05FC5E095AF4848A08D328D3688E3D" sInitV = "8A05FC5E095AF4848A08D328D3688E3D" sCorrect = "192D9B3AA10BB2F7846CCBA0085C657A" Debug.Print "AES Monte Carlo CBC Mode Encrypt:" Debug.Print "KY="; sHexKey Debug.Print "IV="; sInitV Debug.Print "PT="; sBlock hContext = AES_InitHex(sHexKey, 128, 128, True, "CBC", sInitV) If hContext = 0 Then MsgBox "Failed to set context", vbCritical Exit Function End If ' Do 10,000 times sNext = sBlock For j = 0 To 9999 sBlock = sNext lngRet = AES_UpdateHex(hContext, sBlock) If j = 0 Then sNext = sInitV Else sNext = sLast End If sLast = sBlock Next Debug.Print "CT="; sBlock Debug.Print "OK="; sCorrect lngRet = AES_Final(hContext)This should result in output as follows:
AES Monte Carlo CBC Mode Encrypt: KY=8A05FC5E095AF4848A08D328D3688E3D IV=8A05FC5E095AF4848A08D328D3688E3D PT=204F17E2444381F6114FF53934C0BCD3 CT=192D9B3AA10BB2F7846CCBA0085C657A OK=192D9B3AA10BB2F7846CCBA0085C657A
AES_Init
AES_InitHex
AES_Update
AES_Final
[Back to Top]
AES_Update
carries out the AES transformation function on a byte array
according to the direction
and mode set up by an earlier call to AES_Init
or AES_InitHex
.
Public Declare Function AES_Update Lib "diCryptoSys.dll"
(ByVal hContext As Long, aData As Any, ByVal lngDataLen As Long) As Long
lngRet = AES_Update(hContext, aData(0), lngDataLen)
hContext | Long handle to the AES context set up by an earlier call to
AES_Init or AES_InitHex . |
aData | Byte array containing the input
to be processed by the AES function and to receive the output.
|
lngDataLen | Long containing length of the data in bytes.
|
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim lngRet As Long Dim sOutput As String Dim sInput As String Dim sHexKey As String Dim sHexIV As String Dim sCorrect As String Dim lngKeyLen As Long Dim aKey(15) As Byte Dim aData(15) As Byte Dim lngDataLen As Long Dim aInitV(15) As Byte Dim lngIVLen As Long Dim hContext As Long Dim i As Integer sHexKey = "0123456789ABCDEFF0E1D2C3B4A59687" sHexIV = "FEDCBA9876543210FEDCBA9876543210" sInput = "Now is the time for all good men" sCorrect = "C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177" ' Convert to byte arrays lngKeyLen = bu_HexStr2Bytes(sHexKey, aKey) lngIVLen = bu_HexStr2Bytes(sHexIV, aInitV) Debug.Print "KY="; bu_Bytes2HexStr(aKey, lngKeyLen) Debug.Print "IV="; bu_Bytes2HexStr(aInitV, lngIVLen) ' Initialise context hContext = AES_Init(aKey(0), 128, 128, True, "CBC", aInitV(0)) If hContext = 0 Then MsgBox "Unable to create context" Exit Function End If ' Encrypt string in blocks of 16 bytes (128 bits) sOutput = "" lngDataLen = 16 For i = 0 To (Len(sInput) \ lngDataLen) - 1 ' Get next block Call bu_String2Bytes(Mid(sInput, i * lngDataLen + 1, lngDataLen), aData) Debug.Print "PT="; bu_Bytes2String(aData, lngDataLen) lngRet = AES_Update(hContext, aData(0), lngDataLen) Debug.Print "CT="; bu_Bytes2HexStr(aData, lngDataLen), lngRet ' Append to output string sOutput = sOutput & bu_Bytes2String(aData, lngDataLen) Next ' Show result Debug.Print "CT="; bu_Str2Hex(sOutput) Debug.Print "OK="; sCorrect lngRet = AES_Final(hContext) ' Now decrypt sInput = sOutput sOutput = "" hContext = AES_Init(aKey(0), 128, 128, False, "CBC", aInitV(0)) For i = 0 To (Len(sInput) \ lngDataLen) - 1 ' Get next block Call bu_String2Bytes(Mid(sInput, i * lngDataLen + 1, lngDataLen), aData) Debug.Print "CT="; bu_Bytes2HexStr(aData, lngDataLen) lngRet = AES_Update(hContext, aData(0), lngDataLen) Debug.Print "PT="; bu_Bytes2HexStr(aData, lngDataLen), lngRet ' Append to output string sOutput = sOutput & bu_Bytes2String(aData, lngDataLen) Next ' Show result Debug.Print "P'="; bu_Str2Hex(sOutput) Debug.Print "P'="; sOutput lngRet = AES_Final(hContext)This should result in output as follows:
KY=0123456789ABCDEFF0E1D2C3B4A59687 IV=FEDCBA9876543210FEDCBA9876543210 PT=Now is the time CT=C3153108A8DD340C0BCB1DFE8D25D232 0 PT=for all good men CT=0EE0E66BD2BB4A313FB75C5638E9E177 0 CT=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177 OK=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177 CT=C3153108A8DD340C0BCB1DFE8D25D232 PT=4E6F77206973207468652074696D6520 0 CT=0EE0E66BD2BB4A313FB75C5638E9E177 PT=666F7220616C6C20676F6F64206D656E 0 P'=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E P'=Now is the time for all good men
AES_Init
AES_InitHex
AES_UpdateHex
AES_Final
[Back to Top]
AES_Final
closes and clears the AES context.
Public Declare Function AES_Final Lib "diCryptoSys.dll"
(ByVal hContext As Long) As Long
lngRet = AES_Final(hContext)
hContext | Long containing the handle to the AES context. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
See the examples in
AES_UpdateHex
and AES_Update
.
AES_Init
AES_InitHex
AES_UpdateHex
AES_Update
[Back to Top]
AES_Ecb
carries out the block cipher transormation function in ECB mode
on a byte array
using the key schedule set up by an earlier call to AES_Init
or AES_InitHex
.
The user can set the direction of encryption independently.
Public Declare Function AES_Ecb Lib "diCryptoSys.dll"
(ByVal hContext As Long, aData As Any, ByVal lngDataLen As Long,
ByVal bEncrypt As Boolean) As Long
lngRet = AES_Ecb(hContext, aData(0), lngDataLen, bEncrypt)
hContext | Long handle to the AES context set up by an earlier call to
AES_Init or AES_InitHex . |
aData | Byte array containing the input
to be processed by the AES function and to receive the output.
|
lngDataLen | Long containing length of the data in bytes.
|
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
AES_Ecb
only uses the key schedule;
it does not affect subsequent calls to AES_Update
.
The input array aData
should be a multiple of the block length in bytes
(i.e. a multiple of 16, 24 or 32 bytes depending on the block length specified).
If not, odd trailing bytes will be ignored.
Note that the output overwrites the input.
AES_Ecb
independently of
AES_Update
.
Dim lngRet As Long Dim hContext As Long Dim aKey(15) As Byte Dim aData(15) As Byte Dim lngDataLen As Long Dim aInitV(15) As Byte Dim lngKeyLen As Long Dim nBlocks As Integer, i As Integer Dim aECBWork(15) As Byte ' Set up key and IV arrays ' ... hContext = AES_Init(aKey(0), 128&, 128&, True, "CBC", aInitV(0)) For i = 0 To nBlocks - 1 ' ... lngRet = AES_Update(hContext, aData(0), lngDataLen) ' Sneak in here and use ECB mode in both directions lngRet = AES_Ecb(hContext, aECBWork(0), 16&, True) lngRet = AES_Ecb(hContext, aECBWork(0), 16&, False) Next lngRet = AES_Final(hContext)
AES_EcbHex
carries out the block cipher transformation function in ECB mode
on a hexadecimal string
using the key schedule set up by an earlier call to AES_Init
or AES_InitHex
.
The user can set the direction of encryption independently.
Public Declare Function AES_EcbHex Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByVal sHexBlock As String,
ByVal bEncrypt As Boolean) As Long
lngRet = AES_EcbHex(hContext, sHexBlock, bEncrypt)
hContext | Long handle to the AES context set up by an earlier call to
AES_Init or AES_InitHex . |
sHexBlock | String containing the input
in hexadecimal characters
to be processed by the AES function and to receive the output.
|
bEncrypt | Boolean direction flag:
set as True to encrypt or False
to decrypt. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
AES_EcbHex
only uses the key schedule;
it does not affect subsequent calls to AES_Update
.
The length of the input string sHexBlock
should be a multiple of two times the block length in bytes
(i.e. representing multiples of 16, 24 or 32 bytes).
If not, odd trailing characters will be ignored.
Valid hexadecimal characters are [0-9A-Fa-f].
Note that the output overwrites the input.
sHexBlock must be a variable that can receive the output, not a constant.
SHA1_StringHexHash
creates a SHA-1 message digest in hexadecimal format
from a message of String
type.
Public Declare Function SHA1_StringHexHash Lib "diCryptoSys.dll"
(ByVal sDigest As String, ByVal sMessage As String) As Long
iRet = SHA1_StringHexHash(sDigest, sMessage)
sDigest | String to receive message digest. |
sMessage | String containing the message to be hashed. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim iRet As Long Dim sDigest As String ' Set sDigest to be 40 chars sDigest = String(40, " ") iRet = SHA1_StringHexHash(sDigest, "abc") Debug.Print sDigest iRet = SHA1_StringHexHash(sDigest, _ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") Debug.Print sDigestThis should result in output as follows:
a9993e364706816aba3e25717850c26c9cd0d89d 84983e441c3bd26ebaae4aa1f95129e5e54670f1
SHA1_FileHexHash
SHA1_BytesHexHash
[Back to Top]
SHA1_BytesHexHash
creates a SHA-1 message digest in hexadecimal format
from a message in Byte
array format.
Public Declare Function SHA1_BytesHexHash Lib "diCryptoSys.dll"
(ByVal sDigest As String, aByteArray As Any, ByVal lngLength As Long) As Long
iRet = SHA1_BytesHexHash(sDigest, aByteArray(0), lngLength)
sDigest | String to receive message digest. |
aByteArray | Byte array containing the message to be hashed. |
lngLength | Long containing number of bytes in the array. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim iRet As Long Dim aBytes(2) As Byte ' Create 3-byte array (NB zero-based) ' Alternative way of making sure string is 40 chars long Dim sDigest As String * 40 ' Setup byte array with "abc" aBytes(0) = Asc("a") aBytes(1) = Asc("b") aBytes(2) = Asc("c") iRet = SHA1_BytesHexHash(sDigest, aBytes(0), 3) Debug.Print iRet; sDigestThis should result in output as follows:
0 a9993e364706816aba3e25717850c26c9cd0d89d
SHA1_StringHexHash
SHA1_FileHexHash
[Back to Top]
SHA1_FileHexHash
creates a SHA-1 message digest in hexadecimal format
from a file.
Public Declare Function SHA1_FileHexHash Lib "diCryptoSys.dll"
(ByVal sDigest As String, ByVal sFilename As String, ByVal sMode As String) As Long
iRet = SHA1_FileHexHash(sDigest, sFilename, sMode)
sDigest | String to receive message digest. |
sFilename | String with full path name of file. |
sMode | String to set mode: "t" or "b" |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim iRet As Long Dim sDigest As String Dim sFilename As String sFilename = "hello.txt" ' Set sDigest to be 40 chars sDigest = String(40, " ") ' Make sha1sum in text mode (treating CR-LF as single NL) iRet = SHA1_FileHexHash(sDigest, sFilename, "t") Debug.Print "t "; iRet; sDigest ' Do in binary mode (treating CR-LF as two binary bytes) iRet = SHA1_FileHexHash(sDigest, sFilename, "b") Debug.Print "b "; iRet; sDigestFor a 13-byte text file "hello.txt" in the default directory containing the 11 characters "hello world" followed by CR-LF this should result in output as follows:
t 0 22596363b3de40b06f981fb85d82312e8c0ed511 b 0 88a5b867c3d110207786e66523cd1e4a484da697
SHA1_StringHexHash
SHA1_BytesHexHash
[Back to Top]
SHA1_Init
initialises the SHA-1 context ready for subsequent calls with
SHA1_AddString
, SHA1_AddBytes
, and SHA1_HexDigest
.
Public Declare Function SHA1_Init Lib "diCryptoSys.dll"
() As Long
Dim hContext As Long
hContext = SHA1_Init()
Long
: non-zero handle of the context hContext.
Returns zero if an error occurs.
SHA1_HexDigest
SHA1_AddString
SHA1_AddBytes
SHA1_HexDigest
SHA1_Reset
[Back to Top]
SHA1_AddString
adds a string of ascii characters to the digest.
Public Declare Function SHA1_AddString Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByVal sMessage As String) As Long
iRet = SHA1_AddString(hContext, sMessage)
hContext | Long handle to the SHA-1 context. |
sMessage | String containing the next part of the message
to be hashed. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
SHA1_Init
. This function may be called many times before creating
the final message digest with SHA1_HexDigest
This function should only be used to hash "printable" strings.
To hash a string that contains binary characters, such as ascii zero, use SHA1_AddBytes
.
Dim iRet As Long Dim sDigest As String Dim hContext As Long ' Set context handle hContext = SHA1_Init() Debug.Print "SHA1_Init() returns handle = " & Hex(hContext) ' Remember to check for an invalid handle If hContext = 0 Then MsgBox "Failed to set context" Exit Function End If ' Add strings one by one iRet = SHA1_AddString(hContext, "a") iRet = SHA1_AddString(hContext, "bc") ' Set sDigest to be 40 chars sDigest = String(40, " ") iRet = SHA1_HexDigest(sDigest, hContext) Debug.Print sDigestThis should result in output as follows:
SHA1_Init() returns handle = 10029CB0 a9993e364706816aba3e25717850c26c9cd0d89dNote that the actual value of the handle is not important, just that it should not be zero.
SHA1_Init
SHA1_AddBytes
SHA1_HexDigest
SHA1_Reset
[Back to Top]
SHA1_AddBytes
adds an array of bytes to the digest.
Public Declare Function SHA1_AddBytes Lib "diCryptoSys.dll"
(ByVal hContext As Long, aByteArray As Any, ByVal lngLength As Long) As Long
iRet = SHA1_AddBytes(hContext, aByteArray(0), lngLength)
hContext | Long handle to the SHA-1 context. |
aByteArray | Byte array containing the next part of the message
to be hashed. |
lngLength | Long containing the number of bytes in the array |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
SHA1_Init
. This function may be called many times before creating
the final message digest with SHA1_HexDigest
.
SHA1_AddString
may also be called.
SHA1_AddBytes
and SHA1_AddString
.
Dim iRet As Long Dim sDigest As String Dim hContext As Long Dim aByteArray(2) As Byte ' Set context handle hContext = SHA1_Init() ' Remember to check for an invalid handle If hContext = 0 Then MsgBox "Failed to set context" Exit Function End If ' Set up a test array of bytes aByteArray(0) = Asc("a") aByteArray(1) = &H62 ' same as Asc("b") ' Add mixture of bytes and strings iRet = SHA1_AddBytes(hContext, aByteArray(0), 2&) iRet = SHA1_AddString(hContext, "c") ' Set sDigest to be 40 chars sDigest = String(40, " ") iRet = SHA1_HexDigest(sDigest, hContext) Debug.Print sDigestThis should result in output as follows:
a9993e364706816aba3e25717850c26c9cd0d89d
SHA1_Init
SHA1_AddString
SHA1_HexDigest
SHA1_Reset
[Back to Top]
SHA1_HexDigest
returns the final message digest value as a hex string.
Public Declare Function SHA1_HexDigest Lib "diCryptoSys.dll"
(ByVal sDigest As String, ByVal hContext As Long) As Long
iRet = SHA1_HexDigest(sDigest, hContext)
sDigest | String variable long enough to receive the final message digest. |
hContext | Long handle to the SHA-1 context. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
SHA1_Init
. The string variable sDigest must
have been set up with at least 40 characters to receive the digest value in hex format.
Note that this digest operation is a destructive, read-once operation.
Once it has been performed,
the context must be reset before being used to calculate another digest value.
SHA1_AddString
and
SHA1_AddBytes
SHA1_Init
SHA1_AddString
SHA1_AddBytes
SHA1_Reset
[Back to Top]
SHA1_Reset
resets the context.
Public Declare Function SHA1_Reset Lib "diCryptoSys.dll"
(ByVal hContext As Long) As Long
iRet = SHA1_Reset(hContext)
hContext | Long handle to the SHA-1 context. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim iRet As Long
Dim hContext As Long
hContext = SHA1_Init()
iRet = SHA1_Reset(hContext)
SHA1_Init
SHA1_AddString
SHA1_AddBytes
SHA1_HexDigest
[Back to Top]
SHA1_Hmac
is a keyed-hash function that provides message
authentication using the HMAC algorithm.
Public Declare Function SHA1_Hmac Lib "diCryptoSys.dll"
(ByVal sDigest As String, aByteArray As Any, ByVal lngLength As Long,
aKeyArray As Any, ByVal lngKeyLen As Long) As Long
iRet = SHA1_Hmac(sDigest, aByteArray(0), lngLength, aKey(0), lngKeyLen)
sDigest | String variable of sufficient length to
receive the message digest in hex format. |
aByteArray | Byte array containing the text of the message. |
lngLength | Long containing the number of bytes in the array |
aKey | Byte array containing the key. |
lngKeyLen | Long containing the number of bytes in the key |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim iRet As Long Dim aBytes() As Byte Dim aKey() As Byte Dim i As Integer Dim lngLength As Long, lngKeyLen As Long Dim sDigest As String * 40 ' Test No 1. ' Set key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b ReDim aKey(15) For i = 0 To 15 aKey(i) = &HB Next ' Convert string to byte array aBytes() = StrConv("Hi There", vbFromUnicode) lngLength = UBound(aBytes) + 1 ' Create HMAC digest iRet = SHA1_Hmac(sDigest, aBytes(0), lngLength, aKey(0), 16&) Debug.Print 1; iRet; sDigest ' Test No 2. aKey() = StrConv("Jefe", vbFromUnicode) lngKeyLen = UBound(aKey) + 1 aBytes() = StrConv("what do ya want for nothing?", vbFromUnicode) lngLength = UBound(aBytes) + 1 iRet = SHA1_Hmac(sDigest, aBytes(0), lngLength, aKey(0), lngKeyLen) Debug.Print 2; iRet; sDigest ' Test No 3. ReDim aKey(15) For i = 0 To 15 aKey(i) = &HAA Next ReDim aBytes(49) For i = 0 To 49 aBytes(i) = &HDD Next iRet = SHA1_Hmac(sDigest, aBytes(0), 50&, aKey(0), 16&) Debug.Print 3; iRet; sDigestThis should result in output as follows:
1 0 675b0b3a1b4ddf4e124872da6c2f632bfed957e9 2 0 effcdf6ae5eb2fa2d27416d5f184df9c259a7c79 3 0 d730594d167e35d5956fd8003d0db3d3f46dc7bb
SHA2_StringHexHash
creates a SHA-256 message digest in hexadecimal format
from a message of String
type.
Public Declare Function SHA2_StringHexHash Lib "diCryptoSys.dll"
(ByVal sDigest As String, ByVal sMessage As String) As Long
iRet = SHA2_StringHexHash(sDigest, sMessage)
SHA-256 functions (prefixed SHA2_) are identical in syntax and usage to their SHA-1 equivalents (prefixed SHA1_). The only difference is that the message digest must be 64 hexadecimal characters long instead of 40.
Dim iRet As Long Dim sDigest As String ' Set sDigest to be 64 chars sDigest = String(64, " ") iRet = SHA2_StringHexHash(sDigest, "abc") Debug.Print sDigest iRet = SHA2_StringHexHash(sDigest, _ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") Debug.Print sDigestThis should result in output as follows:
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad 248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1
SHA2_FileHexHash
SHA2_BytesHexHash
[Back to Top]
SHA2_BytesHexHash
creates a SHA-256 message digest in hexadecimal format
from a message in Byte
array format.
Public Declare Function SHA2_BytesHexHash Lib "diCryptoSys.dll"
(ByVal sDigest As String, aByteArray As Any, ByVal lngLength As Long) As Long
iRet = SHA2_BytesHexHash(sDigest, aByteArray(0), lngLength)
SHA-256 functions (prefixed SHA2_) are identical in syntax and usage to their SHA-1 equivalents (prefixed SHA1_). The only difference is that the message digest must be 64 hexadecimal characters long instead of 40.
Dim iRet As Long Dim aBytes(2) As Byte ' Create 3-byte array (NB zero-based) ' Alternative way of making sure string is 64 chars long Dim sDigest As String * 64 ' Setup byte array with "abc" aBytes(0) = Asc("a") aBytes(1) = Asc("b") aBytes(2) = Asc("c") iRet = SHA2_BytesHexHash(sDigest, aBytes(0), 3) Debug.Print iRet; sDigestThis should result in output as follows:
0 ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
SHA2_StringHexHash
SHA2_FileHexHash
[Back to Top]
SHA2_FileHexHash
creates a SHA-256 message digest in hexadecimal format
from a file.
Public Declare Function SHA2_FileHexHash Lib "diCryptoSys.dll"
(ByVal sDigest As String, ByVal sFilename As String, ByVal sMode As String) As Long
iRet = SHA2_FileHexHash(sDigest, sFilename, sMode)
SHA-256 functions (prefixed SHA2_) are identical in syntax and usage to their SHA-1 equivalents (prefixed SHA1_). The only difference is that the message digest must be 64 hexadecimal characters long instead of 40.
Dim iRet As Long Dim sDigest As String Dim sFilename As String sFilename = "hello.txt" ' Set sDigest to be 64 chars sDigest = String(64, " ") ' Make sha1sum in text mode (treating CR-LF as single NL) iRet = SHA2_FileHexHash(sDigest, sFilename, "t") Debug.Print "t "; iRet; sDigest ' Do in binary mode (treating CR-LF as two binary bytes) iRet = SHA2_FileHexHash(sDigest, sFilename, "b") Debug.Print "b "; iRet; sDigestFor a 13-byte text file "hello.txt" in the default directory containing the 11 characters "hello world" followed by CR-LF this should result in output as follows:
t 0 a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447 b 0 572a95fee9c0f320030789e4883707affe12482fbb1ea04b3ea8267c87a890fb
SHA2_StringHexHash
SHA2_BytesHexHash
[Back to Top]
SHA2_Init
initialises the SHA-256 context ready for subsequent calls with
SHA2_AddString
, SHA2_AddBytes
, and SHA2_HexDigest
.
Public Declare Function SHA2_Init Lib "diCryptoSys.dll"
() As Long
Dim hContext As Long
hContext = SHA2_Init()
SHA-256 functions (prefixed SHA2_) are identical in syntax and usage to their SHA-1 equivalents (prefixed SHA1_). The only difference is that the message digest must be 64 hexadecimal characters long instead of 40.
SHA2_HexDigest
SHA2_AddString
SHA2_AddBytes
SHA2_HexDigest
SHA2_Reset
[Back to Top]
SHA2_AddString
adds a string of ascii characters to the digest.
Public Declare Function SHA2_AddString Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByVal sMessage As String) As Long
iRet = SHA2_AddString(hContext, sMessage)
SHA-256 functions (prefixed SHA2_) are identical in syntax and usage to their SHA-1 equivalents (prefixed SHA1_). The only difference is that the message digest must be 64 hexadecimal characters long instead of 40.
Dim iRet As Long Dim sDigest As String Dim hContext As Long Dim i As Long Dim sA1000 As String ' Set context handle hContext = SHA2_Init() If hContext = 0 Then MsgBox "Failed to set context" Exit Function End If ' Create a string of 1000 'a's sA1000 = String(1000, "a") ' Add 1000 times => one million repetitions of "a" For i = 1 To 1000 iRet = SHA2_AddString(hContext, sA1000) Next ' Set sDigest to be 64 chars - don't forget!! sDigest = String(64, " ") iRet = SHA2_HexDigest(sDigest, hContext) Debug.Print sDigestThis should result in output as follows:
cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0Note that the actual value of the handle is not important, just that it should not be zero.
SHA2_Init
SHA2_AddBytes
SHA2_HexDigest
SHA2_Reset
[Back to Top]
SHA2_AddBytes
adds an array of bytes to the digest.
Public Declare Function SHA2_AddBytes Lib "diCryptoSys.dll"
(ByVal hContext As Long, aByteArray As Any, ByVal lngLength As Long) As Long
iRet = SHA2_AddBytes(hContext, aByteArray(0), lngLength)
SHA-256 functions (prefixed SHA2_) are identical in syntax and usage to their SHA-1 equivalents (prefixed SHA1_). The only difference is that the message digest must be 64 hexadecimal characters long instead of 40.
SHA2_AddBytes
and SHA2_AddString
.
Dim iRet As Long Dim sDigest As String Dim hContext As Long Dim aByteArray(2) As Byte ' Set context handle hContext = SHA2_Init() ' Remember to check for an invalid handle If hContext = 0 Then MsgBox "Failed to set context" Exit Function End If ' Set up a test array of bytes aByteArray(0) = Asc("a") aByteArray(1) = &H62 ' same as Asc("b") ' Add mixture of bytes and strings iRet = SHA2_AddBytes(hContext, aByteArray(0), 2&) iRet = SHA2_AddString(hContext, "c") ' Set sDigest to be 64 chars sDigest = String(64, " ") iRet = SHA2_HexDigest(sDigest, hContext) Debug.Print sDigestThis should result in output as follows:
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
SHA2_Init
SHA2_AddString
SHA2_HexDigest
SHA2_Reset
[Back to Top]
SHA2_HexDigest
returns the final message digest value as a hex string.
Public Declare Function SHA2_HexDigest Lib "diCryptoSys.dll"
(ByVal sDigest As String, ByVal hContext As Long) As Long
iRet = SHA2_HexDigest(sDigest, hContext)
SHA-256 functions (prefixed SHA2_) are identical in syntax and usage to their SHA-1 equivalents (prefixed SHA1_). The only difference is that the message digest must be 64 hexadecimal characters long instead of 40.
SHA2_AddString
and
SHA2_AddBytes
SHA2_Init
SHA2_AddString
SHA2_AddBytes
SHA2_Reset
[Back to Top]
SHA2_Reset
resets the context.
Public Declare Function SHA2_Reset Lib "diCryptoSys.dll"
(ByVal hContext As Long) As Long
iRet = SHA2_Reset(hContext)
SHA-256 functions (prefixed SHA2_) are identical in syntax and usage to their SHA-1 equivalents (prefixed SHA1_). The only difference is that the message digest must be 64 hexadecimal characters long instead of 40.
Dim iRet As Long
Dim hContext As Long
hContext = SHA2_Init()
iRet = SHA2_Reset(hContext)
SHA2_Init
SHA2_AddString
SHA2_AddBytes
SHA2_HexDigest
[Back to Top]
SHA2_Hmac
is a keyed-hash function that provides message
authentication using the HMAC algorithm.
Public Declare Function SHA2_Hmac Lib "diCryptoSys.dll"
(ByVal sDigest As String, aByteArray As Any, ByVal lngLength As Long,
aKeyArray As Any, ByVal lngKeyLen As Long) As Long
iRet = SHA2_Hmac(sDigest, aByteArray(0), lngLength, aKey(0), lngKeyLen)
SHA-256 functions (prefixed SHA2_) are identical in syntax and usage to their SHA-1 equivalents (prefixed SHA1_). The only difference is that the message digest must be 64 hexadecimal characters long instead of 40.
Dim iRet As Long Dim aBytes() As Byte Dim aKey() As Byte Dim i As Integer Dim lngLength As Long, lngKeyLen As Long Dim sDigest As String * 64 ' Test No 1. ' Set key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b ReDim aKey(15) For i = 0 To 15 aKey(i) = &HB Next ' Convert string to byte array aBytes() = StrConv("Hi There", vbFromUnicode) lngLength = UBound(aBytes) + 1 ' Create HMAC digest iRet = SHA2_Hmac(sDigest, aBytes(0), lngLength, aKey(0), 16&) Debug.Print 1; iRet; sDigest ' Test No 2. aKey() = StrConv("Jefe", vbFromUnicode) lngKeyLen = UBound(aKey) + 1 aBytes() = StrConv("what do ya want for nothing?", vbFromUnicode) lngLength = UBound(aBytes) + 1 iRet = SHA2_Hmac(sDigest, aBytes(0), lngLength, aKey(0), lngKeyLen) Debug.Print 2; iRet; sDigest ' Test No 3. ReDim aKey(15) For i = 0 To 15 aKey(i) = &HAA Next ReDim aBytes(49) For i = 0 To 49 aBytes(i) = &HDD Next iRet = SHA2_Hmac(sDigest, aBytes(0), 50&, aKey(0), 16&) Debug.Print 3; iRet; sDigestThis should result in output as follows:
1 0 492ce020fe2534a5789dc3848806c78f4f6711397f08e7e7a12ca5a4483c8aa6 2 0 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843 3 0 7dda3cc169743a6484649f94f0eda0f9f2ff496a9733fb796ed5adb40a44c3c1
RAN_KeyGenerate
creates a cipher key of specified length using
a secure random number generator.
Public Declare Function RAN_KeyGenerate Lib "diCryptoSys.dll"
(ByVal abytKey As Byte, ByVal lngKeyLen As String,
ByVal bPromptUser As Boolean) As Long
lngRet = RAN_KeyGenerate(abytKey(0), lngKeyLen, bPromptUser)
abytKey | Byte array of sufficient length to receive the output. |
nKeyLen | Long value of the required key length in bytes. |
bPromptUser | Boolean prompt flag:
set as True to prompt user for extra random or secret information,
or False for silent operation. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
abytKey(0)
.
Dim abytKey(40) As Byte Dim sHexKey As String Dim lngRet As Long Dim nKeyLen As Long ' Calculate key length nKeyLen = UBound(abytKey) - LBound(abytKey) + 1 ' Generate key with user prompt lngRet = RAN_KeyGenerate(abytKey(0), nKeyLen, True) ' Convert byte array to hex string and show it sHexKey = bu_Str2Hex(bu_Bytes2String(abytKey, nKeyLen)) MsgBox sHexKey, Title:="Generated key"
RAN_KeyGenHex
RAN_DESKeyGenerate
RAN_TDEAKeyGenerate
[Back to Top]
RAN_KeyGenHex
creates a cipher key of specified length in hexadecimal format using
a secure random number generator.
Public Declare Function RAN_KeyGenerate Lib "diCryptoSys.dll"
(ByVal sHexKey As String, ByVal lngKeyBytes As Long, _
ByVal bPromptUser As Boolean) As Long
lngRet = RAN_KeyGenHex(sHexKey, lngKeyBytes, bPromptUser)
sHexKey | String of sufficient length to receive the output. |
lngKeyBytes | Long value of the required key length in
bytes. |
bPromptUser | Boolean prompt flag:
set as True to prompt user for extra random or secret information,
or False for silent operation. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim sHexKey As String Dim lngRet As Long Dim nKeyBytes As Long ' Set length of hex string to DOUBLE # bytes required nKeyBytes = 16 sHexKey = String(nKeyBytes * 2, " ") ' Generate key silently lngRet = RAN_KeyGenHex(sHexKey, nKeyBytes, False) MsgBox sHexKey, Title:="Generated key"
RAN_DESKeyGenerate
creates a random key of 8 bytes suitable
for the DES block cipher.
Public Declare Function RAN_DESKeyGenerate Lib "diCryptoSys.dll"
(ByVal abytKey As Byte,
ByVal bPromptUser As Boolean) As Long
Dim abytKey(7) As Byte lngRet = RAN_DESKeyGenerate(abytKey(0), bPromptUser)
abytKey | Byte array at least 8 bytes long. |
bPromptUser | Boolean prompt flag:
set as True to prompt user for extra random information. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
abytKey(0)
. A DES key is always 8 bytes long (64 bits).
The parity bit is set. Weak and semi-weak keys are checked for and rejected.
Dim abytKey(7) As Byte Dim sHexKey As String Dim lngRet As Long lngRet = RAN_DESKeyGenerate(abytKey(0), True) sHexKey = bu_Str2Hex(bu_Bytes2String(abytKey, 8)) MsgBox sHexKey, Title:="Generated DES key"
RAN_KeyGenerate
RAN_TDEAKeyGenerate
[Back to Top]
RAN_DESKeyGenHex
creates a random key of 8 bytes in hexadecimal format suitable
for the DES block cipher.
Public Declare Function RAN_DESKeyGenHex Lib "diCryptoSys.dll"
(ByVal sHexKey As String, ByVal bPromptUser As Boolean) As Long
lngRet = RAN_DESKeyGenHex(sHexKey, bPromptUser)
sHexKey | String of sufficient length to receive the output. |
bPromptUser | Boolean prompt flag:
set as True to prompt user for extra random information. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim sHexKey As String Dim lngRet As Long ' Set length of hex string to DOUBLE # bytes required sHexKey = String(16, " ") ' Generate key silently lngRet = RAN_DESKeyGenHex(sHexKey, False) Debug.Print sHexKey
RAN_KeyGenerate
RAN_DESKeyGenerate
[Back to Top]
RAN_TDEAKeyGenerate
creates a random key of 24 bytes suitable
for the Triple DES (TDEA) block cipher.
Public Declare Function RAN_TDEAKeyGenerate Lib "diCryptoSys.dll"
(ByVal abytKey As Byte,
ByVal bPromptUser As Boolean) As Long
Dim abytKey(23) As Byte lngRet = RAN_TDEAKeyGenerate(abytKey(0), bPromptUser)
abytKey | Byte array at least 24 bytes long. |
bPromptUser | Boolean prompt flag:
set as True to prompt user for extra random information. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
abytKey(0)
. A TDEA key is always 24 bytes long (192 bits).
The parity bit is set. Weak and semi-weak keys are rejected.
Dim abytKey(23) As Byte Dim sHexKey As String Dim lngRet As Long lngRet = RAN_TDEAKeyGenerate(abytKey(0), False) sHexKey = bu_Str2Hex(bu_Bytes2String(abytKey, 24)) MsgBox sHexKey, Title:="Generated TDEA key"
RAN_KeyGenerate
RAN_DESKeyGenerate
[Back to Top]
RAN_TDEAKeyGenHex
creates a random key of 24 bytes in hexadecimal format suitable
for the Triple-DES (TDEA) block cipher.
Public Declare Function RAN_TDEAKeyGenHex Lib "diCryptoSys.dll"
(ByVal sHexKey As String, ByVal bPromptUser As Boolean) As Long
lngRet = RAN_TDEAKeyGenHex(sHexKey, bPromptUser)
sHexKey | String of sufficient length to receive the output. |
bPromptUser | Boolean prompt flag:
set as True to prompt user for extra random information. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Dim sHexKey As String Dim lngRet As Long ' Set length of hex string to DOUBLE # bytes required sHexKey = String(48, " ") ' Generate key with prompt lngRet = RAN_TDEAKeyGenHex(sHexKey, True) Debug.Print sHexKey
RAN_KeyGenerate
RAN_DESKeyGenerate
[Back to Top]
RAN_Seed
adds a seed to the randomness pool and causes the pool to be mixed.
The user can be prompted for random keystroke timing and mouse movement data
that will be added to the pool, or the
caller can specify an array of bytes to be added to the pool, or both.
Public Declare Function RAN_Seed Lib "diCryptoSys.dll"
(ByVal abytSeed As Byte, ByVal lngSeedLen
ByVal bPromptUser As Boolean) As Long
lngRet = RAN_Seed(abytSeed(0), lngSeedLen, bPromptUser)
abytSeed | Byte array containing seed values set by the caller.
Set as vbNullString for no seed data. |
lngSeedLen | Long specifying the length of the abytSeed
array. Set as zero for no seed data. |
bPromptUser | Boolean prompt flag:
set as True to prompt user for extra random information. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
abytSeed(0)
. The value of the seed adds to
the randomness pool; it cannot directly influence the actual numbers generated by
the secure random number generator.
lngRet = RAN_Seed(vbNullString, 0, True)This will seed the pool with the three ascii characters "abc" without prompting the user.
Dim lngRet As Long Dim abytSeed(2) As Byte abytSeed(0) = &61 abytSeed(1) = &62 abytSeed(2) = &63 lngRet = RAN_Seed(abytSeed(0), 3&, False)
RAN_Test
will carry out a "Power-up" test on the next 20,000 bits
from the random number generator according to FIPS140-2.
The results and the 20,000 bit
sample are written to a text file.
Public Declare Function RAN_Test Lib "diCryptoSys.dll"
(ByVal sFilename As String) As Long
lngRet = RAN_Test(sFilename)
sFilename | String containing the full path name of a file
to be created containing the results of the test. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
sFilename
does not exist, it will be created.
If it already exists, it will be overwritten without warning. If no directory path
is specified, the file will be created in the current default directory.
For more details on the test refer to section 4.9 of FIPS140-2
[FIPS140]
or see chapter 5 of Menezes [MENE].
lngRet = RAN_Test("Fips140t.txt")Will create the results file "Fips140t.txt" in the default directory. An example file is
FIPS140-2 test for 20000 bits (2500 bytes) at Fri Aug 03 05:10:16 2001 1. Monobits test n1 = 10052 (9725 - 10275) Passed Monobits test. 2. Poker test X = 5.09 (2.16 - 46.17) Passed Poker test. 3. Runs test: Run len: 1 2 3 4 5 6+ Gaps: 2516, 1281, 612, 286, 147, 167 Blocks: 2500, 1231, 646, 329, 145, 158 Min: (2343, 1135, 542, 251, 111, 111) Avg: (2500, 1250, 625, 312, 156, 156) Max: (2657, 1365, 708, 373, 201, 201) Pass: Y Y Y Y Y Y Passed Runs test. 4. Long runs test: Gmax = 14 Bmax = 13 (26) Passed Long Run test. Passed FIPS140-2 test. Test sample was:- 5E2B9DDC9EE2DEB8F13A8339E571C37...
RAN_Nonce
returns a random nonce (number used once) as a specified-length
byte array.
Public Declare Function RAN_Nonce Lib "diCryptoSys.dll"
(ByVal abytNonce As Any, lngNonceLen As Long) As Long
lngRet = RAN_Nonce(abytNonce(0), lngNonceLen)
abytNonce | Byte array to be filled with
random values. |
lngNonceLen | Long specifying the number of
required bytes in the nonce. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
abytNonce
must be at least lngNonceLen
bytes long.
Note the (0) in abytNonce(0). RAN_Nonce
uses a different generator from the
RAN_KeyGenerate
function and is suitable when random but not necessarily unpredictable data are required.
Dim abytNonce(10) As Byte Dim sHex As String Dim lngRet As Long Dim lngNonceLen As Long lngNonceLen = UBound(abytNonce) - LBound(abytNonce) + 1 lngRet = RAN_Nonce(abytNonce(0), lngNonceLen) ' Convert to hex string sHex = bu_Str2Hex(bu_Bytes2String(abytNonce, lngNonceLen)) MsgBox sHex, Title:="Generated nonce"
RAN_NonceHex
RAN_Long
RAN_KeyGenerate
[Back to Top]
RAN_NonceHex
returns a specified-length random nonce (number used once) as a
hexadecimal string.
Public Declare Function RAN_NonceHex Lib "diCryptoSys.dll"
(ByVal sHexData As String, ByVal lngNonceLen As Long) As Long
lngRet = RAN_NonceHex(sHexData, lngNonceLen)
sHexData | String array to be filled with
random values expressed in hexadecimal. |
lngNonceLen | Long specifying the number of
required bytes in the nonce. |
Long
: If successful, the return value is 0;
otherwise it returns a non-zero error code.
sHexData
must be filled with dummy characters
to two times lngNonceLen
before calling the function.
RAN_NonceHex
uses a different generator from the
RAN_KeyGenerate
function and is suitable when random but not necessarily unpredictable data are required.
Dim sHexData As String Dim lngRet As Long ' Set hex string length to 2 x # bytes required. sHexData = String(40, " ") lngRet = RAN_NonceHex(sHexData, 20&) Debug.Print sHexData
RAN_Nonce
RAN_Long
RAN_KeyGenerate
[Back to Top]
RAN_Long
returns a random Long
number between specified limits.
Public Declare Function RAN_Long Lib "diCryptoSys.dll"
(ByVal lngLower As Long, ByVal lngUpper As Long) As Long
lngRandom = RAN_Long(lngLower, lngUpper)
lngLower | Long specifying a lower limit. |
lngUpper | Long specifying an upper limit. |
Long
: random number between lngLower and lngUpper.
RAN_Long
uses the RAN_Nonce
generator.
Dim i As Integer For i = 1 To 10 Debug.Print RAN_Long(-1000000, 1000000) NextThis will generate 8 random bits:
Dim i As Integer For i = 1 To 8 Debug.Print RAN_Long(0, 1); Next Debug.Print
A full set of the required declaration statements are provided in the module basCryptoSys
.
0 = OK, no error 1 = Cannot open input file 2 = Cannot create output file 3 = File read error 4 = File write error 8 = Input not multiple of 8 bytes long 33 = Invalid key length 34 = Invalid block length 35 = Invalid mode 39 = Unexpected Null string found 48 = Invalid key 49 = Invalid block 50 = Invalid hexadecimal character 51 = Invalid initialisation vector 64 = Invalid context handle 1024 = Not enough memory 1281 = Random number memory error 1282 = Random number generator power-up test failure 1283 = Continuous random number generator test failure 1284 = Random number setup error
The source code used in CryptoSys is original code written by David Ireland except for the following:
Version 1. First published September 2001 by DI Management Systems Pty Limited.
Copyright © 2001 D.I. Management Services Pty Limited ABN 78 083 210 584,
Sydney, Australia.
All rights reserved.
<www.di-mgt.com.au>