我已编写代码来发送加密邮件。 但加密的数据SMS没有提供,因为onrecive()方法没有调用。我想我的接收器部分有问题。
现在,我是android的新手..任何身体帮助我下面的代码有什么问题(我使用的是RSA算法)?在此先感谢........
发送:
public class MainMethod extends Activity
{
Button btnSendSMS;
EditText txtPhoneNo;
EditText txtMessage;
Button stop ;
static boolean bombing = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) findViewById(R.id.txtMessage);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try{
KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
SecretKey MD5key = keyGen.generateKey();
FileOutputStream fos = openFileOutput( "MD5key.txt" ,Context.MODE_WORLD_READABLE);
fos.write(MD5key.getEncoded());
}
catch(Exception e) {
;
}
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
if (phoneNo.length()>0 && message.length()>0)
{
try{
stop = (Button)findViewById(R.id.btnSendSMS);
BigInteger ptext = new BigInteger(message.getBytes());
BigInteger ciphertext = encrypt(ptext);
message = ciphertext.toString();
stop.setText("Message Sent");
SMSbomb(phoneNo,message );
}
catch(Exception e) {
;
}
}
else
{
stop = (Button)findViewById(R.id.btnSendSMS);
stop.setText("Send Properly");
Toast.makeText(getBaseContext(), "Please enter both phone number and message.", Toast.LENGTH_SHORT).show();
}
}
});
}
private void SMSbomb(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
stop.setText("Proceeding");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
stop.setText("Send Message");
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendDataMessage(phoneNumber , null, (short) 3492, message.getBytes(), sentPI, deliveredPI);
stop.setText("Send Message");
}
public static void endBombing()
{
bombing = false;
}
public static synchronized String encrypt(String message) {
BigInteger e = new BigInteger("e") ;
BigInteger n = new BigInteger(n");
return (new BigInteger(message.getBytes())).modPow(e, n).toString();
}
public static synchronized BigInteger encrypt(BigInteger message) {
BigInteger e = new BigInteger("e") ;
BigInteger n = new BigInteger("n");
return message.modPow(e, n);
}
}
//////////
接收:
公共类SmsReceiver扩展了BroadcastReceiver {
private static final String TAG = "MySmsReceiver";
public void onReceive(Context context, Intent intent)
{
Log.i(TAG, "Recieved a message");
String ds = "" ;
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
// getting SMS information from PDU
for (int i = 0; i < pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
}
for (SmsMessage currentMessage : messages) {
if (!currentMessage.isStatusReportMessage()) {
String messageBody = currentMessage.getDisplayMessageBody();
byte[] messageByteArray = currentMessage.getPdu();
// skipping PDU header, keeping only message body
int x = 1 + messageByteArray[0] + 19 + 7;
String str = new String(messageByteArray, x, messageByteArray.length-x);
ds = currentMessage.getOriginatingAddress() ;
//---display the new SMS message---
String plaintext = decrypt(str);
String plaintext1 = "SMS from " + ds + " Message " + plaintext ;
Toast t = Toast.makeText(context, plaintext1, Toast.LENGTH_LONG);
t.setDuration(30);
t.setGravity(Gravity.CENTER_VERTICAL, 50, 50);
t.show();
}
}
}
}
//这里e和n是RSA的共享秘密。 public static synchronized String decrypt(String message){ String ds =“e”; 字符串ns =“n”; BigInteger d = new BigInteger(ds); BigInteger n = new BigInteger(ns); return new String((new BigInteger(message))。modPow(d,n).toByteArray()); }
public static synchronized BigInteger decrypt(BigInteger message) {
String ds = "e" ;
String ns = "n" ;
BigInteger d = new BigInteger(ds) ;
BigInteger n = new BigInteger(ns) ;
return message.modPow(d, n);
}
}
答案 0 :(得分:1)
您只需要注册一次接收器。
请在SMSbomb(phoneNo,message );
onCreate()
中注册您拥有的接收器。
我希望有所帮助!