Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password protecting my android app (the simple way)

I've built my first app, and I would like to password protect it. It's fine for me to store the password in the Java files and the method needs to be as simple as possible because i have no experience of java or even xml before this app. I've had a few attempts and failed so I was hoping someone can help me out.

I've created the layout with an EditText field:

<EditText
 android:id="@+id/passwordedittext"
 android:layout_width="200dp"
 android:layout_height="50dp"
 android:inputType="textPassword"
 android:layout_marginTop="40dp"
 android:layout_marginLeft="20dp">
 <requestFocus />

and a submit button:

<Button
 android:id="@+id/submitbutton"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_marginTop="40dp"
 android:background="@drawable/bgo"
 android:clickable="true" 
 android:layout_gravity="right|center_horizontal" 
 android:layout_marginRight="20dp"/>

The Java file:

package com.berry;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;


public class password extends Activity{

MediaPlayer mpbuttonclick;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    setContentView(R.layout.password);

    mpbuttonclick = MediaPlayer.create(this, R.raw.keypress);

    Button sumbitButton = (Button) findViewById(R.id.submitbutton);
    sumbitButton.setOnClickListener(new View.OnClickListener() {        
        public void onClick(View v){
        EditText passwordEditText = (EditText) findViewById(R.id.passwordedittext);
                    if(passwordEditText.getText().toString()=="MyPasswordHere"){
                        startActivity(new Intent("com.berry.intro"));
                        mpbuttonclick.start();


                    }}});
    }}
like image 497
SuperKombol Avatar asked Feb 19 '12 15:02

SuperKombol


2 Answers

This part:

if(passwordEditText.getText().toString()=="MyPasswordHere")

is incorrect. It should be

if(passwordEditText.getText().toString().equals("MyPasswordHere"))

When comparing primitive data types (like int, char, boolean) you can use ==, !=, etc.
When comparing objects (like String, Car, etc) you need to use the .equals() method.

See also this page.

like image 182
nhaarman Avatar answered Nov 08 '22 19:11

nhaarman


It is in no way safe to check your password like that.

There are several ways to easily bypass your code

  1. Calling the activity directly from another App

  2. Reading the disassembled smali code to retrieve the password

  3. Modifying the code using smali to always jump into the codeblock

Solutions available to solve these problems:

  1. Obscure your code (Worst option, but might be enough in most cases)

  2. Comparing the Hashed Password: Much more secure. But should be a salted Hash. (There is also a more simple to understand explaination for the implementation)

  3. Use a HTTP Request to a server of yours to hide the mechanism behind the password check. (But that requires your app to ask for Networking Permissions)

like image 30
devsnd Avatar answered Nov 08 '22 20:11

devsnd