Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:54:37+00:00 2026-06-14T02:54:37+00:00

I am a novice android programmer that needs assistance encrypting and decrypting EditText strings

  • 0

I am a novice android programmer that needs assistance encrypting and decrypting EditText strings stored in SharedPreferences. This seems as if it should be a very common procedure and should have many tutorials covering how to accomplish this but I have not found very good information or guidance. I followed the instructions given HERE but my data shows up in clear text when checking my xml file. Any help will be greatly appreciated.

Class receives EditText string and encodes:

private SharedPreferences sp;


Intent i;
Button regBttn,rtnBttn;
EditText rName,rPwd;
String user, pass, chk;
String stat="a";
String key = "N@!an@jajpn!==";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{


     super.onCreate(savedInstanceState);
     setContentView(R.layout.register);

    rName=(EditText)findViewById(R.id.reg_uname);
    rPwd=(EditText)findViewById(R.id.reg_pswd);
    regBttn=(Button)findViewById(R.id.reg_button);
    rtnBttn=(Button)findViewById(R.id.rtn_button); 
    regBttn.setOnClickListener(this);
    rtnBttn.setOnClickListener(this);

    sp=this.getSharedPreferences("AccessApp", MODE_WORLD_READABLE);
    chk=sp.getString("USERNAME_KEY", "");
    if(chk.length()!=0){
    sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE); 

    i=new Intent(this,AccessApp.class);
    startActivity(i); 

    }     
   }

public void onClick(View arg0) {
    user=rName.getText().toString().trim();
    pass=rPwd.getText().toString().trim();

    if(arg0==regBttn){     
       if((user.length()!=0))
        {
         if((pass.length()!=0))
            {

        sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE);
        Editor myEditor=sp.edit();

        try {

            byte[ ] superSecretKeyBytes = Base64.decode(user);
            byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
            for (int i = 0; i < superSecretKeyBytes.length && i < key.length; i++) {
                key[i] = superSecretKeyBytes[i];
                myEditor.putString("USERNAME_KEY", user);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        try {

            byte[ ] superSecretKeyBytes = Base64.decode(pass);
            byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
            for (int i = 0; i < superSecretKeyBytes.length && i < key.length; i++) {
                key[i] = superSecretKeyBytes[i];
                myEditor.putString("PASSWORD_KEY", pass);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        myEditor.commit();
        Toast.makeText(this, "Registration is successfull",10000).show();
        i=new Intent(this,AccessApp.class);
        startActivity(i);
        }
        else
         {
          Toast.makeText(this, "Please Enter password", 10000).show();  
         }
         }
        else{
            Toast.makeText(this,"Please Enter Username",10000).show();
         }
        }

    else if(arg0==rtnBttn){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
         builder.setTitle("Exit");
         builder.setMessage("Do you want to exit");
         builder.setCancelable(false);
         builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int which) {
  // TODO Auto-generated method stub
  finish();
  }
  });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {
               arg0.cancel();
            }
        });
    AlertDialog alert=builder.create();
    alert.show();

    }
}
public String encrypt(String toencrypt, byte key[]) throws Exception {
    SecretKeySpec secret = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] encryptedbytes = cipher.doFinal(toencrypt.getBytes());
    String encrypted = Base64.encodeBytes(encryptedbytes, 0);
    return encrypted;

}

}

Class used to decrypt and compare:

public class AccessApp extends Activity implements OnClickListener {
private SharedPreferences sp;
String user,pass;
Button lBttn,cBttn;
EditText uname,pword;
Intent i;

int flag=0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{ 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lBttn=(Button)findViewById(R.id.login_button);
    cBttn=(Button)findViewById(R.id.cancel_button);
    uname=(EditText)findViewById(R.id.username);
    pword=(EditText)findViewById(R.id.password);

    lBttn.setOnClickListener(this);
    cBttn.setOnClickListener(this);
}
public void onClick(View arg0) {

    sp=this.getSharedPreferences("AccessApp", MODE_WORLD_READABLE);
    user = sp.getString("USERNAME_KEY", "");
    pass = sp.getString("PASSWORD_KEY", "");




   if(lBttn.equals(arg0)){

      if((uname.getText().toString().equals(user))&& 
        (pword.getText().toString().equals(pass)))

            {
          Toast.makeText(this, "You are Logged In", 20000).show();

               Intent intent;
               intent=new Intent(this,details.class);
               startActivity(intent);
              flag=1;
            }

        else 
           {
            Toast.makeText(this, "Wrong Username or Password",20000).show();
            flag=0;   
           }       
        } 
        else if(cBttn==arg0){
            AlertDialog.Builder builder=new AlertDialog.Builder(this);
          builder.setTitle("Exit");
         builder.setMessage("Do you want to exit");
    builder.setCancelable(false);
    builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

 public void onClick(DialogInterface dialog, int which) {
 // TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

 finish();
 }
 });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {
               arg0.cancel();
            }
        });
    AlertDialog alert=builder.create();
    alert.show();

        }

    }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

     Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
     finish();
 }
 return super.onKeyDown(keyCode, event);
}

