⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clscryptoapi.cls

📁 使用VB写的加密算法库
💻 CLS
📖 第 1 页 / 共 5 页
字号:
  ' valid. This function does not destroy key containers or key pairs.
  Private Declare Function CryptReleaseContext Lib "advapi32.dll" _
          (ByVal hProv As Long, ByVal dwFlags As Long) As Long

  ' The data produced by this function is cryptographically random.  The
  ' data is far more random than the data generated by the typical random
  ' number generator such as the one shipped with your C or VB compiler.
  Private Declare Function CryptGenRandom Lib "advapi32.dll" _
          (ByVal hProv As Long, ByVal dwLen As Long, _
          ByVal pbBuffer As String) As Long

  ' The CryptGetUserKey function retrieves a handle of one of a user's two
  ' public/private key pairs. This function is used only by the owner of
  ' the public/private key pairs and only when the handle of a cryptographic
  ' service provider (CSP) and its associated key container is available.
  Private Declare Function CryptGetUserKey Lib "advapi32.dll" _
          (ByVal hProv As Long, ByVal dwKeySpec As Long, _
          phUserKey As Long) As Long

  ' The CryptGenKey function generates a random cryptographic session key or
  ' a public/private key pair. A handle to the key or key pair is returned
  ' in phKey. This handle can then be used as needed with any CryptoAPI
  ' function requiring a key handle.  The calling application must specify
  ' the algorithm when calling this function. Because this algorithm type is
  ' kept bundled with the key, the application does not need to specify the
  ' algorithm later when the actual cryptographic operations are performed.
  Private Declare Function CryptGenKey Lib "advapi32.dll" _
          (ByVal hProv As Long, ByVal algid As Long, _
          ByVal dwFlags As Long, phKey As Long) As Long

  ' The CryptDeriveKey function generates cryptographic session keys derived
  ' from a base data value. This function guarantees that when the same CSP
  ' and algorithms are used, the keys generated from the same base data are
  ' identical. The base data can be a password or any other user data.  This
  ' function is the same as CryptGenKey, except that the generated session
  ' keys are derived from base data instead of being random. CryptDeriveKey
  ' can only be used to generate session keys. It cannot generate
  ' public/private key pairs.
  Private Declare Function CryptDeriveKey Lib "advapi32.dll" _
          (ByVal hProv As Long, ByVal algid As Long, _
          ByVal hBaseData As Long, ByVal dwFlags As Long, _
          ByRef phKey As Long) As Long
    
  ' The CryptDestroyKey function releases the handle referenced by the hKey
  ' parameter. After a key handle has been released, it becomes invalid and
  ' cannot be used again.
  Private Declare Function CryptDestroyKey Lib "advapi32.dll" _
          (ByVal hkey As Long) As Long
    
  ' The CryptGetKeyParam function retrieves data that governs the operations
  ' of a key. If the Microsoft Cryptographic Service Provider is used, the
  ' base symmetric keying material is not obtainable by this function or any
  ' other function.
  Private Declare Function CryptGetKeyParam Lib "advapi32.dll" _
          (ByVal hkey As Long, ByVal dwParam As Long, _
          ByVal pbData As Long, pdwDataLen As Long, _
          ByVal dwFlags As Long) As Long

  ' The CryptSetKeyParam function customizes various aspects of a session
  ' key's operations. The values set by this function are not persisted
  ' to memory and can only be used with in a single session.
  Private Declare Function CryptSetKeyParam Lib "advapi32.dll" _
          (ByVal hkey As Long, ByVal dwParam As Long, _
          ByVal pbData As Long, ByVal dwFlags As Long) As Long
  
  ' The CryptExportKey function exports a cryptographic key or a key pair
  ' from a cryptographic service provider (CSP) in a secure manner.
  Private Declare Function CryptExportKey Lib "advapi32.dll" _
          (ByVal hkey As Long, ByVal hExpKey As Long, _
          ByVal dwBlobType As Long, ByVal dwFlags As Long, _
          ByVal pbData As Long, pdwDataLen As Long) As Long

  ' The CryptImportKey function transfers a cryptographic key from a key
  ' BLOB into a cryptographic service provider (CSP).This function can be
  ' used to import an Schannel session key, regular session key, public
  ' key, or public/private key pair. For all but the public key, the key
  ' or key pair is encrypted.
  Private Declare Function CryptImportKey Lib "advapi32.dll" _
          (ByVal hProv As Long, ByVal pbData As Long, _
          ByVal dwDataLength As Long, ByVal hPubKey As Long, _
          ByVal dwFlags As Long, pKeyval As Long) As Long
  
  ' The CryptEncrypt function encrypts data. The algorithm used to encrypt
  ' the data is designated by the key held by the CSP module and is
  ' referenced by the hKey parameter.
  Private Declare Function CryptEncrypt Lib "advapi32.dll" _
          (ByVal hkey As Long, ByVal hhash As Long, ByVal Final As Long, _
          ByVal dwFlags As Long, ByVal pbData As String, _
          ByRef pdwDataLen As Long, ByVal dwBufLen As Long) As Long
    
  ' The CryptDecrypt function decrypts data previously encrypted using
  ' CryptEncrypt function.
  Private Declare Function CryptDecrypt Lib "advapi32.dll" _
          (ByVal hkey As Long, ByVal hhash As Long, _
          ByVal Final As Long, ByVal dwFlags As Long, _
          ByVal pbData As String, ByRef pdwDataLen As Long) As Long
    
  ' The CryptGetProvParam function retrieves parameters that govern the
  ' operations of a cryptographic service provider (CSP).
  Private Declare Function CryptGetProvParam Lib "advapi32.dll" _
          (ByVal hProv As Long, ByVal dwParam As Long, _
          pbData As Any, pdwDataLen As Long, _
          ByVal dwFlags As Long) As Long
          
  ' Alias of CryptGetProvParam
  Private Declare Function CryptGetProvParamString Lib "advapi32.dll" _
          Alias "CryptGetProvParam" (ByVal hProv As Long, _
          ByVal dwParam As Long, ByVal pbData As String, _
          pdwDataLen As Long, ByVal dwFlags As Long) As Long

          
