Sub EncryptRSAwithPublicKeyJSON As String
'======================================================================================
'1 Generate AES Symmetric Key (encryptKey) and encode key to Base 64.
'2 Generate JSON corresponding to the “payload” attribute (username, password, encryptKey, refreshToken).
'3 Encrypt JSON string from step 2 using MRA Public Key
'4 Encode encrypted JSON from step 3 to Base 64
'5 Generate JSON corresponding to the authentication request with a unique request ID
'6 Call the authentication API with username and EBS MRA ID in the request header and JSON from 5 in the request bodY
'AES Symmetric Key =========================
Dim r2 As String
Dim r3 As String
p="1234567812345678"
'kg.KeyFromBytes(md.GetMessageDigest(p.GetBytes("UTF8"),"SHA-256"))
kg.KeyFromBytes( p.GetBytes("UTF8") )
enCryptKey=su.EncodeBase64(cipher.Encrypt(p.GetBytes("UTF8"), kg.Key, False))
secukey=kg.Key
Log("SECKey : " & secukey)
Log("AESKey : " & enCryptKey)
'===========================================
'JSON corresponding to the Payload
objMap.Initialize
objMap.Put("username", MYUser)
objMap.Put("password", MYPwd )
objMap.Put("encryptKey", enCryptKey)
objMap.Put("refreshToken", "false")
objJSon.Initialize (objMap)
r2 = objJSon.ToPrettyString(5)
Log(r2)
'Encrypt JSON string from step 2 using Public Key
Dim kpg As KeyPairGenerator
Dim Cipher2 As Cipher
Cipher2.Initialize("RSA/ECB/PKCS1Padding")
kpg.Initialize("RSA", 4096)
kpg.PublicKeyFromBytes(su.DecodeBase64(MRA.MRAPublicKeyCert) )
r3= su.EncodeBase64(Cipher2.Encrypt(r2.GetBytes("UTF8") ,kpg.PublicKey,False))
'=============================================
Log (r3.Length)
Log (">>>" & r3)
Return r3
End Sub
Sub EncryptDataUsingKey2023 (receivingKey As String, OurData As String) As String
'1 Decrypt Key received from MRA using the random AES Key that was generated in the authentication step
' Base 64 encoded string of encrypted key. Key has been generated using AES
' 256(AES/ECB/PKCS7Padding) algorithm And encrypted with the encryptKey present in
' the request payload
'2. Generate invoice corresponding to sample JSON invoice (MRA requested format
'3 (OPTIONAL)Sign JSON in step 2 using the SHA256withRSA and the Private key of EBS downloaded during the registration process
'4 Encrypt JSON string from step 2 using decrypted Key from step 1 (Encrypted key that was received FROM AUTHENTICATE)
Log("MYKEY :" & p)
Log("MYKEY :" & enCryptKey)
Log("RCVKEY:" & receivingKey)
cipher.Initialize("AES/ECB/PKCS5Padding")
kg.KeyFromBytes( su.DecodeBase64(enCryptKey) )
secukey=kg.Key
Log("KEY1: " & secukey)
cipher.Initialize("AES/ECB/PKCS5Padding")
'RECEIVING-KEY decode/decrypt
Dim EKey() As Byte = cipher.Decrypt( su.DecodeBase64(receivingKey) ,secukey , False)
kg.KeyFromBytes(md.GetMessageDigest(EKey, "SHA-256"))
Log("KEY2: " & kg.Key)
cipher.Initialize("AES/ECB/PKCS5Padding")
'invoice encrypt/encode
Dim ENPInvoice As String = su.EncodeBase64(cipher.Encrypt(OurData.GetBytes("UTF8"), kg.Key,False ))
Log(ENPInvoice)
Return ENPInvoice
End Sub