public static String decrypt(String encryptedText, byte[ ] key) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] toDecrypt = Base64.decode(encryptedText);
    byte[] encrypted = cipher.doFinal(toDecrypt);
    return new String(encrypted);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:54:38+00:00Added an answer on June 14, 2026 at 2:54 am

    You seem to be saving the unencrypted username and password to the editor:

            byte[ ] superSecretKeyBytes = Base64.decode(pass);  
            byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
            for (int i = 0; i < superSecretKeyBytes.length && i < key.length; i++) {
                key[i] = superSecretKeyBytes[i];
                myEditor.putString("PASSWORD_KEY", pass);
            }
    

    To be completely honest, I don’t see why you’re doing ANY of that. What you’re actually doing seems really unnecessary, since you’re decoding it into a byte array, creating a fixed-amount byte array, looping through the decoded array, and replacing the created fixed-amount array’s data with the decoded one, and then adding the un-changed String password to the editor (which you’re doing far too many times, also, btw). And I’m pretty sure that’s not what you’re trying to do.

    I believe what you’re trying to do there is actually encrypt it, but you never got around to it, so let’s try this:

    First, your key should be a static variable as if that changes, it will no longer be able to encrypt/decrypt appropriately.

    byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
    

    Now to encrypt the password, based on what you already have, call your encrypt method based on your secret key (in this case: key) and the String you want to encrypt (in this case: pass):

    String encryptedPassword = encrypt(pass, key);
    

    Now encryptedPassword contains the Base64 encrypted form of your password based on the key you entered. Now all that’s left to do is save the encrypted form of your password with the editor, instead of the unencrypted form like you were doing previously.

    myEditor.putString("PASSWORD_KEY", encryptedPassword);
    

    So now when you call myEditor.commit(), you’ll store the encrypted form.

    Here’s the edited onClick method from the first source file for both username and password encryption. I didn’t look at your decryption part, but you should be able to figure it out based on what I gave you:

    public void onClick(View arg0) {
        user=rName.getText().toString().trim();
        pass=rPwd.getText().toString().trim();
    
        if(arg0==regBttn){     
           if((user.length()!=0))
            {
              if((pass.length()!=0))
            {
    
            sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE);
            Editor myEditor=sp.edit();
    
            byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
    
            try {
                 String encryptedUser = encrypt(user, key);  
                 myEditor.putString("USERNAME_KEY", encryptedUser); 
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        try {
                 String encryptedPass = encrypt(pass, key);  
                 myEditor.putString("PASSWORD_KEY", encryptedPass); 
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        myEditor.commit();
        Toast.makeText(this, "Registration is successfull",10000).show();
        i=new Intent(this,AccessApp.class);
        startActivity(i);
        }
        else
         {
          Toast.makeText(this, "Please Enter password", 10000).show();  
         }
         }
        else{
            Toast.makeText(this,"Please Enter Username",10000).show();
         }
        }
    
    else if(arg0==rtnBttn){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
         builder.setTitle("Exit");
         builder.setMessage("Do you want to exit");
         builder.setCancelable(false);
         builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
    
      public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      finish();
      }
      });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface arg0, int arg1) {
                    arg0.cancel();
                }
            });
        AlertDialog alert=builder.create();
        alert.show();
    
        }
    }
    

    Hope it helps~

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a novice programmer and I'm trying to learn Android coding using Eclipse. This
I'm a novice Android programmer, and to help me understand how Intents really work,
Hi I'm a real novice at android and really getting desperate at this apparently
I'm a novice programmer so feel free to comment on things that make no
I know that there is code like this: http://www.java2s.com/Code/Android/Security/RSAencryptdecryptfunctionRSAECBPKCS1Padding.htm /* Copyright (c) 2010, Sungjin
First of all I'm a novice to android, so this could probably be a
I notice that some of the base applications in Android are using this little
I'm a novice Android monkey and I've hit a snag trying to implement a
Php novice. 1.Is there anything wrong with this PHP & MySQL code? include_once db_login.php
I am developing an Android application. I am novice in Touch enabled Android Application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.