' ***************************************************************************
'                        Property area
' ***************************************************************************
Public Property Let InputData(arInData() As Byte)
Attribute InputData.VB_Description = "Input only.  Data to be Encrypted/Decrypted."

' ---------------------------------------------------------------------------
' Input data only in byte array
' ---------------------------------------------------------------------------
  m_strInputData = ByteArrayToString(arInData())

End Property

Public Property Get OutputData() As Byte()
Attribute OutputData.VB_Description = "Read only.  Return encrypted/decrypted data in byte array format."
  
' ---------------------------------------------------------------------------
' Output data only in byte array
' ---------------------------------------------------------------------------
  OutputData = m_abytOutputData

End Property

Public Property Get EnhancedProvider() As Boolean
Attribute EnhancedProvider.VB_Description = "Returns a boolean flag designating if the enhanced provider is being used."
  
' ---------------------------------------------------------------------------
' Output data only as TRUE or FALSE.
' ---------------------------------------------------------------------------
  EnhancedProvider = m_blnEnhancedProvider

End Property

Public Property Let EnhancedProvider(blnUseEnhanced As Boolean)
Attribute EnhancedProvider.VB_Description = "Boolean flag to designate if the enhanced provider is to be used."
  
' ---------------------------------------------------------------------------
' Designates whether or not to use the MS Enhanced provider.
' ---------------------------------------------------------------------------
  m_blnEnhancedProvider = blnUseEnhanced

End Property

Public Property Let Password(arPWord() As Byte)
Attribute Password.VB_Description = "Receives user supplied password in byte array format."
  
' ---------------------------------------------------------------------------
' Receives user supplied password in byte array format.
' ---------------------------------------------------------------------------
  Erase m_abytPWord()                  ' Empty module level password array
  ReDim m_abytPWord(0)                 ' resize to smallest size
  
