Android Question Asymmetric encryption #2

pfillion

Member
Licensed User
Longtime User
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)


Hi wl wrote this code in another thread,

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 the key pair 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 key 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 keys once generated 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'd like to be able to paste the key private in a keygen with the text to encrypt and have the encrypted text back that could be decrypted with the public key in the program.

Thanks
 

pfillion

Member
Licensed User
Longtime User
This will save the key in a file. Then each time I want to encrypt something I have to code to get the proper RAF file according to what I want to encrypt. I tough that there was an easy way for me to convert them to string and then later just to copy and paste them in a key generator I would make to create codes. Instead of having a RAF file for all the the keys I keep for every application I make.

That way I generate new keys save them in a document and copy paste them in the same keygen. I don't have to modify the keygen to load a new RAF file every time I generate a new key pair...

Then again the data encrypted does not seem to be in an ascii readable format...

I guess I will have to stick to 3DES encryption with a single key.

Thanks.
 
Upvote 0
Top