· 8 years ago · Nov 21, 2017, 02:06 AM
1package android.encdecsms;
2import java.security.Key;
3import java.util.ArrayList;
4import javax.crypto.Cipher;
5import javax.crypto.spec.SecretKeySpec;
6import android.app.Activity;
7import android.os.Bundle;
8import android.telephony.SmsManager;
9import android.view.View;
10import android.widget.Button;
11import android.widget.EditText;
12import android.widget.Toast;
13public class EncDecSMSActivity extends Activity {
14 /** Called when the activity is first created. */
15 EditText recNum;
16 EditText secretKey;
17 EditText msgContent;
18 Button send;
19 Button cancel;
20 @Override
21 public void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.main);
24 recNum = (EditText) findViewById(R.id.recNum);
25 secretKey = (EditText) findViewById(R.id.secretKey);
26 msgContent = (EditText) findViewById(R.id.msgContent);
27 send = (Button) findViewById(R.id.Send);
28 cancel = (Button) findViewById(R.id.cancel);
29 // finish the activity when click Cancel button
30 cancel.setOnClickListener(new View.OnClickListener() {
31 public void onClick(View v) {
32 finish();
33 }
34 });
35 Fall 2017 44
36 // encrypt the message and send when click Send button
37 send.setOnClickListener(new View.OnClickListener() {
38 public void onClick(View v) {
39 String recNumString = recNum.getText().toString();
40 String secretKeyString = secretKey.getText().toString();
41 String msgContentString = msgContent.getText().toString();
42 // check for the validity of the user input
43 // key length should be 16 characters as defined by AES-128-bit
44 if (recNumString.length() > 0 && secretKeyString.length() > 0 && msgContentString.length() > 0 && secretKeyString.length() == 16) {
45 // encrypt the message
46 byte[] encryptedMsg = encryptSMS(secretKeyString,
47 msgContentString);
48 // convert the byte array to hex format in order for
49 // transmission
50 String msgString = byte2hex(encryptedMsg);
51 // send the message through SMS
52 sendSMS(recNumString, msgString);
53 // finish
54 finish();
55 } else
56 Toast.makeText(
57 getBaseContext(),
58 "Please enter phone number, secret key and the message. Secret key must be 16 characters!",
59 Toast.LENGTH_SHORT).show();
60 }
61 });
62 }
63 public static void sendSMS(String recNumString, String encryptedMsg) {
64 try {
65 // get a SmsManager
66 SmsManager smsManager = SmsManager.getDefault();
67 // Message may exceed 160 characters
68 // need to divide the message into multiples
69 ArrayList < String > parts = smsManager.divideMessage(encryptedMsg);
70 smsManager.sendMultipartTextMessage(recNumString, null, parts,
71 null, null);
72 } catch (Exception e) {
73 e.printStackTrace();
74 }
75 }
76 // utility function
77 public static String byte2hex(byte[] b) {
78 String hs = "";
79 String stmp = "";
80 Fall 2017 45
81 for (int n = 0; n < b.length; n++) {
82 stmp = Integer.toHexString(b[n] & 0xFF);
83 if (stmp.length() == 1)
84 hs += ("0" + stmp);
85 else
86 hs += stmp;
87 }
88 return hs.toUpperCase();
89 }
90 // encryption function
91 public static byte[] encryptSMS(String secretKeyString,
92 String msgContentString) {
93 try {
94 byte[] returnArray;
95 // generate AES secret key from user input
96 Key key = generateKey(secretKeyString);
97 // specify the cipher algorithm using AES
98 Cipher c = Cipher.getInstance("AES");
99 // specify the encryption mode
100 c.init(Cipher.ENCRYPT_MODE, key);
101 // encrypt
102 returnArray = c.doFinal(msgContentString.getBytes());
103 return returnArray;
104 } catch (Exception e) {
105 e.printStackTrace();
106 byte[] returnArray = null;
107 return returnArray;
108 }
109 }
110 private static Key generateKey(String secretKeyString) throws Exception {
111 // generate secret key from string
112 Key key = new SecretKeySpec(secretKeyString.getBytes(), "AES");
113 return key;
114 }
115}