Android Question Encrypt decrypt using RSA

carlos7000

Well-Known Member
Licensed User
Longtime User
Hello everyone

I am trying to create a simple application that allows to send and receive messages in a secure way, using rsa or another similar system.

I wrote this little code to see if it was possible to create a pair of keys (one public and one private), save them and use them again later.

Simple Test:
    Dim TextOri As String
    Dim Encoded1, Encoded2 As String
    Dim cBytes() As Byte
    Dim Decoded As String
    Dim PublicKey1, PrivateKey1 As String
    Dim PublicKey2, PrivateKey2 As String
    
    TextOri = "Hello World"
    
    'Generate keys
    k.GenerateKey   
    PublicKey1 = su.EncodeBase64(k.PublicKeyToBytes)
    PrivateKey1 = su.EncodeBase64(k.PrivateKeyToBytes)   
    
    'Encode
    Encoded1 = su.EncodeBase64(TextOri.GetBytes("UTF8"))
    
    'Generate new keys
    k.GenerateKey
    PublicKey2 = su.EncodeBase64(k.PublicKeyToBytes)
    PrivateKey2 = su.EncodeBase64(k.PrivateKeyToBytes)
        
    'I try to use the first set of keys
    k.PublicKeyFromBytes(PublicKey1.GetBytes("UTF8"))
    k.PrivateKeyFromBytes(PrivateKey1.GetBytes("UTF8"))
    
    'Encode
    Encoded2 = su.EncodeBase64(TextOri.GetBytes("UTF8"))


I have read several post, but I can't find one that shows how I can use a pair of pre-generated keys to encode decode.

It can be Rsa or another similar algorithm.
 

carlos7000

Well-Known Member
Licensed User
Longtime User
You need to provide more information. How and where are you sending the messages to?

Why not use a secure connection?

Hi Erel.

They have asked me for an application that can send a text message by sms safely (using rsa or another algorithm).

Although today there are a multitude of applications that can send texts with total security, starting with WhatsApp. There are still many companies that use obsolete hardware or software.

They use sms to send some important messages, because they have old phones that cannot run whatsapp and others also do not have internet.

Thanks
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
They have asked me for an application that can send a text message by sms safely (using rsa or another algorithm).
A sms can only be a few characters long. Encrypting "Hello World" does probably generate a longer Text (as you need to convert the encrypted bytes to be a base64 string).

Also your app need to be the default device SMS App on the receiverside as the default SMS App does not decrypt anything.

The Deviceuser will see a SMS with base64 text and are not able to read that sms.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
You need a server between (as Manfred mentioned SMS is not usable as you need to read/send SMS in your app which is restricted on actual Android versions) which handles all the messages (probably with a database). B4J is a good choice on servers where you can run own sw (like VPS). PHP can be used on any cheap hosted solution.

Use FCM (Firebase Messaging) for messages up to 4 KB or at least to push an info that there is a message (like WhatsApp and other messengers do). Think about a user registration and security features. Every client needs an own private and public key. The public one needs to be exchanged, stored and maybe updated. Another to do for you app.

Use the search function and you'll find tons of examples (RSA, AES, PHP, B4J-Server, Databases, Firebase, etc.). Please take care that this is a huge project.
 
Upvote 0

carlos7000

Well-Known Member
Licensed User
Longtime User
You need a server between (as Manfred mentioned SMS is not usable as you need to read/send SMS in your app which is restricted on actual Android versions) which handles all the messages (probably with a database). B4J is a good choice on servers where you can run own sw (like VPS). PHP can be used on any cheap hosted solution.

Use FCM (Firebase Messaging) for messages up to 4 KB or at least to push an info that there is a message (like WhatsApp and other messengers do). Think about a user registration and security features. Every client needs an own private and public key. The public one needs to be exchanged, stored and maybe updated. Another to do for you app.

Use the search function and you'll find tons of examples (RSA, AES, PHP, B4J-Server, Databases, Firebase, etc.). Please take care that this is a huge project.

Hello KMatle.

The server is not necessary.

The first time the application is run, each user creates their key pair, and shares their public key.

Each person stores the public keys of the people to whom they wish to send messages.

Who wants to send a message, select a recipient, and write a message. The program encrypts the message with the recipient's public key and copies the encrypted text to the clipboard, the user opens the sms application on the phone, pastes and sends the message.

The receiver copies the text from the sms to application. Decode the message with your private key.

The application cannot use the internet.

Thanks.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Will work of course
Most probably NOT using SMS due to the payload size. AND other hurdles on the way ;-)

 
Upvote 0

carlos7000

Well-Known Member
Licensed User
Longtime User
Most probably NOT using SMS due to the payload size. AND other hurdles on the way ;-)


Hello DonManfred
Like most of the time, the number of words that are sent are few, I don't think there will be a problem.

Thank you very much for the link to the library. I'm going to try it.

Thank you.
 
Upvote 0
Top