You are on page 1of 6

Lab2: RSA Encryption program(Eclipse) - Mobile Security Labware

1 of 6

https://sites.google.com/site/mobilesecuritylabware/3-data-location-priva...

Mobile Security Labware


3. Data, Location and cryptography Privacy > Lab Activity > Cryptography for Mobile Apps > Cryptography Mobile Labs > Lab 1: Encryption/Decryption > 2.
Lab Activity >

Lab2: RSA Encryption program(Eclipse)


In this tutorial, we will learn to create an RSA Encrytion application. User can enter a text into the editable textbox and
then click the "Encrypt" button. The application will put the cipher text onto the second textbox. Then, clicking the
"Decrypt" button will result in the plain text shown in the last textbox.
Objective
This lab gives the reader a first impression of how the encryption and decryption works by taking RSA encryption and
decryption algorithm as an example. Also, we expect the reader to gain a general notion of the public-key encryption
infrastructure. In this lab, we will utilize the public-key infrastructure support by Java. For the private-key encryption
infrastructure example AES, we will leave it as a post-lab assignment for readers' implementation.
Software Requirement

Eclipse IDE

Java JDK, JRE

Android SDK

Tutorial
Lab1: RSA Encryption Program Video Demo

Create a new android project

Project Name: Encrypt


Target Name: Android 2.2.2
Package Name: Android.Encrypt
In Encrytpt->res->layout->main.xml,

4/9/2015 9:59 PM

Lab2: RSA Encryption program(Eclipse) - Mobile Security Labware

2 of 6

https://sites.google.com/site/mobilesecuritylabware/3-data-location-priva...

drag three EditTexts, two Buttons to the screen.


Set button1s Id property-> Id @+id/enbutton, and Text->Encrypt it by PublicKey
Set button2's Id property->Id @+id/debutton, and Text->Decrypt it by PirivateKey
Set EditText1's Id property-> Id @+id/Input, and Hint->Input Message
Set EditText2's Id property->Id @+id/raw, and Text->Encrypted Message
Set EditText3's Id property->Id @+id/OriginText, and Text->Original Message

Go to Encrypt->src->Android.Encrypt->EncryptActivity.java

4/9/2015 9:59 PM

Lab2: RSA Encryption program(Eclipse) - Mobile Security Labware

3 of 6

https://sites.google.com/site/mobilesecuritylabware/3-data-location-priva...

Copy and paste the following code in EncryptActivity.java


package Android.Encrypt;
import android.app.Activity;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;
import
import
import
import

java.security.KeyPair;
java.security.KeyPairGenerator;
java.security.PrivateKey;
java.security.PublicKey;

public class EncryptActivity extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try {
generateKey();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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";

4/9/2015 9:59 PM

Lab2: RSA Encryption program(Eclipse) - Mobile Security Labware

4 of 6

https://sites.google.com/site/mobilesecuritylabware/3-data-location-priva...

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)

4/9/2015 9:59 PM

Lab2: RSA Encryption program(Eclipse) - Mobile Security Labware

5 of 6

https://sites.google.com/site/mobilesecuritylabware/3-data-location-priva...

{
String item = new String(b, n, 2);
b2[n/2] = (byte)Integer.parseInt(item, 16);
}
return b2;
}

Save and run the project, enter a message in to the Input textbox, click the Encrypt it by PublicKey button, the result will be like this.

Then click the "Decrypt it by PrivateKey", you can see this.

Sample QR(Use Barcode Scanner on your Android Phone to scan this image):

Manual Download: Encrypt.apk

Discuss Board:

4/9/2015 9:59 PM

Lab2: RSA Encryption program(Eclipse) - Mobile Security Labware

6 of 6

https://sites.google.com/site/mobilesecuritylabware/3-data-location-priva...

Encrypt.apk

Zhengzhe Li,

v.7

Hello_world.apk

Zhengzhe Li,

v.7

Hello_world_QR.jpg

Zhengzhe Li,

v.7

Layout_Outline.jpg

Zhengzhe Li,

v.7

bq.png

Zhengzhe Li,

v.7

decrypt.jpg

Zhengzhe Li,

v.7

encrypt.jpg

Zhengzhe Li,

v.7

image002.jpg

Zhengzhe Li,

v.7

image004.jpg

Zhengzhe Li,

v.7

image006.jpg

Zhengzhe Li,

v.7

layout.jpg

Zhengzhe Li,

v.7

line.jpg

Zhengzhe Li,

v.7

Sign in | Recent Site Activity | Report Abuse | Print Page | Powered By Google Sites

4/9/2015 9:59 PM

You might also like