' ---------------------------------------------------------------------------
' Test input
' ---------------------------------------------------------------------------
  If UBound(arPWord) > 0 Then
      ' Transfer password to byte array
      m_abytPWord = arPWord            ' transfer pass array to module array
      Erase arPWord()                  ' erase passed array
  Else
      If m_blnUseDefaultPWD Then
          m_abytPWord = GetPassword(True)   ' Use the default password
      End If
  End If
  
End Property

Public Property Get Password() As Byte()
Attribute Password.VB_Description = "Returns password in byte array format."
  
' ---------------------------------------------------------------------------
' Returns password in byte array format.
' ---------------------------------------------------------------------------
  Password = m_abytPWord()
  
End Property

Public Property Let UseDefaultPWD(blnUseDefaultPWD As Boolean)
Attribute UseDefaultPWD.VB_Description = "Input only.  Boolean flag used to override Password property.  TRUE=Use default password.  FALSE=Create random data password (used for initialization vectors)."
  
' ---------------------------------------------------------------------------
' Input only.  Used to override Password property.
'      [Default] TRUE  = Use default password.
'                FALSE = Create random data password. Used for initialization
'                        vectors.
' ---------------------------------------------------------------------------

' ---------------------------------------------------------------------------
' See if we are to use the default password or create one on the fly
' ---------------------------------------------------------------------------
  m_blnUseDefaultPWD = blnUseDefaultPWD
  
End Property

' ***************************************************************************
'                    Functions and Procedures
' ***************************************************************************
Public Function ByteArrayToString(arByte() As Byte) As String
Attribute ByteArrayToString.VB_Description = "Convert a byte array to string format"

' ***************************************************************************
' Routine:       ByteArrayToString
'
' Description:   Convert a byte array to string format
'
' Parameters:    arByte() - Incoming data in byte format
'
' Returns:       Single string of data
'
' ===========================================================================
'    DATE      NAME / eMAIL
'              DESCRIPTION
' -----------  --------------------------------------------------------------
' 03-OCT-2000  Kenneth Ives  kenaso@home.com
'              Modified and documented
' 20-JAN-2001  VB2-The-Max   http://www.vb2themax.com/
'              From an article titled "10 Hot Tips from VB-2-the-Max"
'              by Francesco Balena
'              This is tip no.9 on faster string concatenation with a little
'              modification.
' 24-JUL-2001  Kenneth Ives  kenaso@home.com
'              Modified and added documentation
' ***************************************************************************
    
' ---------------------------------------------------------------------------
' Define variables
' ---------------------------------------------------------------------------
  Dim lngLoop          As Long
  Dim lngMax           As Long
  Dim lngLength        As Long
  Dim lngPaddingLen    As Long
  Dim lngIndexPointer  As Long
  Dim strTemp          As String
  Dim strOutput        As String
  
  Const ADD_SPACES     As Long = 10000
  
' ---------------------------------------------------------------------------
' Determine amount of data in the byte array.
' ---------------------------------------------------------------------------
  strTemp = ""
  lngIndexPointer = 1                 ' index pointer for output string
  lngMax = UBound(arByte)             ' determine number of elements in array
  lngPaddingLen = (ADD_SPACES * 9)    ' 90000 blank spaces
  strOutput = Space$(lngPaddingLen)   ' preload output string
  
' ---------------------------------------------------------------------------
' Unload the byte array and convert each character back to its ASCII
' character value
' ---------------------------------------------------------------------------
  For lngLoop = 0 To lngMax - 1
      
      strTemp = Chr$(arByte(lngLoop))  ' Convert each byte to an ASCII character
      lngLength = Len(strTemp)         ' save the length of the converted data
      
      ' see if some more padding has to be added to the output string
      If (lngIndexPointer + lngLength) >= lngPaddingLen Then
          lngPaddingLen = lngPaddingLen + ADD_SPACES ' boost blank space counter
          strOutput = strOutput & Space$(ADD_SPACES) ' append some blank spaces
      End If
      
      ' insert data into output string
      Mid$(strOutput, lngIndexPointer, lngLength) = strTemp
      
      ' increment output string pointer
      lngIndexPointer = lngIndexPointer + lngLength
             
  Next
  
' ---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -