1

As i am new to Encryption i am stucked and your help is highly appreciated.

I have two class one is used to encrypt and decrypt the text send from main.xml

Base64Activity:

public class Base64Activity extends Activity {
private Button btnEncrypt, btnDecrypt;
private EditText txtOrg, txtEncr, txtEncr1, txtDecr;
private byte[] encrypted;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnEncrypt = (Button)findViewById(R.id.button1);
btnDecrypt = (Button)findViewById(R.id.button2);
txtOrg = (EditText)findViewById(R.id.editText1);
txtEncr = (EditText)findViewById(R.id.editText2);
txtEncr1 = (EditText)findViewById(R.id.editText3);
txtDecr = (EditText)findViewById(R.id.editText4);

btnEncrypt.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
try {
// Generate a temporary key. In practice, you would save this key.
// See also Encrypting with DES Using a Pass Phrase.
String key="Helloooooo";
Log.d("prk","step0");

DesEncrypter a= new DesEncrypter();
String a1=txtOrg.getText().toString();
Log.d("prk",a1);
// Encrypt
encrypted = a.encrypt(a1.getBytes("UTF8"));
String value1=new String(encrypted);
txtEncr.setText(encrypted.toString());
txtEncr1.setText(value1.toString());
Log.d("prk","step2");
// Decrypt

} catch (Exception e)
{
Log.d("Exception ",e.toString());

}
}
});


btnDecrypt.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
try {
DesEncrypter a= new DesEncrypter();
//Log.d("while decrypting",encrypted );
String a1=txtEncr.getText().toString();
//byte[] decrypted = a.decrypt(a1.getBytes("UTF-8"));
byte[] decrypted = a.decrypt(encrypted);

String value=new String(decrypted);
Log.d("Decrypted Test",value);
txtDecr.setText(value);
// Decrypt

byte[] decrypted1 = a.decrypt(a1.getBytes("UTF-8"));
//byte[] decrypted = a.decrypt(encrypted);

String value1=new String(decrypted1);

Log.d("Loaded Decrypted=",value1);
FileOutputStream fout=openFileOutput("textFile.txt",MODE_WORLD_READABLE);
//OutputStreamWriter osw=new OutputStreamWriter(fout);
//write the string to the file
fout.write(encrypted);
fout.flush();
fout.close();

} catch (Exception e)
{
Log.d("Exception ",e.toString());

}
}
});
} 
}

DesEncrypter:

public class DesEncrypter {

private static final String ALGO="AES";
private static final String a="TheBestSecretKey";
private static final byte[] keyValue=a.getBytes();

public byte[] encrypt(byte[] bs) throws Exception{

byte[] key={'h','e','l','l','o','o','o','o','h','e','l','l','o','o','o','o'};
SecretKeySpec skeyspec=new SecretKeySpec(key,"AES");
Log.d("Encrypted Key=  ",key+"");
Cipher c=Cipher.getInstance("AES/ECB/PKCS7Padding");
c.init(Cipher.ENCRYPT_MODE,skeyspec);
byte[] encVal=bs;
Log.d("Encrypted",encVal.toString());
return c.doFinal(encVal);
}

public byte[] decrypt(byte[] encryptedData) throws Exception{
byte[] key={'h','e','l','l','o','o','o','o','h','e','l','l','o','o','o','o'};
Log.d("Decrypted Key=  ",key+"");
SecretKeySpec skeyspec=new SecretKeySpec(key,"AES");
Cipher c=Cipher.getInstance("AES/ECB/PKCS7Padding");
c.init(Cipher.DECRYPT_MODE,skeyspec);
Log.d("Inside Decryption 2 ",encryptedData+"");
byte[] decValue=c.doFinal(encryptedData);
return decValue;
}
}

Encryption works and when i use

DesEncrypter a= new DesEncrypter();
String a1=txtEncr.getText().toString();
byte[] decrypted = a.decrypt(encrypted);

This portion works too. but when i use

byte[] decrypted = a.decrypt(*a1.getBytes("UTF8")*);

and use this value to decrypt it, gives me an error "last block incomplete in decryption". I think there is an issue while converting byte to string and vice versa.

I'm desperate and need your help. What i want is to store encrypted text to be place in text box or in file and then later use it to decrypt the text.

Thanking you in advance

caitriona
  • 8,569
  • 4
  • 32
  • 36
roshan
  • 13
  • 3

2 Answers2

0

No need to be desperate, You can check as following:

txtEncr.setText(encrypted.toString());

then

byte[] txtEncrBytes = txtEncr.getText().toString().getBytes();

By comparing encrypted and txtEncrBytes using

Arrays.equals(encrypted,txtEncyBytes);

You will find the two bytes array is not equal.

The reason is txtEncr's text is encrypted's toString(), Your code invokes toString on an array, which will generate a fairly useless result such as [C@16f0472. You should at least using Arrays.toString to convert the array into a readable String that gives the contents of the array. See Programming Puzzlers, chapter 3, puzzle 12.

BUT BE WARE: The encrypted bytes array generally contains byte values that do not correspond to a character in the default platform encoding. In that case, when you create the String, the byte will be translated to the "replacement character", U+FFFD (�), and the correct value will be irretrievably lost. see this.

So the Answer is: JUST KEEP your code as its original version:

DesEncrypter a= new DesEncrypter();
String a1=txtEncr.getText().toString();
byte[] decrypted = a.decrypt(encrypted);

In a word, you should always keep the the encrypted bytes array, instead of saving it using a temp String then retrieving bytes array from the temp String.

PS: I think you may need to format your post, your code list is looking bad.

Community
  • 1
  • 1
Song
  • 157
  • 5
  • Thank you for your reply and your suggestion is highly accepted. But my requirement is still not fullfilled. I want to keep encrypted data either in file storage or in database. Is there a way to save byte array in its original form to external storage? – roshan Jun 19 '12 at 05:10
  • Maybe you can write the encrypted data to file using OutputStream which open a file on the sdcard. Makesure you declare the android.permission.WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml – Song Jun 21 '12 at 07:54
0

The simplest way to store and transport your byte array of encrypted data would be to convert it to Base64:

get encryptedBytes from your encryption method. Then:

String storeThis = Base64.encode(encryptedBytes, Base64.DEFAULT);

then, when you want to get your bytes back to decrypt:

byte[] encryptedBytes = Base64.decode(storeThis, Base64.DEFAULT);

pass encryptedBytes to your decrypt method.

J.B
  • 123
  • 1
  • 7