Android Question Asymmetric encryption

wl

Well-Known Member
Licensed User
Longtime User
Hi,

I have used the Encryption library succesfully to do symmetric encryption.

I noticed the library can generate RSA keys (asymmetric encryption), but it seems it can not encrypt/decrypt RSA ?

Tx
 

wl

Well-Known Member
Licensed User
Longtime User
I can confirm this just works. For anyone interested:

B4X:
    'Generate the RSA key pair
    Dim kpg As KeyPairGenerator
    kpg.Initialize("RSA", 512)
    kpg.GenerateKey
 
    'Encrypt a string
    Dim C As Cipher
    Dim plainText As String = "Hello world, this is an RSA test"
   
    C.Initialize("RSA/ECB/NoPadding")
    Dim encrypted() As Byte = C.Encrypt(plainText.GetBytes("UTF8"), kpg.privateKey, False)
   
    'Decrypt
    Dim bc As ByteConverter
    Dim decrypted As String = bc.StringFromBytes(C.Decrypt(encrypted, kpg.PublicKey, False), "UTF8")
   
    ToastMessageShow(decrypted, True)
 
Upvote 0

pfillion

Member
Licensed User
Longtime User
Hi,

Can you tell me how to store the private and public RSA key in a variable after I generate it for later use ? Tried to convert it to string but it did not work...

I would like to add an offline registration mode in my app. To do this, I would like the user to send me a code (it will be it's device ID)

I want to encrypt that code with the private key and send him the new encrypted string that he will enter in my app.

Once entered, the app will save the string locally in a RAF file. I would then use the public key in the app to decrypt the string and compare the device id with the one in the string to validate the licence.

I just don't know how to save the private in a variable to reuse them easily in my app after I generate a pub/private key pair. I don't think that it would be practical to save the key once generate in a file and have to use the file to get the key back... I would not know how to regenerate the file if lost or corrupted...

I need this to be secure to make sure that nobody will make a keygen for all to use...

Thanks
 
Upvote 0
Top