rc2cryptoserviceprovider.cls

来自「VB 加密----------能够加密解密控件」· CLS 代码 · 共 616 行 · 第 1/2 页

CLS
616
字号
' <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
' obtained from ObjPtr. If a different method needs to be impelmented
' then change the method here in this function.
'
' @return Returns a number identifing this instance.
'
Public Function GetHashCode() As Long
    GetHashCode = ObjPtr(CUnk(Me))
End Function

''
' Returns a string representation of this object instance.
' The default method simply returns the application name
' and class name in which this class resides.
'
' @return Returns a string representation of this instance.
'
Public Function ToString() As String
    ToString = Object.ToString(Me, App)
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   Private Helpers
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function CreateCipher(ByRef RgbKey As Variant, ByRef RgbIV As Variant, ByVal IsEncrypting As Boolean) As CryptoAPITransform
    If IsMissing(RgbKey) <> IsMissing(RgbIV) Then _
        Throw Cor.NewArgumentException("Argument is missing.", IIf(IsMissing(RgbKey), "RgbKey", "RgbIV"))
    
    Dim Key() As Byte
    Key = mBase.CloneRgbKey(RgbKey)
    
    ' We calculate the effective key size because the key itself may have been passed into the CreateEncryptor/CreateDecryptor
    ' functions so the KeySize property may not reflect the true size of the key to be used.
    Set CreateCipher = New CryptoAPITransform
    Call CreateCipher.Init(CALG_RC2, Key, mBase.CloneRgbIV(RgbIV), mBase, IsEncrypting, cArray.GetLength(Key) * 8, mUseSalt)
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   Class Events
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Class_Initialize()
    Set mBase = New SymmetricAlgorithmBase
    With mBase
        Call .AddLegalBlockSize(DEF_MINBLOCKSIZE, DEF_MAXBLOCKSIZE, DEF_BLOCKSKIPSIZE)
        Call .AddLegalKeySize(DEF_MINKEYSIZE, DEF_MAXKEYSIZE, DEF_KEYSKIPSIZE)
        .BlockSize = DEF_BLOCKSIZE
        .KeySize = DEF_KEYSIZE
        .FeedbackSize = DEF_FEEDBACKSIZE
        .Mode = CipherMode.CBC
        .Padding = PaddingMode.PKCS7
    End With
End Sub


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   IObject Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function IObject_Equals(Value As Variant) As Boolean
    IObject_Equals = Equals(Value)
End Function

Private Function IObject_GetHashcode() As Long
    IObject_GetHashcode = GetHashCode
End Function

Private Function IObject_ToString() As String
    IObject_ToString = ToString
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   RC2 Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Property Let RC2_BlockSize(ByVal RHS As Long)
    BlockSize = RHS
End Property

Private Property Get RC2_BlockSize() As Long
    RC2_BlockSize = BlockSize
End Property

Private Sub RC2_Clear()
    Call Clear
End Sub

Private Function RC2_CreateDecryptor(Optional ByRef RgbKey As Variant, Optional ByRef RgbIV As Variant) As ICryptoTransform
    Set RC2_CreateDecryptor = CreateDecryptor(RgbKey, RgbIV)
End Function

Private Function RC2_CreateEncryptor(Optional ByRef RgbKey As Variant, Optional ByRef RgbIV As Variant) As ICryptoTransform
    Set RC2_CreateEncryptor = CreateEncryptor(RgbKey, RgbIV)
End Function

Private Property Let RC2_EffectiveKeySize(ByVal RHS As Long)
    EffectiveKeySize = RHS
End Property

Private Property Get RC2_EffectiveKeySize() As Long
    RC2_EffectiveKeySize = EffectiveKeySize
End Property

Private Function RC2_Equals(Value As Variant) As Boolean
    RC2_Equals = Equals(Value)
End Function

Private Property Let RC2_FeedbackSize(ByVal RHS As Long)
    FeedbackSize = RHS
End Property

Private Property Get RC2_FeedbackSize() As Long
    RC2_FeedbackSize = FeedbackSize
End Property

Private Sub RC2_GenerateIV()
    Call GenerateIV
End Sub

Private Sub RC2_GenerateKey()
    Call GenerateKey
End Sub

Private Function RC2_GetHashCode() As Long
    RC2_GetHashCode = GetHashCode
End Function

Private Property Let RC2_IV(RHS() As Byte)
    Call mBase.SetIV(RHS)
End Property

Private Property Get RC2_IV() As Byte()
    RC2_IV = IV
End Property

Private Property Let RC2_Key(RHS() As Byte)
    Call mBase.SetKey(RHS)
End Property

Private Property Get RC2_Key() As Byte()
    RC2_Key = Key
End Property

Private Property Let RC2_KeySize(ByVal RHS As Long)
    KeySize = RHS
End Property

