我已經編寫了發送加密消息的代碼。 但是由於onrecive()方法沒有調用,加密的數據SMS沒有發送。我認爲我在接收部分有問題。Android:發送數據短信的問題
現在,我是新的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擴展廣播接收器 {
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」; String 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);
}
}