
Cómo hacer hash y cifrado en VBA
Vivimos en un mundo en el que la seguridad de los datos y las comunicaciones deben almacenarse y transmitirse de forma segura. Por ello, resulta cada vez más importante utilizar técnicas de hash y/o cifrado/descifrado.
Ya he discutido esto un poco anteriormente y proporcioné un par de enfoques posibles:
Hoy quería aportar otra posible solución a este asunto.
Descripción general
El enfoque que propuse hoy es emplear el uso de una biblioteca de criptografía JavaScript que proporciona una amplia variedad de hashers y cifrados que podemos explotar.
Algunos de los hashers incluyen:
- MD5
- SHA-1
- SHA-2
- 224
- 256
- 384
- 512
- SHA-3
- 224
- 256
- 384
- 512
- RIPEMD-160
Algunos de los cifrados incluyen:
- AES
- DESDE
- Triple DES
- Conejo
- RC4
A continuación, voy a proporcionar el código básico para el hash y el cifrado, pero sepa que puede hacer cosas mucho más avanzadas si lo desea: claves personalizadas, modos de bloque y relleno, cifrado progresivo, claves, salazón, … Incluso puede especificar diferentes tipos de salida: base64, Hex, … Si esto le interesa, entonces es muy sencillo de hacer en general y le insto a que revise la documentación para obtener más información para estar en condiciones de alterar los ejemplos de código que se presentan a continuación.
El código
Aquí proporciono varias versiones con fines ilustrativos, para aquellos que quieran entender cómo puede evolucionar el código, pero siempre se recomienda utilizar la última versión disponible, ya que normalmente será la que se haya optimizado y se hayan solucionado los errores.
' --------------------------------------------------------------------------------
' Date : 2024-03-14
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Series of cryptography function utilizing the crypto-js JS library
' to perform hashing and/or encryption/decryption.
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' --------------------------------------------------------------------------------
Option Compare Database
Option Explicit
'**********************WARNING**********************
' SHA-3 originally was the same as Keccak, but at the last minute, SHA-3 got an extra
' bit of padding to distinguish it from Keccak. This library implements Keccak, and
' Ethereum was developed to use the early/original Keccak variant of SHA-3.
'***************************************************
Enum JSLibraryType
LocalFile = 1
CDNFile = 2
End Enum
Enum HashAlgorithm
MD5 = 1
SHA1 = 2
SHA2224 = 3
SHA2256 = 4
SHA2384 = 5
SHA2512 = 6
SHA3224 = 7
SHA3256 = 8
SHA3384 = 9
SHA3512 = 10
RIPEMD160 = 11
End Enum
Enum CipherAlgorithm
AES = 1
DES = 2
TripleDES = 3
Rabbit = 4
RC4 = 5
End Enum
Enum CipherDirection
Encrypt = 1
Decrypt = 2
End Enum
Enum OutputFormat
Latin1 = 1
UTF8 = 2
Hex = 3
UTF16 = 4
Base64 = 5
End Enum
#Const HF_EarlyBind = False 'True => Early Binding / False => Late Binding
'---------------------------------------------------------------------------------------
' Procedure : Crypto_Hash
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Hash's strings using a variety of algorithms
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' Req'd Refs: Late Binding -> none required
' Early Binding -> Microsoft HTML Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTextToHash : String to hash
' lHashAlgorithm : Algorithm to use for the hashing
' JSFileLocation : Crypto-js file location (CDN or Local)
' lOutputFormat : The desired returned output format
'
' Usage:
' ~~~~~~
' ? Crypto_Hash("This is a test!", MD5)
' Returns -> 702edca0b2181c15d457eacac39de39b
'
' ? Crypto_Hash("This is a test!", RIPEMD160)
' Returns -> ed5c1d5ffeabb0a285f68ddef18363922dc4100d
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2024-03-20 Initial Release
' 2 2024-03-21 Added OutputFormat argument
' Fixed SHA384 bug
'---------------------------------------------------------------------------------------
Function Crypto_Hash(ByVal sTextToHash As String, _
ByVal lHashAlgorithm As HashAlgorithm, _
Optional JSFileLocation As JSLibraryType = LocalFile, _
Optional lOutputFormat As OutputFormat = Hex)
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim sHashAlgorithm As String
Dim sOutputFormat As String
Dim bSHA3 As Boolean
Dim result As String
oHTMLFile.body.innerHTML = "" 'Clear the default content
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
DoEvents 'Important!!!
Select Case lHashAlgorithm
Case MD5
sHashAlgorithm = "MD5"
Case SHA1
sHashAlgorithm = "SHA1"
Case SHA2224
sHashAlgorithm = "SHA224"
Case SHA2256
sHashAlgorithm = "SHA256"
Case SHA2384
sHashAlgorithm = "SHA384"
Case SHA2512
sHashAlgorithm = "SHA512"
Case SHA3224
sHashAlgorithm = "SHA3224"
bSHA3 = True
Case SHA3256
sHashAlgorithm = "SHA3256"
bSHA3 = True
Case SHA3384
sHashAlgorithm = "SHA3384"
bSHA3 = True
Case SHA3512
sHashAlgorithm = "SHA3512"
bSHA3 = True
Case RIPEMD160
sHashAlgorithm = "RIPEMD160"
End Select
Select Case lOutputFormat
Case Latin1
sOutputFormat = "Latin1"
Case UTF8
sOutputFormat = "Utf8"
Case Hex
sOutputFormat = "Hex"
Case UTF16
sOutputFormat = "Utf16"
Case Base64
sOutputFormat = "Base64"
End Select
If Not bSHA3 Then
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sHashAlgorithm & "('" & sTextToHash & "').toString(CryptoJS.enc." & _
sOutputFormat & ");")
Else
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA3('" & _
sTextToHash & "', {outputLength:" & Right(sHashAlgorithm, 3) & _
"}).toString(CryptoJS.enc." & sOutputFormat & ");")
End If
DoEvents
Crypto_Hash = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
'---------------------------------------------------------------------------------------
' Procedure : Crypto_Cipher
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Cipher's strings (encrypt/decrypt) using a variety of algorithms
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' Req'd Refs: Late Binding -> none required
' Early Binding -> Microsoft HTML Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTextToHash : String to cipher
' sPassPhrase : Pass Phrase to use for the ciphering
' lCipherAlgorithm : Algorithm to use for the ciphering
' lDirection : Ciphering direction (encrypt or decrypt)
' JSFileLocation : Crypto-js file location (CDN or Local)
' lOutputFormat : The desired returned output format
'
' Usage:
' ~~~~~~
' ? Crypto_Cipher("This is a test!", "123hkeio488", TripleDES, Encrypt)
' Returns -> U2FsdGVkX1/CQMQKDXqolBNHuE8ZifaVGPg5/O4EOJ8=
'
' ? Crypto_Cipher("U2FsdGVkX1/CQMQKDXqolBNHuE8ZifaVGPg5/O4EOJ8=", "123hkeio488", TripleDES, Decrypt)
' Returns -> This is a test!
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2024-03-20 Initial Release
' 2 2024-03-21 Fix an issue with decryption .toString(CryptoJS.enc.Utf8)
' Added OutputFormat argument
'---------------------------------------------------------------------------------------
Function Crypto_Cipher(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
ByVal lCipherAlgorithm As CipherAlgorithm, _
ByVal lDirection As CipherDirection, _
Optional JSFileLocation As JSLibraryType = LocalFile, _
Optional lOutputFormat As OutputFormat)
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim sCipherAlgorithm As String
Dim sCipherDirection As String
Dim sOutputFormat As String
Dim result As String
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
DoEvents 'Important!!!
Select Case lCipherAlgorithm
Case AES
sCipherAlgorithm = "AES"
Case DES
sCipherAlgorithm = "DES"
Case TripleDES
sCipherAlgorithm = "TripleDES"
Case Rabbit
sCipherAlgorithm = "Rabbit"
Case RC4
sCipherAlgorithm = "RC4"
End Select
Select Case lDirection
Case Encrypt
sCipherDirection = "encrypt"
If lOutputFormat = 0 Then lOutputFormat = Hex
Case Decrypt
sCipherDirection = "decrypt"
If lOutputFormat = 0 Then lOutputFormat = UTF8
End Select
Select Case lOutputFormat
Case Latin1
sOutputFormat = "Latin1"
Case UTF8
sOutputFormat = "Utf8"
Case Hex
sOutputFormat = "Hex"
Case UTF16
sOutputFormat = "Utf16"
Case Base64
sOutputFormat = "Base64"
End Select
If lDirection = Encrypt Then
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sCipherAlgorithm & "." & sCipherDirection & "('" & _
sTextToHash & "', '" & sPassPhrase & "').toString();")
Else
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sCipherAlgorithm & "." & sCipherDirection & "('" & _
sTextToHash & "', '" & sPassPhrase & "').toString(CryptoJS.enc." & sOutputFormat & ");")
End If
DoEvents
Crypto_Cipher = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
' --------------------------------------------------------------------------------
' Date : 2024-03-14
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Series of cryptography function utilizing the crypto-js JS library
' to perform hashing and/or encryption/decryption, but using
' self-healing object variables (SHOV)
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' --------------------------------------------------------------------------------
Option Compare Database
Option Explicit
'**********************WARNING**********************
' SHA-3 originally was the same as Keccak, but at the last minute, SHA-3 got an extra
' bit of padding to distinguish it from Keccak. This library implements Keccak, and
' Ethereum was developed to use the early/original Keccak variant of SHA-3.
'***************************************************
Enum JSLibraryType
LocalFile = 1
CDNFile = 2
End Enum
Enum HashAlgorithm
MD5 = 1
SHA1 = 2
SHA2224 = 3
SHA2256 = 4
SHA2384 = 5
SHA2512 = 6
SHA3224 = 7
SHA3256 = 8
SHA3384 = 9
SHA3512 = 10
RIPEMD160 = 11
End Enum
Enum CipherAlgorithm
AES = 1
DES = 2
TripleDES = 3
Rabbit = 4
RC4 = 5
End Enum
Enum CipherDirection
Encrypt = 1
Decrypt = 2
End Enum
Enum OutputFormat
Latin1 = 1
UTF8 = 2
Hex = 3
UTF16 = 4
Base64 = 5
End Enum
' Req'd Refs: Late Binding -> None required
' Early Binding -> Microsoft HTML Object Library
#Const HTMLFile_EarlyBind = False
#If HTMLFile_EarlyBind = True Then
Private pHTMLFile As MSHTML.HTMLDocument
#Else
Private pHTMLFile As Object
#End If
#If HTMLFile_EarlyBind = True Then
Public Function oHTMLFile(Optional bForceRefresh As Boolean = False) As MSHTML.HTMLDocument
#Else
Public Function oHTMLFile(Optional bForceRefresh As Boolean = False) As Object
#End If
If pHTMLFile Is Nothing Or bForceRefresh Then
Debug.Print "*** Setting oHTMLFile ***"
#If HTMLFile_EarlyBind = True Then
Set pHTMLFile = New MSHTML.HTMLDocument
#Else
Set pHTMLFile = CreateObject("HTMLFile")
#End If
End If
Set oHTMLFile = pHTMLFile
End Function
Public Sub oHTMLFile_Clear()
'Be sure to always run this when closing your Form/DB to avoid
' hidden instances from running in the background!
Set pHTMLFile = Nothing
End Sub
'---------------------------------------------------------------------------------------
' Procedure : Crypto_Hash_SHOV
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Hash's strings using a variety of algorithms
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' Req'd Refs: Late Binding -> none required
' Early Binding -> Microsoft HTML Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTextToHash : String to hash
' lHashAlgorithm : Algorithm to use for the hashing
' JSFileLocation : Crypto-js file location (CDN or Local)
' lOutputFormat : The desired returned output format
'
' Usage:
' ~~~~~~
' ? Crypto_Hash_SHOV("This is a test!", MD5)
' Returns -> 702edca0b2181c15d457eacac39de39b
'
' ? Crypto_Hash_SHOV("This is a test!", RIPEMD160)
' Returns -> ed5c1d5ffeabb0a285f68ddef18363922dc4100d
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2024-03-20 Initial Release
' 2 2024-03-21 Added OutputFormat argument
' Fixed SHA384 bug
'---------------------------------------------------------------------------------------
Function Crypto_Hash_SHOV(ByVal sTextToHash As String, _
ByVal lHashAlgorithm As HashAlgorithm, _
Optional JSFileLocation As JSLibraryType = LocalFile, _
Optional lOutputFormat As OutputFormat = Hex)
#If HTMLFile_EarlyBind = True Then
Dim oElem As MSHTML.HTMLGenericElement
#Else
Dim oElem As Object
Const acComplete = 4
#End If
Dim sFilePath As String
Dim sHashAlgorithm As String
Dim sOutputFormat As String
Dim bSHA3 As Boolean
Dim result As String
oHTMLFile.body.innerHTML = "" 'Clear the default content
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
DoEvents 'Important!!!
Select Case lHashAlgorithm
Case MD5
sHashAlgorithm = "MD5"
Case SHA1
sHashAlgorithm = "SHA1"
Case SHA2224
sHashAlgorithm = "SHA224"
Case SHA2256
sHashAlgorithm = "SHA256"
Case SHA2384
sHashAlgorithm = "SHA384"
Case SHA2512
sHashAlgorithm = "SHA512"
Case SHA3224
sHashAlgorithm = "SHA3224"
bSHA3 = True
Case SHA3256
sHashAlgorithm = "SHA3256"
bSHA3 = True
Case SHA3384
sHashAlgorithm = "SHA3384"
bSHA3 = True
Case SHA3512
sHashAlgorithm = "SHA3512"
bSHA3 = True
Case RIPEMD160
sHashAlgorithm = "RIPEMD160"
End Select
Select Case lOutputFormat
Case Latin1
sOutputFormat = "Latin1"
Case UTF8
sOutputFormat = "Utf8"
Case Hex
sOutputFormat = "Hex"
Case UTF16
sOutputFormat = "Utf16"
Case Base64
sOutputFormat = "Base64"
End Select
If Not bSHA3 = True Then
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sHashAlgorithm & "('" & sTextToHash & "').toString(CryptoJS.enc." & _
sOutputFormat & ");")
Else
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA3('" & _
sTextToHash & "', {outputLength:" & Right(sHashAlgorithm, 3) & "}).toString(CryptoJS.enc." & _
sOutputFormat & ");")
End If
DoEvents
Crypto_Hash_SHOV = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
End Function
'---------------------------------------------------------------------------------------
' Procedure : Crypto_Cipher_SHOV
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Cipher's strings (encrypt/decrypt) using a variety of algorithms
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' Req'd Refs: Late Binding -> none required
' Early Binding -> Microsoft HTML Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTextToHash : String to cipher
' sPassPhrase : Pass Phrase to use for the ciphering
' lCipherAlgorithm : Algorithm to use for the ciphering
' lDirection : Ciphering direction (encrypt or decrypt)
' JSFileLocation : Crypto-js file location (CDN or Local)
' lOutputFormat : The desired returned output format
'
' Usage:
' ~~~~~~
' ? Crypto_Cipher_SHOV("This is a test!", "123hkeio488", TripleDES, Encrypt)
' Returns -> U2FsdGVkX1/XHdM52T/+0KOsoGqIox9JbWTDo2itepA=
'
' ? Crypto_Cipher_SHOV("U2FsdGVkX1/XHdM52T/+0KOsoGqIox9JbWTDo2itepA=", _
' "123hkeio488", TripleDES, Decrypt, LocalFile, UTF8)
' Returns -> This is a test!
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2024-03-20 Initial Release
' 2 2024-03-21 Fix an issue with decryption .toString(CryptoJS.enc.Utf8)
' Added OutputFormat argument
'---------------------------------------------------------------------------------------
Function Crypto_Cipher_SHOV(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
ByVal lCipherAlgorithm As CipherAlgorithm, _
ByVal lDirection As CipherDirection, _
Optional JSFileLocation As JSLibraryType = LocalFile, _
Optional lOutputFormat As OutputFormat)
#If HTMLFile_EarlyBind = True Then
Dim oElem As MSHTML.HTMLGenericElement
#Else
Dim oElem As Object
Const acComplete = 4
#End If
Dim sFilePath As String
Dim sCipherAlgorithm As String
Dim sCipherDirection As String
Dim sOutputFormat As String
Dim result As String
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
DoEvents 'Important!!!
Select Case lCipherAlgorithm
Case AES
sCipherAlgorithm = "AES"
Case DES
sCipherAlgorithm = "DES"
Case TripleDES
sCipherAlgorithm = "TripleDES"
Case Rabbit
sCipherAlgorithm = "Rabbit"
Case RC4
sCipherAlgorithm = "RC4"
End Select
Select Case lDirection
Case Encrypt
sCipherDirection = "encrypt"
If lOutputFormat = 0 Then lOutputFormat = Hex
Case Decrypt
sCipherDirection = "decrypt"
If lOutputFormat = 0 Then lOutputFormat = UTF8
End Select
Select Case lOutputFormat
Case Latin1
sOutputFormat = "Latin1"
Case UTF8
sOutputFormat = "Utf8"
Case Hex
sOutputFormat = "Hex"
Case UTF16
sOutputFormat = "Utf16"
Case Base64
sOutputFormat = "Base64"
End Select
If lDirection = Encrypt Then
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sCipherAlgorithm & "." & sCipherDirection & "('" & _
sTextToHash & "', '" & sPassPhrase & "').toString();")
Else
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sCipherAlgorithm & "." & sCipherDirection & "('" & _
sTextToHash & "', '" & sPassPhrase & "').toString(CryptoJS.enc." & sOutputFormat & ");")
End If
DoEvents
Crypto_Cipher_SHOV = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
End Function
' --------------------------------------------------------------------------------
' Date : 2024-03-14
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Series of cryptography function utilizing the crypto-js JS library
' to perform hashing and/or encryption/decryption.
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' --------------------------------------------------------------------------------
Option Compare Database
Option Explicit
Enum JSLibraryType
LocalFile = 1
CDNFile = 2
End Enum
Enum HashAlgorithm
MD5 = 1
SHA1 = 2
SHA2224 = 3 'SHA2 224
SHA2256 = 4 'SHA2 256
SHA2384 = 5 'SHA2 384
SHA2512 = 6 'SHA2 512
SHA3224 = 7 'SHA3 224
SHA3256 = 8 'SHA3 256
SHA3384 = 9 'SHA3 384
SHA3512 = 10 'SHA3 512
RIPEMD160 = 11
End Enum
Enum CipherAlgorithm
AES = 1
DES = 2
TripleDES = 3
Rabbit = 4
RC4 = 5
End Enum
Enum CipherDirection
Encrypt = 1
Decrypt = 2
End Enum
#Const HF_EarlyBind = False 'True => Early Binding / False => Late Binding
'---------------------------------------------------------------------------------------
' Procedure : Crypto_Hash
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Hash's strings using a variety of algorithms
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' Req'd Refs: Late Binding -> none required
' Early Binding -> Microsoft HTML Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTextToHash : String to hash
' lHashAlgorithm : Algorithm to use for the hashing
' JSFileLocation : Crypto-js file location (CDN or Local)
'
' Usage:
' ~~~~~~
' ? Crypto_Hash("This is a test!", MD5)
' Returns -> 702edca0b2181c15d457eacac39de39b
'
' ? Crypto_Hash("This is a test!", RIPEMD160)
' Returns -> ed5c1d5ffeabb0a285f68ddef18363922dc4100d
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2024-03-20 Initial Release
'---------------------------------------------------------------------------------------
Function Crypto_Hash(ByVal sTextToHash As String, _
ByVal lHashAlgorithm As HashAlgorithm, _
Optional JSFileLocation As JSLibraryType = LocalFile)
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim sHashAlgorithm As String
Dim result As String
oHTMLFile.body.innerHTML = "" 'Clear the default content
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
DoEvents 'Important!!!
Select Case lHashAlgorithm
Case MD5
sHashAlgorithm = "MD5"
Case SHA1
sHashAlgorithm = "SHA1"
Case SHA2224
sHashAlgorithm = "SHA224"
Case SHA2256
sHashAlgorithm = "SHA256"
Case SHA2384
sHashAlgorithm = "SHA384"
Case SHA2512
sHashAlgorithm = "SHA512"
Case SHA3224
sHashAlgorithm = "SHA3224"
Case SHA3256
sHashAlgorithm = "SHA3256"
Case SHA3384
sHashAlgorithm = "SHA3384"
Case SHA3512
sHashAlgorithm = "SHA3512"
Case RIPEMD160
sHashAlgorithm = "RIPEMD160"
End Select
If Not sHashAlgorithm Like "SHA3*" Then
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sHashAlgorithm & "('" & sTextToHash & "');")
Else
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA3('" & _
sTextToHash & "', {outputLength:" & Right(sHashAlgorithm, 3) & "});")
End If
DoEvents
Crypto_Hash = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
'---------------------------------------------------------------------------------------
' Procedure : Crypto_Cipher
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Cipher's strings (encrypt/decrypt) using a variety of algorithms
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' Req'd Refs: Late Binding -> none required
' Early Binding -> Microsoft HTML Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTextToHash : String to cipher
' sPassPhrase : Pass Phrase to use for the ciphering
' lCipherAlgorithm : Algorithm to use for the ciphering
' lDirection : Ciphering direction (encrypt or decrypt)
' JSFileLocation : Crypto-js file location (CDN or Local)
'
' Usage:
' ~~~~~~
' ? Crypto_Cipher("This is a test!", "123hkeio488", TripleDES, Encrypt)
' Returns -> U2FsdGVkX1+/3V0UqBe//E9twcd4ZQ24tKRmJt/cMYk=
'
' ? Crypto_Cipher("U2FsdGVkX1+/3V0UqBe//E9twcd4ZQ24tKRmJt/cMYk=", _
' "123hkeio488", TripleDES, Decrypt)
' Returns -> 546869732069732061207465737421
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2024-03-20 Initial Release
' 2 2024-03-21 Fix an issue with decryption .toString(CryptoJS.enc.Utf8)
'---------------------------------------------------------------------------------------
Function Crypto_Cipher(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
ByVal lCipherAlgorithm As CipherAlgorithm, _
ByVal lDirection As CipherDirection, _
Optional JSFileLocation As JSLibraryType = LocalFile)
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim sCipherAlgorithm As String
Dim sCipherDirection As String
Dim result As String
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
DoEvents 'Important!!!
Select Case lCipherAlgorithm
Case AES
sCipherAlgorithm = "AES"
Case DES
sCipherAlgorithm = "DES"
Case TripleDES
sCipherAlgorithm = "TripleDES"
Case Rabbit
sCipherAlgorithm = "Rabbit"
Case RC4
sCipherAlgorithm = "RC4"
End Select
Select Case lDirection
Case encrypt
sCipherDirection = "encrypt"
Case Decrypt
sCipherDirection = "decrypt"
End Select
If lDirection = encrypt Then
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sCipherAlgorithm & "." & sCipherDirection & "('" & _
sTextToHash & "', '" & sPassPhrase & "').toString();")
Else
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sCipherAlgorithm & "." & sCipherDirection & "('" & _
sTextToHash & "', '" & sPassPhrase & "').toString(CryptoJS.enc.Utf8);")
End If
DoEvents
Crypto_Cipher = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
' --------------------------------------------------------------------------------
' Date : 2024-03-14
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Series of cryptography function utilizing the crypto-js JS library
' to perform hashing and/or encryption/decryption, but using
' self-healing object variables (SHOV)
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' --------------------------------------------------------------------------------
Option Compare Database
Option Explicit
' Req'd Refs: Late Binding -> None required
' Early Binding -> Microsoft HTML Object Library
Enum JSLibraryType
LocalFile = 1
CDNFile = 2
End Enum
Enum HashAlgorithm
MD5 = 1
SHA1 = 2
SHA2224 = 3 'SHA2 224
SHA2256 = 4 'SHA2 256
SHA2384 = 5 'SHA2 384
SHA2512 = 6 'SHA2 512
SHA3224 = 7 'SHA3 224
SHA3256 = 8 'SHA3 256
SHA3384 = 9 'SHA3 384
SHA3512 = 10 'SHA3 512
RIPEMD160 = 11
End Enum
Enum CipherAlgorithm
AES = 1
DES = 2
TripleDES = 3
Rabbit = 4
RC4 = 5
End Enum
Enum CipherDirection
Encrypt = 1
Decrypt = 2
End Enum
#Const HTMLFile_EarlyBind = False
#If HTMLFile_EarlyBind = True Then
Private pHTMLFile As MSHTML.HTMLDocument
#Else
Private pHTMLFile As Object
#End If
#If HTMLFile_EarlyBind = True Then
Public Function oHTMLFile(Optional bForceRefresh As Boolean = False) As MSHTML.HTMLDocument
#Else
Public Function oHTMLFile(Optional bForceRefresh As Boolean = False) As Object
#End If
If pHTMLFile Is Nothing Or bForceRefresh Then
Debug.Print "*** Setting oHTMLFile ***"
#If HTMLFile_EarlyBind = True Then
Set pHTMLFile = New MSHTML.HTMLDocument
#Else
Set pHTMLFile = CreateObject("HTMLFile")
#End If
End If
Set oHTMLFile = pHTMLFile
End Function
Public Sub oHTMLFile_Clear()
'Be sure to always run this when closing your Form/DB to avoid
' hidden instances from running in the background!
Set pHTMLFile = Nothing
End Sub
'---------------------------------------------------------------------------------------
' Procedure : Crypto_Hash_SHOV
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Hash's strings using a variety of algorithms
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' Req'd Refs: Late Binding -> none required
' Early Binding -> Microsoft HTML Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTextToHash : String to hash
' lHashAlgorithm : Algorithm to use for the hashing
' JSFileLocation : Crypto-js file location (CDN or Local)
'
' Usage:
' ~~~~~~
' ? Crypto_Hash_SHOV("This is a test!", MD5)
' Returns -> 702edca0b2181c15d457eacac39de39b
'
' ? Crypto_Hash_SHOV("This is a test!", RIPEMD160)
' Returns -> ed5c1d5ffeabb0a285f68ddef18363922dc4100d
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2024-03-20 Initial Release
'---------------------------------------------------------------------------------------
Function Crypto_Hash_SHOV(ByVal sTextToHash As String, _
ByVal lHashAlgorithm As HashAlgorithm, _
Optional JSFileLocation As JSLibraryType = LocalFile)
#If HTMLFile_EarlyBind = True Then
Dim oElem As MSHTML.HTMLGenericElement
#Else
Dim oElem As Object
Const acComplete = 4
#End If
Dim sFilePath As String
Dim sHashAlgorithm As String
Dim result As String
oHTMLFile.body.innerHTML = "" 'Clear the default content
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
DoEvents 'Important!!!
Select Case lHashAlgorithm
Case MD5
sHashAlgorithm = "MD5"
Case SHA1
sHashAlgorithm = "SHA1"
Case SHA2224
sHashAlgorithm = "SHA224"
Case SHA2256
sHashAlgorithm = "SHA256"
Case SHA2384
sHashAlgorithm = "SHA384"
Case SHA2512
sHashAlgorithm = "SHA512"
Case SHA3224
sHashAlgorithm = "SHA3224"
Case SHA3256
sHashAlgorithm = "SHA3256"
Case SHA3384
sHashAlgorithm = "SHA3384"
Case SHA3512
sHashAlgorithm = "SHA3512"
Case RIPEMD160
sHashAlgorithm = "RIPEMD160"
End Select
If Not sHashAlgorithm Like "SHA3*" Then
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sHashAlgorithm & "('" & sTextToHash & "');")
Else
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA3('" & _
sTextToHash & "', {outputLength:" & Right(sHashAlgorithm, 3) & "});")
End If
DoEvents
Crypto_Hash_SHOV = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
End Function
'---------------------------------------------------------------------------------------
' Procedure : Crypto_Cipher_SHOV
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Cipher's strings (encrypt/decrypt) using a variety of algorithms
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' Req'd Refs: Late Binding -> none required
' Early Binding -> Microsoft HTML Object Library
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sTextToHash : String to cipher
' sPassPhrase : Pass Phrase to use for the ciphering
' lCipherAlgorithm : Algorithm to use for the ciphering
' lDirection : Ciphering direction (encrypt or decrypt)
' JSFileLocation : Crypto-js file location (CDN or Local)
'
' Usage:
' ~~~~~~
' ? Crypto_Cipher_SHOV("This is a test!", "123hkeio488", TripleDES, Encrypt)
' Returns -> U2FsdGVkX1+/3V0UqBe//E9twcd4ZQ24tKRmJt/cMYk=
'
' ? Crypto_Cipher_SHOV("U2FsdGVkX1+/3V0UqBe//E9twcd4ZQ24tKRmJt/cMYk=", _
' "123hkeio488", TripleDES, Decrypt)
' Returns -> 546869732069732061207465737421
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2024-03-20 Initial Release
' 2 2024-03-21 Fix an issue with decryption .toString(CryptoJS.enc.Utf8)
'---------------------------------------------------------------------------------------
Function Crypto_Cipher_SHOV(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
ByVal lCipherAlgorithm As CipherAlgorithm, _
ByVal lDirection As CipherDirection, _
Optional JSFileLocation As JSLibraryType = LocalFile)
#If HTMLFile_EarlyBind = True Then
Dim oElem As MSHTML.HTMLGenericElement
#Else
Dim oElem As Object
Const acComplete = 4
#End If
Dim sFilePath As String
Dim sCipherAlgorithm As String
Dim sCipherDirection As String
Dim result As String
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
DoEvents 'Important!!!
Select Case lCipherAlgorithm
Case AES
sCipherAlgorithm = "AES"
Case DES
sCipherAlgorithm = "DES"
Case TripleDES
sCipherAlgorithm = "TripleDES"
Case Rabbit
sCipherAlgorithm = "Rabbit"
Case RC4
sCipherAlgorithm = "RC4"
End Select
Select Case lDirection
Case encrypt
sCipherDirection = "encrypt"
Case Decrypt
sCipherDirection = "decrypt"
End Select
If lDirection = encrypt Then
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sCipherAlgorithm & "." & sCipherDirection & "('" & _
sTextToHash & "', '" & sPassPhrase & "').toString();")
Else
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS." & _
sCipherAlgorithm & "." & sCipherDirection & "('" & _
sTextToHash & "', '" & sPassPhrase & "').toString(CryptoJS.enc.Utf8);")
End If
DoEvents
Crypto_Cipher_SHOV = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
End Function
' --------------------------------------------------------------------------------
' Date : 2024-03-14
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website :
' Purpose : Series of cryptography function utilizing the crypto-js JS library
' to perform hashing and/or encryption/decryption.
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) -
' --------------------------------------------------------------------------------
Option Compare Database
Option Explicit
Enum JSLibraryType
LocalFile = 1
CDNFile = 2
End Enum
Enum SHA3OutputLength
Bits224 = 224
Bits256 = 256
Bits384 = 384
Bits512 = 512
End Enum
'? MD5("This is a test.", CDNFile)
Function MD5(ByVal sTextToHash As String, Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.MD5('" & sTextToHash & "');")
DoEvents
MD5 = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
'? SHA1("This is a test.", CDNFile)
Function SHA1(ByVal sTextToHash As String, Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA1('" & sTextToHash & "');")
DoEvents
SHA1 = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function SHA256(ByVal sTextToHash As String, Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA256('" & sTextToHash & "');")
DoEvents
SHA256 = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function SHA224(ByVal sTextToHash As String, Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA224('" & sTextToHash & "');")
DoEvents
SHA224 = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function SHA384(ByVal sTextToHash As String, Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA384('" & sTextToHash & "');")
DoEvents
SHA384 = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function SHA512(ByVal sTextToHash As String, Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA512('" & sTextToHash & "');")
DoEvents
SHA512 = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function SHA3(ByVal sTextToHash As String, Optional JSFileLocation As JSLibraryType = LocalFile, Optional lSHA3Length As SHA3OutputLength = Bits512) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.SHA3('" & sTextToHash & "', {outputLength:" & lSHA3Length & "});")
DoEvents
SHA3 = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function RIPEMD160(ByVal sTextToHash As String, Optional JSFileLocation As JSLibraryType = LocalFile, Optional lSHA3Length As SHA3OutputLength = Bits512) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.RIPEMD160('" & sTextToHash & "');")
DoEvents
RIPEMD160 = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function AESEncrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.AES.encrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
AESEncrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function AESDecrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.AES.decrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
AESDecrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function DESEncrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.DES.encrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
DESEncrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function DESDecrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.DES.decrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
DESDecrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function TripleDESEncrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.TripleDES.encrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
TripleDESEncrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function TripleDESDecrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.TripleDES.decrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
TripleDESDecrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function RabbitEncrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.Rabbit.encrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
RabbitEncrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function RabbitDecrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.Rabbit.decrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
RabbitDecrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function RC4Encrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.RC4.encrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
RC4Encrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
Function RC4Decrypt(ByVal sTextToHash As String, _
ByVal sPassPhrase As String, _
Optional JSFileLocation As JSLibraryType = LocalFile) As String
'Declare a string variable to store the JavaScript function.
'#Const HF_EarlyBind = True 'True => Early Binding / False => Late Binding
'Typically a Global Module Level Variable
#If HF_EarlyBind = True Then
Dim oHTMLFile As MSHTML.HTMLDocument
Dim oElem As MSHTML.HTMLGenericElement
Set oHTMLFile = New MSHTML.HTMLDocument
#Else
Dim oHTMLFile As Object
Dim oElem As Object
Const acComplete = 4
Set oHTMLFile = CreateObject("HTMLFile")
#End If
Dim sFilePath As String
Dim result As String
Set oHTMLFile = CreateObject("HTMLFile")
oHTMLFile.body.innerHTML = "" 'Clear the default line
Set oElem = oHTMLFile.createElement("p")
Call oElem.setAttribute("id", "result")
Call oHTMLFile.body.appendChild(oElem)
If JSFileLocation = 1 Then
'Use local copy of crypto-js / Faster obviously
'*******************************************************************************************************
sFilePath = Application.CodeProject.Path & "\js\crypto-js\4.0.0\crypto-js.min.js"
Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
'Call oElem.setAttribute("src", "file:///C:\Demos\Crypto\js\crypto-js\4.0.0\crypto-js.min.js")
Call oElem.setAttribute("src", "file:///" & sFilePath)
Call oHTMLFile.head.appendChild(oElem)
Else
'Use CDN copy of crypto-js
'*******************************************************************************************************
' Set oElem = oHTMLFile.createElement("script")
'Call oElem.setAttribute("type", "text/javascript")
Call oElem.setAttribute("src", "
Call oElem.setAttribute("integrity", "sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==")
Call oElem.setAttribute("crossorigin", "anonymous")
Call oElem.setAttribute("referrerpolicy", "no-referrer")
Call oHTMLFile.head.appendChild(oElem)
'The following line is sometimes required to allow time to load
'Sleep 150 '************** Errs without this!!!!!!!!!!!!!!!!!!!!! needs time to load, adjust delay by testing
End If
'While oHTMLFile.parentWindow.status acComplete: DoEvents: Wend 'Does NOT work!
DoEvents 'Important!!!
Call oHTMLFile.parentWindow.execScript("document.getElementById('result').innerText = CryptoJS.RC4.decrypt('" & sTextToHash & "', '" & sPassPhrase & "');")
DoEvents
RC4Decrypt = oHTMLFile.getElementById("result").innerText
Set oElem = Nothing
Set oHTMLFile = Nothing
End Function
¡Advertencia!
Después de algunas pruebas y excavaciones, descubrí que la implementación de SHA3 de Crypto-JS no es una implementación estándar.
En un principio, SHA-3 era igual a Keccak, pero en el último momento, SHA-3 recibió un poco más de relleno para distinguirlo de Keccak. Esta biblioteca implementa Keccak, y Ethereum se desarrolló para usar la variante original de SHA-3 de Keccak.
Por lo tanto, no es realmente válido su uso.
Investigaré esto más a fondo y quizás encuentre otra biblioteca que podamos explotar.
Validación
Si desea validar la precisión de los hashes o el cifrado generados, simplemente debe utilizar uno de los muchos codificadores en línea. Sin embargo, tenga en cuenta que debe validar con varias fuentes, ya que descubrí que algunos sitios web tenían errores.
A continuación se muestran dos que utilicé y que parecían precisos en ese momento:
- https://codebeautify.org/md5-hash-generator
- https://md5calc.com/hash/md5 (me gusta este en particular)
Consideraciones
Fuente del archivo JavaScript
Las funciones le permiten utilizar la versión CDN de la biblioteca crypto-js o una copia local. Si desea utilizar una copia local, lo cual recomiendo, simplemente tome la URL de CDN de arriba e ingrésela en cualquier navegador. Guarde la página de renderizado como un archivo JS y ajuste el código según sea necesario para reflejar la ruta y el nombre del archivo. En todos los ejemplos anteriores, está codificado para encontrar el archivo llamado ‘crypto-js.min.js’ y se guardó en la subcarpeta ‘\js\crypto-js\4.0.0\’ de la carpeta de la base de datos de llamada.
¿Por qué recomiendo utilizar una copia local?
Bueno, hace que su base de datos sea autosuficiente y no requiera llamadas constantes a Internet.
Es más sensible ya que no hay retraso en la descarga del archivo de Internet.
¡La velocidad puede ser un problema!
Al utilizar el archivo CDN, a veces he experimentado errores que he atribuido a un problema de sincronización. Por eso verás que tengo la API Sleep implementada (a veces) para agregar un pequeño retraso que permita la descarga del archivo de Internet. Así que haz la prueba tú mismo y comprueba si es necesario o no.
Las declaraciones de la API de Sleep se pueden copiar desde:


¡EMPUJALO!
¡Esta es una gran oportunidad para mejorar el rendimiento mediante la implementación de SHOV! Si aún no está familiarizado con SHOV, consulte el siguiente artículo para obtener más información:


sigue leyendo
Algunos recursos adicionales para explorar


Implementaciones en JavaScript de algoritmos criptográficos estándar y seguros
CryptoJS es una colección en constante crecimiento de algoritmos criptográficos estándar y seguros implementados en JavaScript utilizando las mejores prácticas y patrones. Son rápidos y tienen una interfaz consistente y simple.


Historial de la página
Fecha | Resumen de Cambios |
---|---|
2024-03-20 | Versión inicial |
28-03-2024 | Se agregó un video de YouTube |