Android Question RSA help need conect with webservices[Solved]

Carlos marin

Active Member
Licensed User
Hi experts
I'm doing a project with RSA encryption a small example, here I generate the two keys public and private encrypted a small message and send the message with the two keys the server must return the message without encrypting so the generated key is correct but I have an error after error. it does not work to give me some help.

anyone who uses the rsa library of andorid studio works perfectly I do not know what I'm missing or what I'm doing wrong

B4X:
    Dim data(0) As Byte
    Dim formats(0) As String
    Dim su As StringUtils
    Dim kpg As KeyPairGenerator
    Dim c1 As Cipher
    c1.Initialize("RSA/ECB/PKCS1Padding")
    kpg.Initialize("RSA", 1024)
    kpg.GenerateKey
    formats = kpg.Formats
  
    pubkey = kpg.PublicKeyToBytes
    prvkey = kpg.PrivateKeyToBytes


    Dim clear  As String = "prueba de encryptado rsa"
    data = Bconv.StringToBytes(clear, "UTF8")
    data = c1.Encrypt(data, kpg.PublicKey, False)

    Dim pkey As String = su.EncodeBase64(kpg.PublicKeyToBytes)
    Dim prikey As String = su.EncodeBase64(kpg.PrivateKeyToBytes)
    Dim data1 As String = su.EncodeBase64(data)
   

    Dim j As HttpJob
    j.Initialize("", Me)
    Dim mapjson As Map
    mapjson.Initialize
    mapjson.Put("publica",pkey)
    mapjson.Put("privada",prikey)
    mapjson.Put("mensaje",data1)
    js.Initialize(mapjson)
    strjson = js.ToString
   
    j.PostString("http://67.205.76.93:8008/decrypt2",strjson)
    j.GetRequest.SetContentType("application/json")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success = False Then
        Log(j.ErrorMessage)
    Else
        Log(j.GetString)
    End If
server response:
B4X:
{"result":"Error TypeError: Cannot read property 'decrypt' of undefined"}
 
Last edited:

Carlos marin

Active Member
Licensed User
Hi KMatle

thanks for responding, the webservice is an external provider, it was not possible to obtain the logic. the only thing that they indicate to me is that I must generate two keys for each user and they gave me this ip to do tests, They show me an example of how I should generate the key like this: but as I generate the key with this lib it is different

B4X:
"ServerKey":"-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCqDc9ai69q5QkkrSUM2SQoRml5\niM0VY0rM3umAQlB728EqDTMg3W7ljSQypMxLk6reB259yeH7hrkIhgNQ7mDxeRJs\nKlFzGCb2bbU/TQZD00YPoxPTLOB3aDO4MtRYGqs/2I55bVU9rzCTSXX1JxCTJo1S\nY5jcTmClIaIFop+9WwIDAQAB\n-----END PUBLIC KEY-----"}
 

Carlos marin

Active Member
Licensed User
i solved, thanks KMale

B4X:
c1.Initialize("RSA/NONE/OAEPPadding")
    kg.Initialize("RSA",3072)
    'kg.GenerateKey
    '---- PEDIR PUBLICA ----
    If miclase.pubk = "" Then
        
    Else
        key=su.DecodeBase64(miclase.pubk)
    End If
    kg.PublicKeyFromBytes(key)
    data = str_json.GetBytes("UTF8")
    data = c1.Encrypt(data,kg.PublicKey, False)
    Dim str2 As String = su.EncodeBase64(data)
 
Top