Public Function rsaEncryptBytes(abMessage() As Byte, strPublicKey As String) As Variant
' Uses RSA public key to encrypt a message using default PKCS-1-v1.5 encoding method
' Returns the resulting ciphertext as an array of bytes passed as a Variant,
' or an empty value on error.
Dim lngRet As Long
Dim abDummy(0) As Byte
Dim abBlock() As Byte
' All lengths are in octets (i.e. 8-bit bytes)
Dim nkLen As Long ' length, k, of RSA key modulus
Dim nmLen As Long ' length of message, M
rsaEncryptBytes = abDummy
' Compute lengths
nmLen = BytesLength(abMessage)
nkLen = RSA_KeyBytes(strPublicKey)
' Encode using EME
ReDim abBlock(nkLen - 1)
lngRet = RSA_EncodeMsg(abBlock(0), nkLen, abMessage(0), nmLen, PKI_EME_DEFAULT)
If lngRet <> 0 Then Exit Function
' Encrypt using RSA public key
lngRet = RSA_RawPublic(abBlock(0), nkLen, strPublicKey, 0)
If lngRet <> 0 Then Exit Function
' Return ciphertext block
rsaEncryptBytes = abBlock
End Function
Public Function rsaDecryptBytes(abCipher() As Byte, strPrivateKey As String) As Variant
' Decrypts RSA-encrypted ciphertext using the RSA private key passed as a string.
' Returns the resulting message as an array of bytes passed as a Variant,
' or an empty value on error.
Dim abDummy(0) As Byte
Dim abMessage() As Byte
Dim lngRet As Long
Dim abBlock() As Byte
Dim nkLen As Long ' length, k, of RSA key modulus
Dim nmLen As Long ' length of message, M
rsaDecryptBytes = abDummy
nkLen = BytesLength(abCipher)
If nkLen <> RSA_KeyBytes(strPrivateKey) Then
Exit Function
End If
abBlock = abCipher
' Decrypt
lngRet = RSA_RawPrivate(abBlock(0), nkLen, strPrivateKey, 0)
If lngRet <> 0 Then
Exit Function
End If
' Now we decode according to EME-PKCS-V1_5-DECODE
nmLen = RSA_DecodeMsg(0, 0, abBlock(0), nkLen, PKI_EME_DEFAULT)
If nmLen <= 0 Then Exit Function
ReDim abMessage(nmLen - 1)
nmLen = RSA_DecodeMsg(abMessage(0), nmLen, abBlock(0), nkLen, PKI_EME_DEFAULT)
' Output M.
rsaDecryptBytes = abMessage
End Function
Private Function BytesLength(abBytes() As Byte) As Long
' General function to return length of byte array
' Trap error if array is empty
On Error Resume Next
BytesLength = UBound(abBytes) - LBound(abBytes) + 1
End Function
Public Function TestEncryptAndDecrypt()
Dim abMessage() As Byte
Dim abBlock() As Byte
Dim strPublicKey As String
Dim strPubKeyFile As String
strPubKeyFile = "mykeypub.bin"
' Convert ANSI text to bytes
abMessage = StrConv("Hello world!", vbFromUnicode)
Debug.Print "M (ansi): " & StrConv(abMessage, vbUnicode)
Debug.Print "M (hex): " & cnvHexStrFromBytes(abMessage)
' Read in the public key from file
strPublicKey = rsaReadPublicKey(strPubKeyFile)
If Len(strPublicKey) = 0 Then
MsgBox "Cannot read RSA public key file '" & strPubKeyFile & "'", vbCritical
Exit Function
End If
' Call encrypt function
abBlock = rsaEncryptBytes(abMessage, strPublicKey)
Debug.Print "CT: " & cnvHexStrFromBytes(abBlock)
' DECRYPTION
Dim strPrivateKey As String
Dim strPriKeyFile As String
Dim abPlain() As Byte
strPriKeyFile = "mykeypri.bin"
' Read in the private key from file
strPrivateKey = rsaReadPrivateKey(strPriKeyFile, "password")
If Len(strPrivateKey) = 0 Then
MsgBox "Cannot read RSA private key file '" & strPriKeyFile & "'", vbCritical
Exit Function
End If
' Call decrypt function
abPlain = rsaDecryptBytes(abBlock, strPrivateKey)
Debug.Print "PT (hex) : " & cnvHexStrFromBytes(abPlain)
Debug.Print "PT (ansi): " & StrConv(abPlain, vbUnicode)
End Function