Android 将 RSA 公钥存储到文件夹
我正在尝试创建具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您具体询问公钥,我假设您不是在问如何保护它,而只是在询问 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.