Tuesday, May 14, 2013

[android help] encryption and decryption using RSA in android app


i have been trying to develop an android app which could encrypt and decrypt the SMS of the inbox..but there is some error in the public key and private key.. here is my code



public static PublicKey generateRsaPublicKey(BigInteger modulus, BigInteger publicExponent)
{
try
{
return KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
}
catch(Exception e)
{
// Logger.e(e.toString());
}
return null;
}

public static PrivateKey generateRsaPrivateKey(BigInteger modulus, BigInteger privateExponent)
{
try
{
return KeyFactory.getInstance("RSA").generatePrivate(new RSAPrivateKeySpec(modulus, privateExponent));
}
catch(Exception e)
{
// Logger.e(e.toString());
}
return null;
}

public static byte[] rsaEncrypt(byte[] name, PublicKey key)
{
try
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(name);
}
catch(Exception e)
{
// Logger.e(e.toString());
}
return null;
}

public static byte[] rsaDecrypt(byte[] dsms, PrivateKey key)
{
try
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(dsms);
}
catch(Exception e)
{
// Logger.e(e.toString());
}
return null;
}


code for encrypt-



String data = StringCryptor.rsaEncrypt( byte[] name, PublicKey );
String enSMS=sender + "\n" + data;
//Toast.makeText( this, data, Toast.LENGTH_SHORT).show();
EditText txtEncryptSMS = (EditText) findViewById(R.id.EncryptText);
txtEncryptSMS.setText(enSMS);

Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSms, null,null,null,null);
//int id = c.getInt(0);
//int thread_id = c.getInt(1); //get the thread_id
getContentResolver().delete(Uri.parse("content://sms/" + msgid),null,null);

ContentValues values = new ContentValues();
values.put("address", sender);
values.put("body", data);
//values.put("type", 2);
//values.put("date", rcvdDateTime);

getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

}


code for decrypt-



String data = StringCryptor.rsaDecrypt( byte[] name, PrivateKey );
String enSMS=sender + "\n" + data;
//Toast.makeText( this, data, Toast.LENGTH_SHORT).show();
EditText txtEncryptSMS = (EditText) findViewById(R.id.EncryptText);
txtEncryptSMS.setText(enSMS);

Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSms, null,null,null,null);
//int id = c.getInt(0);
//int thread_id = c.getInt(1); //get the thread_id
getContentResolver().delete(Uri.parse("content://sms/" + msgid),null,null);

ContentValues values = new ContentValues();
values.put("address", sender);
values.put("body", data);
//values.put("type", 2);
//values.put("date", rcvdDateTime);

getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

}


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...