· 8 years ago · Nov 21, 2017, 02:12 AM
1package android.encdecsms;
2import java.security.Key;
3import javax.crypto.Cipher;
4import javax.crypto.spec.SecretKeySpec;
5import android.os.Bundle;
6import android.view.View;
7import android.widget.Button;
8import android.widget.EditText;
9import android.widget.TextView;
10import android.widget.Toast;
11import android.app.Activity;
12public class DisplaySMSActivity extends Activity {
13EditText secretKey;
14TextView senderNum;
15TextView encryptedMsg;
16TextView decryptedMsg;
17Button submit;
18Button cancel;
19String originNum = "";
20String msgContent = "";
21@Override
22public void onCreate(Bundle savedInstanceState) {
23super.onCreate(savedInstanceState);
24setContentView(R.layout.onreceive);
25senderNum = (TextView) findViewById(R.id.senderNum);
26encryptedMsg = (TextView) findViewById(R.id.encryptedMsg);
27decryptedMsg = (TextView) findViewById(R.id.decryptedMsg);
28secretKey = (EditText) findViewById(R.id.secretKey);
29submit = (Button) findViewById(R.id.submit);
30cancel = (Button) findViewById(R.id.cancel);
31// get the Intent extra
32Bundle extras = getIntent().getExtras();
33if (extras != null) {
34// get the sender phone number from extra
35originNum = extras.getString("originNum");
36// get the encrypted message body from extra
37msgContent = extras.getString("msgContent");
38// set the text fields in the UI
39senderNum.setText(originNum);
40encryptedMsg.setText(msgContent);
41} else {
42// if the Intent is null, there should be something wrong
43Toast.makeText(getBaseContext(), "Error Occurs!",
44Fall 2017 49
45Toast.LENGTH_SHORT).show();
46finish();
47}
48// when click on the cancel button, return
49cancel.setOnClickListener(new View.OnClickListener() {
50public void onClick(View v) {
51finish();
52}
53});
54// when click on the submit button decrypt the message body
55submit.setOnClickListener(new View.OnClickListener() {
56public void onClick(View v) {
57// user input the AES secret key
58String secretKeyString = secretKey.getText().toString();
59 //key length should be 16 characters as defined by AES-128-bit
60if (secretKeyString.length() > 0
61&& secretKeyString.length() == 16) {
62try {
63// convert the encrypted String message body to a byte
64// array
65byte[] msg = hex2byte(msgContent.getBytes());
66// decrypt the byte array
67byte[] result = decryptSMS(secretKey.getText()
68.toString(), msg);
69// set the text view for the decrypted message
70decryptedMsg.setText(new String(result));
71} catch (Exception e) {
72// in the case of message corrupted or invalid key
73// decryption cannot be carried out
74decryptedMsg.setText("Message Cannot Be Decrypted!");
75}
76} else
77Toast.makeText(getBaseContext(),
78"You must provide a 16-character secret key!",
79Toast.LENGTH_SHORT).show();
80}
81});
82}
83// utility function: convert hex array to byte array
84public static byte[] hex2byte(byte[] b) {
85if ((b.length % 2) != 0)
86throw new IllegalArgumentException("hello");
87byte[] b2 = new byte[b.length / 2];
88Fall 2017 50
89for (int n = 0; n < b.length; n += 2) {
90String item = new String(b, n, 2);
91b2[n / 2] = (byte) Integer.parseInt(item, 16);
92}
93return b2;
94}
95// decryption function
96public static byte[] decryptSMS(String secretKeyString, byte[] encryptedMsg)
97throws Exception {
98// generate AES secret key from the user input secret key
99Key key = generateKey(secretKeyString);
100// get the cipher algorithm for AES
101Cipher c = Cipher.getInstance("AES");
102// specify the decryption mode
103c.init(Cipher.DECRYPT_MODE, key);
104// decrypt the message
105byte[] decValue = c.doFinal(encryptedMsg);
106return decValue;
107}
108private static Key generateKey(String secretKeyString) throws Exception {
109// generate AES secret key from a String
110Key key = new SecretKeySpec(secretKeyString.getBytes(), "AES");
111return key;
112}
113}