Private Property Get RC2_KeySize() As Long
    RC2_KeySize = KeySize
End Property

Private Property Get RC2_LegalBlockSizes() As KeySizes()
    RC2_LegalBlockSizes = LegalBlockSizes
End Property

Private Property Get RC2_LegalKeySizes() As KeySizes()
    RC2_LegalKeySizes = LegalKeySizes
End Property

Private Property Let RC2_Mode(ByVal RHS As CipherMode)
    Mode = RHS
End Property

Private Property Get RC2_Mode() As CipherMode
    RC2_Mode = Mode
End Property

Private Property Let RC2_Padding(ByVal RHS As PaddingMode)
    Padding = RHS
End Property

Private Property Get RC2_Padding() As PaddingMode
    RC2_Padding = Padding
End Property

Private Function RC2_ToString() As String
    RC2_ToString = ToString
End Function

Private Function RC2_ValidKeySize(ByVal BitLength As Long) As Boolean
    RC2_ValidKeySize = ValidKeySize(BitLength)
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   SymmetricAlgorithm Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Property Let SymmetricAlgorithm_BlockSize(ByVal RHS As Long)
    BlockSize = RHS
End Property

Private Property Get SymmetricAlgorithm_BlockSize() As Long
    SymmetricAlgorithm_BlockSize = BlockSize
End Property

Private Sub SymmetricAlgorithm_Clear()
    Call Clear
End Sub

Private Function SymmetricAlgorithm_CreateDecryptor(Optional ByRef RgbKey As Variant, Optional ByRef RgbIV As Variant) As ICryptoTransform
    Set SymmetricAlgorithm_CreateDecryptor = CreateDecryptor(RgbKey, RgbIV)
End Function

Private Function SymmetricAlgorithm_CreateEncryptor(Optional ByRef RgbKey As Variant, Optional ByRef RgbIV As Variant) As ICryptoTransform
    Set SymmetricAlgorithm_CreateEncryptor = CreateEncryptor(RgbKey, RgbIV)
End Function

Private Function SymmetricAlgorithm_Equals(Value As Variant) As Boolean
    SymmetricAlgorithm_Equals = Equals(Value)
End Function

Private Property Let SymmetricAlgorithm_FeedbackSize(ByVal RHS As Long)
    FeedbackSize = RHS
End Property

Private Property Get SymmetricAlgorithm_FeedbackSize() As Long
    SymmetricAlgorithm_FeedbackSize = FeedbackSize
End Property

Private Sub SymmetricAlgorithm_GenerateIV()
    Call GenerateIV
End Sub

Private Sub SymmetricAlgorithm_GenerateKey()
    Call GenerateKey
End Sub

Private Function SymmetricAlgorithm_GetHashCode() As Long
    SymmetricAlgorithm_GetHashCode = GetHashCode
End Function

Private Property Let SymmetricAlgorithm_IV(RHS() As Byte)
    Call mBase.SetIV(RHS)
End Property

Private Property Get SymmetricAlgorithm_IV() As Byte()
    SymmetricAlgorithm_IV = IV
End Property

Private Property Let SymmetricAlgorithm_Key(RHS() As Byte)
    Call mBase.SetKey(RHS)
End Property

Private Property Get SymmetricAlgorithm_Key() As Byte()
    SymmetricAlgorithm_Key = Key
End Property

Private Property Let SymmetricAlgorithm_KeySize(ByVal RHS As Long)
    KeySize = RHS
End Property

Private Property Get SymmetricAlgorithm_KeySize() As Long
    SymmetricAlgorithm_KeySize = KeySize
End Property

Private Property Get SymmetricAlgorithm_LegalBlockSizes() As KeySizes()
    SymmetricAlgorithm_LegalBlockSizes = LegalBlockSizes
End Property

Private Property Get SymmetricAlgorithm_LegalKeySizes() As KeySizes()
    SymmetricAlgorithm_LegalKeySizes = LegalKeySizes
End Property

Private Property Let SymmetricAlgorithm_Mode(ByVal RHS As CipherMode)
    Mode = RHS
End Property

Private Property Get SymmetricAlgorithm_Mode() As CipherMode
    SymmetricAlgorithm_Mode = Mode
End Property

Private Property Let SymmetricAlgorithm_Padding(ByVal RHS As PaddingMode)
    Padding = RHS
End Property

Private Property Get SymmetricAlgorithm_Padding() As PaddingMode
    SymmetricAlgorithm_Padding = Padding
End Property

Private Function SymmetricAlgorithm_ToString() As String
    SymmetricAlgorithm_ToString = ToString
End Function

Private Function SymmetricAlgorithm_ValidKeySize(ByVal BitLength As Long) As Boolean
    SymmetricAlgorithm_ValidKeySize = ValidKeySize(BitLength)
End Function

⌨️ 快捷键说明

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