This hands-on lab practice is intended to help students develop a better understanding of encryption for data at rest and the consequences of not having encryption.
Create a new Android Studio project and name it “Encrypt” with company domain of "android.encrypt" and click on Next.
Create a new project, choose "Empty Project" and click on "Next"
Name the project as "Encrypt" and click on "Finish" You can leave it on the default settings
Click on "File-->New-->Activity--> EmptyActivity"
Enter the Activity Name as "EncryptActivity" and Layout Name as "activity_encrypt" and click on "Finish"
Now, click on "res-->New-->Directory"
Name the directory as "menu"
Now, click on "res-->New-->Directory"
Name the directory as "menu_enrypt.xml"
Copy and paste all the codes into specified files
//Copy and paste the following code into "EncryptActivity.java".
//EncryptActivity.java
package encrypt.android.encrypt;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import javax.crypto.Cipher;
public class EncryptActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
generateKey();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_encrypt);
final Button enButton = (Button) findViewById(R.id.enbutton);
final Button deButton = (Button) findViewById(R.id.debutton);
final EditText input = (EditText) findViewById(R.id.Input);
final EditText Raw = (EditText) findViewById(R.id.raw);
final EditText output = (EditText) findViewById(R.id.OriginText);
enButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
Raw.setText(encrypt(input.getText().toString()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
});
deButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
output.setText(String.valueOf(decrypt(Raw.getText().toString())));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private final static String RSA = "RSA";
public static PublicKey uk;
public static PrivateKey rk;
public static void generateKey() throws Exception {
KeyPairGenerator gen = KeyPairGenerator.getInstance(RSA);
gen.initialize(512, new SecureRandom());
KeyPair keyPair = gen.generateKeyPair();
uk = keyPair.getPublic();
rk = keyPair.getPrivate();
}
private static byte[] encrypt(String text, PublicKey pubRSA)
throws Exception {
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, pubRSA);
return cipher.doFinal(text.getBytes());
}
public final static String encrypt(String text) {
try {
return byte2hex(encrypt(text, uk));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public final static String decrypt(String data) {
try {
return new String(decrypt(hex2byte(data.getBytes())));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] decrypt(byte[] src) throws Exception {
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, rk);
return cipher.doFinal(src);
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xFF);
if (stmp.length() == 1)
hs += ("0" + stmp);
else
hs += stmp;
}
return hs.toUpperCase();
}
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("hello");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_encrypt, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//Copy and paste the following code into "activity_encrypt.xml".
//activity_encrypt.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World, EncryptActivity!" />
<EditText
android:id="@+id/Input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input Message" />
<Button
android:id="@+id/enbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Encrypt it by PublicKey" />
<EditText
android:id="@+id/raw"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Encrypted Message" />
<Button
android:id="@+id/debutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Decrypt it by PrivateKey" />
<EditText
android:id="@+id/OriginText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Original Message" />
</LinearLayout>
//Copy and paste the following code into the newly created "menu_encrypt.xml".
//menu_encrypt.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".EncryptActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
In the project sidebar, open the "strings.xml" file in the "values" folder.
//Copy and paste the following code into "strings.xml".
//strings.xml
<resources>
<string name="app_name">Encrypt</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
//Copy and paste the following code into "AndroidManifest.xml". It can be located in the manifest folder in the project sidebar.
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="encrypt.android.encrypt" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Encrypt" >
<activity
android:name=".EncryptActivity"
android:exported="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Save your project and run it on the AVD.
The first output interface shall be look like this where you can enter any messages you want that shall show as enecripted message
After entering a message into the Input Message text box, and click on the Encrypt button, the message will be encrypted and be displayed in the Encrypted Message
Clicking on the Decrypt it by PrivateKey will decrypt the encrypted message and result in your original inputted message.