How to Encrypt and Decrypt Data in Android Application

Author: Veeresh Rudrappa. Date: Sep 14, 2012

Here is an easy and quick way to encrypt and decrypt data. I needed it for one of my project where I building an Anonymous Voting System on android. The technique shown here could be used else where on your Java projects as well.

Firstly generate a set of public and private keys, and the associated modulus. I'm assuming you already have a set of public and private key. Consider the generated keys are private key <253,407> and public key <37,407>, with 407 being the modulus.

Suppose the data you want to encrypt is an integer, say 5. Let us encrypt this data with the private key.

privateKey = 253
encryptedData = ( 5 ^ privateKey ) mod 407 = 301
Now let us decrypt this encrypted data 301 to get back our original data using the public key.
publicKey = 37
originalData = (301 ^ publicKey) mod 407 = 5

Below is the code to implement the above logic. In my project, I was encrypting the data in android application and then decrypting it in the Java servers. I have pasted the code snippet below. Use this code to get a good idea on encryption and decryption. The public and private keys I have used in the code are no way close to being called secure.
public class ElectionPage extends ListActivity 
{

	private static BigInteger ClientprivateKey=new BigInteger("21719");
	private static BigInteger ClientpublicKey=new BigInteger("59");
	private static BigInteger Clientmodulus= new BigInteger("37001");
	
	public static String EncryptData(String MyMessage,BigInteger privatekey,BigInteger modulus)
	{
	
		String EncryptedMessage = "";
    
		BigInteger mg;
	    BigInteger encrypt;
	    for(int i=0;i< MyMessage.length();i++)
                     {

                                if(i>0)
	    	  EncryptedMessage+=" ";
	    	  mg = intToBigint((int) MyMessage.charAt(i));
	    	  encrypt = encrypt(mg,privatekey,modulus);
	    	  EncryptedMessage+= encrypt.toString();
	      }
	      
	      return EncryptedMessage;
  }
  
                                       

                                   
  public static String  DecryptData(String MyMessage,BigInteger publicKey,BigInteger modulus)
  {
 
	  String DecryptedMessage = "";
	  BigInteger mg;
	  BigInteger decrypt;
	  String[] tokens = MyMessage.split(" ");
	  int ascii;
	  char ch;
	  for(int i=0;i< tokens.length; i++)
	  {
	   	  mg = intToBigint(Integer.parseInt(tokens[i]));
	   	  decrypt = decrypt(mg,publicKey,modulus);
	   	  ascii = decrypt.intValue();
	   	  ch = (char) ascii;
	   	  DecryptedMessage+=  ch;
	   	  
	  }

	  return DecryptedMessage;
}
  
static BigInteger encrypt(BigInteger message,BigInteger privatekey,BigInteger modulus) {
		      return message.modPow(privatekey, modulus);
}

static BigInteger decrypt(BigInteger encrypted,BigInteger publickey,BigInteger modulus) {
		      return encrypted.modPow(publickey, modulus);
}
  
	  static BigInteger intToBigint(int x) {  
	       return BigInteger.valueOf(x);  
	    }	
}

 
Hope this Helps :)
comments powered by Disqus