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