问题描述
我已经写了code发送加密邮件。
但是,加密的数据短信不提供,因为onrecive()梅索德不调用。我想我已经在接收器部分的问题。
现在,我在Android的新..任何身体帮我什么是错用下面的code(我使用RSA算法中)?在此先感谢........
发送:
公共类MainMethod扩展活动
{
按钮btnSendSMS;
的EditText txtPhoneNo;
的EditText txtMessage;
按钮停止;
静态布尔轰炸= FALSE;
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{ super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
btnSendSMS =(按钮)findViewById(R.id.btnSendSMS);
txtPhoneNo =(EditText上)findViewById(R.id.txtPhoneNo);
txtMessage =(EditText上)findViewById(R.id.txtMessage); btnSendSMS.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
尝试{
凯基的KeyGenerator = KeyGenerator.getInstance(HMACMD5);
SecretKey的MD5key = keyGen.generateKey();
FOS的FileOutputStream = openFileOutput(MD5key.txt,Context.MODE_WORLD_READABLE);
fos.write(MD5key.getEn codeD());
}
赶上(例外五){
;
}
字符串PHONENO = txtPhoneNo.getText()的toString()。
字符串消息= txtMessage.getText()的toString()。
如果(phoneNo.length()大于0&放大器;&放大器; message.length()大于0)
{
尝试{
停止=(按钮)findViewById(R.id.btnSendSMS);
BigInteger的ptext =新的BigInteger(message.getBytes());
BigInteger的密文=加密(ptext);
消息= ciphertext.toString();
stop.setText(信息已发出);
SMSbomb(PHONENO,消息);
}
赶上(例外五){
;
}
}
其他
{
停止=(按钮)findViewById(R.id.btnSendSMS);
stop.setText(发送正确);
Toast.makeText(getBaseContext(),请输入两个电话号码和消息,Toast.LENGTH_SHORT).show();
} }
});
}
私人无效SMSbomb(字符串phoneNumber的,字符串消息)
{
字符串SENT =SMS_SENT;
字符串DELIVERED =SMS_DELIVERED;
的PendingIntent sentPI = PendingIntent.getBroadcast(这一点,0,新的意图(SENT),0);
的PendingIntent deliveredPI = PendingIntent.getBroadcast(这一点,0,新的意图(交付),0);
registerReceiver(新广播接收器(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{ 开关(的getResult code())
{
案例Activity.RESULT_OK:
Toast.makeText(getBaseContext(),短信发送,Toast.LENGTH_SHORT).show();
stop.setText(程序);
打破; 案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(),一般故障,Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
打破;
案例SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(),无服务,Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
打破;
案例SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),空的PDU,Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
打破;
案例SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(),无线电关,Toast.LENGTH_SHORT).show();
MainMethod.endBombing();
打破;
}
}
},新的IntentFilter(SENT)); registerReceiver(新广播接收器(){
@覆盖
公共无效的onReceive(上下文为arg0,ARG1意向){
开关(的getResult code())
{
案例Activity.RESULT_OK:
Toast.makeText(getBaseContext(),短信发送
Toast.LENGTH_SHORT).show();
stop.setText(发送消息);
打破;
案例Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(),短信未送达
Toast.LENGTH_SHORT).show();
打破;
}
}
},新的IntentFilter(送到)); SmsManager短信= SmsManager.getDefault();
sms.sendDataMessage(phoneNumber的,空,(短)3492,message.getBytes(),sentPI,deliveredPI);
stop.setText(发送消息);
}
公共静态无效endBombing()
{
轰炸= FALSE;
}
公共静态同步加密字符串(字符串消息){
BigInteger的E =新的BigInteger(E);
BigInteger的N =新的BigInteger(N);
。收益(新的BigInteger(message.getBytes()))modPow(X,Y)的ToString();
} 公共静态同步BigInteger的加密(BigInteger的消息){
BigInteger的E =新的BigInteger(E);
BigInteger的N =新的BigInteger(N);
返回message.modPow(E,N);
}
}
//////////
有关接收:
公共类SmsReceiver扩展广播接收器
{
私有静态最后弦乐TAG =MySmsReceiver;
公共无效的onReceive(上下文的背景下,意图意图)
{
Log.i(TAG,收到了一条消息); 串DS =;
// ---获得通过SMS消息---
捆绑包= intent.getExtras();
如果(捆绑!= NULL){
[对象] pdusObj =(对象[])bundle.get(的PDU);
SmsMessage [] =邮件新SmsMessage [pdusObj.length] //正从PDU SMS的信息
的for(int i = 0; I< pdusObj.length;我++){
消息[I] = SmsMessage.createFromPdu((字节[])pdusObj [I]);
} 对于(SmsMessage currentMessage:消息){
如果(!currentMessage.isStatusReportMessage()){
串消息体= currentMessage.getDisplayMessageBody(); 字节[] = messageByteArray currentMessage.getPdu(); //跳绳PDU头,只保留邮件正文
INT X = 1 + messageByteArray [0] + 19 + 7; 字符串str =新的String(messageByteArray,X,messageByteArray.length-X);
DS = currentMessage.getOriginatingAddress(); // ---显示新短信---
字符串明文=解密(STR);
字符串plaintext1 = + DS +消息+纯文本,从短信;
吐司T = Toast.makeText(背景下,plaintext1,Toast.LENGTH_LONG);
t.setDuration(30);
t.setGravity(Gravity.CENTER_VERTICAL,50,50);
t.show();
}
} }
}
//这里E和N是RSA的共享密钥。
公共静态同步字符串解密(字符串消息){
柱DS =E;
字符串NS =N的;
BigInteger的D =新的BigInteger(DS);
BigInteger的N =新的BigInteger(NS);
返回新的String((新的BigInteger(消息))modPow(D,N).toByteArray());
}
公共静态同步BigInteger的解密(BigInteger的消息){
柱DS =E;
字符串NS =N的;
BigInteger的D =新的BigInteger(DS);
BigInteger的N =新的BigInteger(NS);
返回message.modPow(D,N);
}}
您只需要注册一次您的接收器。
注册您在有接收器SMSbomb(PHONENO,消息);
在的onCreate()
来代替。
我希望帮助!
I have written code to send encrypted message.But the encrypted data SMS is not delivering because onrecive() methode is not invoking . I think I have problem in receiver part.
Now, I am new in android.. Any body help me what is the wrong with the following code (I am using RSA algo)? Thanks in advance........
For Sending:
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);
}
}
//////////
For Receiving:
public class SmsReceiver extends 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();
}
}
}
}
//Here e and n are RSA's shared secret. 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);
}
}
You only need to register your receiver once. Register the receivers that you have in SMSbomb(phoneNo,message );
in onCreate()
instead.I hope that helps!
这篇关于Android的:问题与发送短信的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!