gpt4 book ai didi

Android:发送数据短信时出现问题

转载 作者:行者123 更新时间:2023-11-29 02:12:39 25 4
gpt4 key购买 nike

我已经编写了发送加密消息的代码。但是加密的数据 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);
}
}

//////////

对于接收:

公共(public)类 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的共享 key 。 公共(public)静态同步字符串解密(字符串消息){ 字符串 ds = "e"; 字符串 ns = "n"; BigInteger d = new BigInteger(ds) ; BigInteger n = new BigInteger(ns) ; 返回新字符串((新 BigInteger(消息)).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);
}

}

最佳答案

您只需注册您的接收器一次。改为在 onCreate() 中注册您在 SMSbomb(phoneNo,message ); 中的接收器。希望对您有所帮助!

关于Android:发送数据短信时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6318689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com