简体   繁体   中英

Android Storing RSA Public Key to folder

I'm trying to create a SMS Apps with RSA encryption protection. I would like to ask how can I store the PublicKey?

I tried to import methods from Storing.class, but failed.

this is my code

public class storePubKey extends BroadcastReceiver
{   
Storing store = new Storing();

public void onReceive(Context context, Intent intent)
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    if (bundle != null)
    {
        try{
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str = msgs[i].getMessageBody().toString();                
            store.saveToFile("public.key",str);

        }
        }catch(Exception e){}

        }
        //---display the new SMS message---
        try {
            Toast.makeText(context,("public.keyYYYYYY"), Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}         

public class Storing extends Activity {

 public void saveToFile(String filename, String sms) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException{
        OutputStreamWriter out = new OutputStreamWriter(openFileOutput(filename, Context.MODE_APPEND));
        out.write(sms);


        out.close();

}

Since you're asking about the public key specifically, I'll assume you're not asking how to secure it, and you're just asking about IO.

You have several options, detailed in the Data Storage section of the Android developer's guide. The 3 main ones for local storage are SQL database, SharedPreferences, and Files. If the question is more about how to store the user's public key on their own device, that would probably either be a file or a shared preference. However, since you're going to be storing multiple public keys (the ones of everyone you're communicating with, in order to decrypt their messages), I'd recommend going the SQL database route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM