asad_hussain
asad_hussain

Reputation: 2001

Encrypting the message but not able to decrypt in android

I am having three activities.In MainActivity,I have two buttons which allow me to choose from two options I.e click on encrypt to go toEncryptActivity.java and click on decrypt to go on DecryptActivity.java.

In first activity I.e. EncryptActivity,I am encrypting the message written by the user in the EditText. Then I press back button to go to main menu and then I click on decrypt button to enter into second activity DecryptActivity.java to decrypt the message and print the message in a textview.

But the problem is when I click on decrypt button in DecryptActivity,nothing gets printed in the Textview.

EncryptActivity.java:

public class EncryptActivity extends AppCompatActivity {

EditText ed1;
Button b1;

private ClipboardManager myClipboard;
private ClipData myClip;
KeyGenerator keygenerator;
static SecretKey secretkey;

Cipher cipher;
byte[] encrypted;
String encryptedText = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.encrypt_activity);
    ed1 = (EditText) findViewById(R.id.editText);
    b1 = (Button) findViewById(R.id.button);
    myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = ed1.getText().toString();//now encrypt the message
            try {
                keygenerator = KeyGenerator.getInstance("Blowfish");
                secretkey = keygenerator.generateKey();
                Log.d("asd", secretkey.toString());
                cipher = Cipher.getInstance("Blowfish");
                cipher.init(Cipher.ENCRYPT_MODE, secretkey);
                encrypted = cipher.doFinal(text.getBytes());
                encryptedText = encrypted.toString();
                myClip = ClipData.newPlainText("text", encryptedText);
                myClipboard.setPrimaryClip(myClip);

                Toast.makeText(getApplicationContext(), "Text Copied", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
            }
        }
    });
}

}

DecryptActivity.java:

public class DecryptActivity extends AppCompatActivity {

EditText ed1;
TextView tv;
Button b1,b2;
byte[] decrypted;
String decryptedText = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.decrypt_activity);
    ed1 = (EditText) findViewById(R.id.decrypteditText);
    b1 = (Button) findViewById(R.id.decryptbutton);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("asd","inside onclick");
            try {
                Log.d("asd","inside try of decryptActivity");
                KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
                SecretKey secretkey = keygenerator.generateKey();
                Cipher cipher2 = Cipher.getInstance("Blowfish");
                  cipher2.init(Cipher.DECRYPT_MODE,secretkey);
                Log.d("asd","in decrypt key is"+secretkey.toString());
                Log.d("asd","before getBytes");
                byte[] encrypted =ed1.getText().toString().getBytes();
                Log.d("asd",ed1.getText().toString());
                decrypted = cipher2.doFinal(encrypted);
                tv=(EditText)findViewById((R.id.textView));
                tv.setText("Asd");
                Log.d("asd",new String(decrypted));
            } catch (Exception e) {
            }
            Toast.makeText(getApplicationContext(), "Text Pasted", Toast.LENGTH_SHORT).show();
        }
    });
}

}

Upvotes: 0

Views: 203

Answers (1)

Robert
Robert

Reputation: 42595

When using a symmetric krypto algorithm like Blowfish it is essential that the encryption and the decryption have to use the same key - otherwise the decryption will fail.

In your case you generate a random key before encrypting and again before decrypting:

 KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
 SecretKey secretkey = keygenerator.generateKey();

Therefore you are trying to decrypt with a different key! By definition this can not work...

For encrypting you can generate a new key but then you have to save it and use it again for decrypting.

Additionally you make the mistake that a byte[] can not be converted to a String by calling it's toString() method. Convert it to a hex or base64 string otherwise it will never work.

Upvotes: 1

Related Questions