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