📄 descryptoserviceprovider.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 = "DESCryptoServiceProvider"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2006 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: DESCryptoServiceProvider
'
''
' Defines a wrapper class around a DES cryptographic service provider.
'
' @remarks This provides access to the Windows CryptoAPI cryptography services.
' @see DES
' @include "..\..\..\Includes\DESCryptoServiceProviderExample.txt"
Option Explicit
Implements IObject
Implements SymmetricAlgorithm
Implements DES
Private Const DEF_BLOCKSIZE As Long = 64
Private Const DEF_MINBLOCKSIZE As Long = DEF_BLOCKSIZE
Private Const DEF_MAXBLOCKSIZE As Long = DEF_BLOCKSIZE
Private Const DEF_BLOCKSKIPSIZE As Long = 0
Private Const DEF_FEEDBACKSIZE As Long = 8
Private Const DEF_MINKEYSIZE As Long = 64
Private Const DEF_MAXKEYSIZE As Long = 64
Private Const DEF_KEYSIZE As Long = DEF_MINKEYSIZE
Private Const DEF_KEYSKIPSIZE As Long = 0
Private WithEvents mBase As SymmetricAlgorithmBase
Attribute mBase.VB_VarHelpID = -1
''
' Returns the block size, in bits, used by this algorithm.
'
' @return The block size in bits.
' @remarks This will always return 64 bits (8 bytes).
'
Public Property Get BlockSize() As Long
BlockSize = mBase.BlockSize
End Property
''
' Sets the block size, in bits, used by this algorithm.
'
' @param RHS The new block size in bits.
' @remarks The only valid block size is 64 bits.
'
Public Property Let BlockSize(ByVal RHS As Long)
mBase.BlockSize = RHS
End Property
''
' Returns a list of legal block sizes.
'
' @return A list of legal block sizes.
' @remarks Only one block size is legal, 64 bits.
'
Public Property Get LegalBlockSizes() As KeySizes()
LegalBlockSizes = mBase.LegalBlockSizes
End Property
''
' Returns the Feedback Size in bits.
'
' @return The Feedback size in bits.
' @remarks Always returns 64 bits.
'
Public Property Get FeedbackSize() As Long
FeedbackSize = mBase.FeedbackSize
End Property
''
' Sets the Feedback size in bits.
'
' @param RHS The new feedback size in bits.
' @remarks Only 64 bits is valid.
'
Public Property Let FeedbackSize(ByVal RHS As Long)
mBase.FeedbackSize = RHS
End Property
''
' Returns the key size in bits.
'
' @Return The key size in bits.
'
Public Property Get KeySize() As Long
KeySize = mBase.KeySize
End Property
''
' Sets the key size in bits.
'
' @param RHS The new key size in bits.
' @remarks Only 64 bits is supported for DES encryption.
'
Public Property Let KeySize(ByVal RHS As Long)
mBase.KeySize = RHS
End Property
''
' Returns a list of valid key sizes supported by this algorithm.
'
' @return A list of valid key sizes.
'
Public Property Get LegalKeySizes() As KeySizes()
LegalKeySizes = mBase.LegalKeySizes
End Property
''
' Returns the Cipher mode this algorithm will use.
'
' @return The Cipher mode.
'
Public Property Get Mode() As CipherMode
Mode = mBase.Mode
End Property
''
' Sets the Cipher mode this algorithm will use.
'
' @param RHS The cipher mode to use.
'
Public Property Let Mode(ByVal RHS As CipherMode)
mBase.Mode = RHS
End Property
''
' Returns the padding mode being used.
'
' @return The current padding mode.
'
Public Property Get Padding() As PaddingMode
Padding = mBase.Padding
End Property
''
' Sets the padding mode to be used.
'
' @param RHS The new padding mode.
'
Public Property Let Padding(ByVal RHS As PaddingMode)
mBase.Padding = RHS
End Property
''
' Returns the current secret key.
'
' @return The secret key.
' @remarks If no key has been set, then a new random key will be generated.
'
Public Property Get Key() As Byte()
Key = mBase.GetKey
End Property
''
' Sets the secret key.
'
' @param RHS The new secret key.
' @param The key must be a valid key length. Currently only 64 bits (8 bytes) is supported.
'
Public Property Let Key(ByRef RHS() As Byte)
Call mBase.SetKey(RHS)
End Property
''
' Generates a random key to be used.
'
Public Sub GenerateKey()
Call mBase.GenerateKey
End Sub
''
' Returns the Initialization Vector.
'
' @return The Initialization Vector.
' @remarks If no IV has been set, then a new random IV will be generated.
'
Public Property Get IV() As Byte()
IV = mBase.GetIV
End Property
''
' Sets the Initialization Vector to be used.
'
' @param RHS The new IV to use.
' @remarks The array must be 8 bytes in length.
'
Public Property Let IV(ByRef RHS() As Byte)
Call mBase.SetIV(RHS)
End Property
''
' Generates a random IV array to use.
'
Public Sub GenerateIV()
Call mBase.GenerateIV
End Sub
''
' Clears the Key and IV arrays.
'
Public Sub Clear()
Call mBase.Clear
End Sub
''
' Tests if a specific bit length is valid for a key.
'
' @param BitLength The number of bits to test for.
' @return Return True if the number of bits is supported, False otherwise.
' @remarks Only 64bits (8 bytes) is supported.
'
Public Function ValidKeySize(ByVal BitLength As Long) As Boolean
ValidKeySize = mBase.ValidKeySize(BitLength)
End Function
''
' Returns a new cipher used to encrypt data.
'
' @param RgbKey A supplied byte array used as the secret key.
' @param RgbIV A supplied byte array used for the Initialization Vector (IV).
' @return An ICryptoTransform object used to encrypt data.
' @remarks Both parameters must be either supplied or missing. A single parameter will throw an exception.
' <p>If no parameters are supplied then the key and IV will be retrieved through the Key and IV properties.</p>
'
Public Function CreateEncryptor(Optional ByRef RgbKey As Variant, Optional ByRef RgbIV As Variant) As ICryptoTransform
Set CreateEncryptor = CreateCipher(RgbKey, RgbIV, True)
End Function
''
' Returns a new cipher used to decrypt data.
'
' @param RgbKey A supplied byte array used as the secret key.
' @param RgbIV A supplied byte array used for the Initialization Vector (IV).
' @return An ICryptoTransform object used to dencrypt data.
' @remarks Both parameters must be either supplied or missing. A single parameter will throw an exception.
' <p>If no parameters are supplied then the key and IV will be retrieved through the Key and IV properties.</p>
'
Public Function CreateDecryptor(Optional ByRef RgbKey As Variant, Optional ByRef RgbIV As Variant) As ICryptoTransform
Set CreateDecryptor = CreateCipher(RgbKey, RgbIV, False)
End Function
''
' This function determines if the value passed in is the same
' as the current object instance. Meaning, are the Value and
' this object the same object in memory.
'
' @param Value The value to compare against this instance.
' @return Returns True if the values are the same.
'
Public Function Equals(ByRef Value As Variant) As Boolean
Equals = Object.Equals(Me, Value)
End Function
''
' Returns a psuedo-unique number used to help identify this
' object in memory. The current method is to return the value
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -