Android 将 RSA 公钥存储到文件夹

发布于 2024-11-28 16:57:41 字数 1527 浏览 1 评论 0原文

我正在尝试创建具有 RSA 加密保护的短信应用程序。 我想问一下如何保存PublicKey?

我尝试从 Storage.class 导入方法,但失败了。

这是我的代码

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();

}

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();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所有深爱都是秘密 2024-12-05 16:57:41

由于您具体询问公钥,我假设您不是在问如何保护它,而只是在询问 IO。

您有多个选项,详细信息请参阅Android 开发者指南。本地存储的 3 个主要存储是 SQL 数据库、SharedPreferences 和文件。如果问题更多是关于如何将用户的公钥存储在他们自己的设备上,那么这可能是文件或共享首选项。但是,由于您将存储多个公钥(与您通信的每个人的公钥,以便解密他们的消息),因此我建议使用 SQL 数据库路线。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文