Function Encrypt_RSA(str As String)
Dim encryptedJson As String
Dim dataToEncrypt() As Byte
dataToEncrypt = StrConv(str, vbFromUnicode)
Dim encryptedData() As Byte
'Create a new instance of RSACryptoServiceProvider
Dim rsa As New RSACryptoServiceProvider
Dim RSAPublicKeyInfo
Open ThisWorkbook.Path + "/Security/publicKeyXML.txt" For Input As #1
Input #1, inutile
Input #1, startRoot
Input #1, Modulus
Input #1, Exponent
Input #1, endRoot
Close #1
RSAPublicKeyInfo = startRoot + Modulus + Exponent + endRoot
Foglio1.Range("B12").Value = RSAPublicKeyInfo
rsa.FromXmlString (RSAPublicKeyInfo)
encryptedData = rsa.Encrypt(dataToEncrypt, True)
encryptedJson = EncodeBase64(encryptedData)
Encrypt_RSA = encryptedJson
End Function
Public Function EncodeBase64(ByRef arrData() As Byte) As String
Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement
' help from MSXML
Set objXML = New MSXML2.DOMDocument
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
EncodeBase64 = objNode.Text
End Function |