📄 clscryptoapi.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsCryptoAPI"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
' ***************************************************************************
' Module: clsCryptoAPI.cls
'
' Description: This module is used to make calls to the the advapi32.dll
' where the functions for CryptoAPI reside.
'
' Always give credit where credit is due. If you attach your
' creditials to a piece of code, you should be available to
' answer questions concerning that code.
'
' Thanks to: Phil Fresle http://www.frez.co.uk
' Found a lot of good code snippets at his site.
' Some you will recognize in this module.
' Kevin Matthew Goss
' His hashing routine pointed me in the right
' direction.
' Alex Rohr arohr@ub2b.com
' Collected ideas from his file encryption class.
' ===========================================================================
' DATE NAME / eMAIL
' DESCRIPTION
' ----------- --------------------------------------------------------------
' 29-DEC-2000 Kenneth Ives kenaso@home.com
' Original module
' 10-JUL-2001 Kenneth Ives kenaso@home.com
' Converted to a DLL
' 09-SEP-2001 Kenneth Ives kenaso@home.com
' Enhanced and fixed some minor bugs
' ***************************************************************************
' ---------------------------------------------------------------------------
' Module level variables
' ---------------------------------------------------------------------------
Private m_blnEnhancedProvider As Boolean
Private m_blnBlockCipher As Boolean
Private m_blnUseDefaultPWD As Boolean
Private m_lngCryptContext As Long
Private m_strInputData As String
Private m_abytOutputData() As Byte
Private m_abytPWord() As Byte
' Export keys
Private Const SIMPLEBLOB As Long = 1
Private Const PUBLICKEYBLOB As Long = 6
Private Const PRIVATEKEYBLOB As Long = 7
Private Const PLAINTEXTKEYBLOB As Long = 8
' Algorithm classes
Private Const ALG_CLASS_ANY As Long = 0
Private Const ALG_CLASS_SIGNATURE As Long = 8192
Private Const ALG_CLASS_MSG_ENCRYPT As Long = 16384
Private Const ALG_CLASS_DATA_ENCRYPT As Long = 24576
Private Const ALG_CLASS_HASH As Long = 32768
' Algorithm types
Private Const ALG_TYPE_ANY As Long = 0
Private Const ALG_TYPE_BLOCK As Long = 1536
Private Const ALG_TYPE_STREAM As Long = 2048
' Block cipher IDs
Private Const ALG_SID_DES As Long = 1
Private Const ALG_SID_RC2 As Long = 2
Private Const ALG_SID_3DES As Long = 3
Private Const ALG_SID_3DES_112 As Long = 9
' Stream cipher IDs
Private Const ALG_SID_RC4 As Long = 1
' Hash IDs
Private Const ALG_SID_MD2 As Long = 1
Private Const ALG_SID_MD4 As Long = 2
Private Const ALG_SID_MD5 As Long = 3
Private Const ALG_SID_SHA As Long = 4
Private Const ALG_SID_SHA1 As Long = 4
Private Const HP_HASHVAL As Long = 2
' Hash algorithms
Private Const CALG_MD2 As Long = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2
Private Const CALG_MD4 As Long = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4
Private Const CALG_MD5 As Long = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5
Private Const CALG_SHA As Long = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA
Private Const CALG_SHA1 As Long = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1
' Block ciphers
Private Const CALG_RC2 As Long = ALG_CLASS_DATA_ENCRYPT Or ALG_TYPE_BLOCK Or ALG_SID_RC2
Private Const CALG_DES As Long = ALG_CLASS_DATA_ENCRYPT Or ALG_TYPE_BLOCK Or ALG_SID_DES
Private Const CALG_3DES As Long = ALG_CLASS_DATA_ENCRYPT Or ALG_TYPE_BLOCK Or ALG_SID_3DES
Private Const CALG_3DES_112 As Long = ALG_CLASS_DATA_ENCRYPT Or ALG_TYPE_BLOCK Or ALG_SID_3DES_112
' Stream cipher
Private Const CALG_RC4 As Long = ALG_CLASS_DATA_ENCRYPT Or ALG_TYPE_STREAM Or ALG_SID_RC4
' CryptSetProvParam
Private Const PROV_RSA_FULL As Long = 1
' used when aquiring the provider
Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000
Private Const CRYPT_NEWKEYSET As Long = &H8&
' Microsoft provider data
Private Const MS_DEFAULT_PROVIDER As String = _
"Microsoft Base Cryptographic Provider v1.0"
Private Const MS_ENHANCED_PROVIDER As String = _
"Microsoft Enhanced Cryptographic Provider v1.0"
' ---------------------------------------------------------------------------
' Error codes
' ---------------------------------------------------------------------------
Private Const ERR_CONTEXTOPEN As Long = 100
Private Const ERR_LOCKED As Long = 101
Private Const ERR_NOCONTEXT As Long = 102
Private Const ERR_KEYNOTVALID As Long = 103
' ---------------------------------------------------------------------------
' Numbers defined by GetLastError
' ---------------------------------------------------------------------------
Private Const ERROR_BUSY As Long = 170
Private Const ERROR_INVALID_PARAMETER As Long = 87
Private Const ERROR_NOT_ENOUGH_MEMORY As Long = 8
Private Const ERROR_MORE_DATA As Long = 234
Private Const NTE_BAD_DATA As Long = &H80090005
' ---------------------------------------------------------------------------
' Error messages
' ---------------------------------------------------------------------------
Private Const ERROR_AQUIRING_CONTEXT As String = "Could not acquire context"
Private Const ERROR_CREATING_HASH As String = "Could not create hash"
Private Const ERROR_CREATING_HASH_DATA As String = "Could not create hash data"
Private Const ERROR_DERIVING_KEY As String = "Could not derive key"
Private Const ERROR_ENCRYPTING_DATA As String = "Could not encrypt data"
Private Const ERROR_DECRYPTING_DATA As String = "Could not decrypt data"
Private Const ERROR_INVALID_HEX_STRING As String = "Not a valid hex string"
Private Const ERROR_MISSING_PARAMETER As String = "Both a string and a key are required"
Private Const ERROR_BAD_ENCRYPTION_TYPE As String = "Invalid encryption type specified"
' ---------------------------------------------------------------------------
' Declares
' ---------------------------------------------------------------------------
' CopyMemory moves the contents of a portion of memory from one location
' to another. The two locations are identified by pointers to the memory
' addresses. After the copy, the original contents in the source are set
' to zeros.
'
' Useful whenever you want to move a block of bytes between two memory
' locations. When the source or the destination is an array of numbers
' (or of UDTs that contains only numeric and fixed-length strings), you
' must pass the first element of the array by reference. Example below
' depicts zero based arrays.
'
' Copy the first 1000 elements of array a() to b(). Both arrays must be
' of the same type, and cannot be objects or variable-length strings.
'
' CopyMemory b(0), a(0), 1000 * Len(a(0))
'
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(dest As Any, source As Any, ByVal bytes As Long)
' The GetTickCount() API will capture the time in milliseconds. The
' counter overflows after 1192.8 hours (49.7 days) from the last reboot.
Private Declare Function GetTickCount Lib "kernel32" () As Long
' The GetLastError function returns the calling thread's last-error
' code value. Most Win32 functions set their calling thread's
' last-error value when they fail; a few functions set it when they
' succeed. You should call the GetLastError function immediately when
' a function's return value indicates that such a call will return
' useful data. That is because some functions call SetLastError(0) when
' they succeed, wiping out the error code set by the most recently
' failed function.
Private Declare Function GetLastError Lib "kernel32" () As Long
' The CryptHashData function adds data to a specified hash object.
' This function and CryptHashSessionKey can be called multiple
' times to compute the hash of long or discontinuous data streams.
Private Declare Function CryptHashData Lib "advapi32.dll" _
(ByVal hhash As Long, ByVal pbData As String, _
ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
' Alias of CryptHashData
Private Declare Function CryptHashDataString Lib "advapi32.dll" _
Alias "CryptHashData" (ByVal hhash As Long, _
ByVal bData As String, ByVal dwDataLen As Long, _
ByVal dwFlags As Long) As Long
' Alias of CryptHashData
Private Declare Function CryptHashDataBytes Lib "advapi32.dll" _
Alias "CryptHashData" (ByVal hhash As Long, _
bData As Byte, ByVal dwDataLen As Long, _
ByVal dwFlags As Long) As Long
' The CryptCreateHash function initiates the hashing of a stream of
' data. It creates and returns to the calling application a handle
' to a CSP hash object. This handle is used in subsequent calls to
' CryptHashData and CryptHashSessionKey to hash session keys and
' other streams of data.
Private Declare Function CryptCreateHash Lib "advapi32.dll" _
(ByVal hProv As Long, ByVal algid As Long, _
ByVal hkey As Long, ByVal dwFlags As Long, _
ByRef phHash As Long) As Long
' The CryptSignHash function signs data. Because all signature
' algorithms are asymmetric and thus slow, the CryptoAPI does not
' allow data be signed directly. Instead, data is first hashed and
' CryptSignHash is used to sign the hash.
Private Declare Function CryptSignHash Lib "advapi32.dll" _
Alias "CryptSignHashA" (ByVal hhash As Long, _
ByVal hkey As Long, ByVal Description As Long, _
ByVal dwFlags As Long, ByVal pData As Long, _
dwDataLength As Long) As Long
' The CryptVerifySignature function verifies the signature of a
' hash object. Before calling this function, CryptCreateHash must be
' called to create the handle of a hash object. CryptHashData or
' CryptHashSessionKey is then used to add data or session keys to the
' hash object.
Private Declare Function CryptVerifySignature Lib "advapi32.dll" _
Alias "CryptVerifySignatureA" (ByVal hhash As Long, _
ByVal pData As Long, ByVal datalength As Long, _
ByVal PublicKey As Long, ByVal Description As Long, _
ByVal dwFlags As Long) As Long
' The CryptGetHashParam function retrieves data that governs the
' operations of a hash object. The actual hash value can be
' retrieved by using this function.
Private Declare Function CryptGetHashParam Lib "advapi32.dll" _
(ByVal hhash As Long, ByVal dwParam As Long, ByVal pbData As String, _
pdwDataLen As Long, ByVal dwFlags As Long) As Long
' Alias of CryptGetHashParam
Private Declare Function CryptGetHashParamSize Lib "advapi32.dll" _
Alias "CryptGetHashParam" (ByVal hhash As Long, _
ByVal dwParam As Long, pbData As Long, _
dwDataLength As Long, ByVal dwFlags As Long) As Long
'The CryptDestroyHash function destroys the hash object referenced
' by the hHash parameter. After a hash object has been destroyed,
' it can no longer be used. The destruction of hash objects after
' their use is finished is recommended for security reasons.
Private Declare Function CryptDestroyHash Lib "advapi32.dll" _
(ByVal hhash As Long) As Long
' The CryptAcquireContext function is used to acquire a handle to a
' particular key container within a particular cryptographic service
' provider (CSP). This returned handle can then be used to make
' calls to the selected CSP. This function performs two operations.
' It first attempts to find a CSP with the characteristics described
' in the dwProvType and pszProvider parameters. If the CSP is found,
' the function attempts to find a key container within the CSP
' matching the name specified by the pszContainer parameter. With the
' appropriate setting of dwFlags, this function can also create and
' destroy key containers.
Private Declare Function CryptAcquireContext Lib "advapi32.dll" _
Alias "CryptAcquireContextA" (ByRef phProv As Long, _
ByVal pszContainer As String, ByVal pszProvider As String, _
ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
' The CryptReleaseContext function releases the handle of a
' cryptographic service provider (CSP) and a key container. At each
' call to this function, the reference count on the CSP is reduced
' by one. When the reference count reaches zero, the context is fully
' released and it can no longer be used by any function in the application.
' An application calls this function after finishing the use of the CSP.
' After this function is called, the released CSP handle is no longer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -