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 8645869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:39:41+00:00 2026-06-12T12:39:41+00:00

I am trying to encode my sharedPreferences username and password using Base64 but when

  • 0

I am trying to encode my sharedPreferences username and password using Base64 but when running my program in eclipse emulator, I receive and error message saying “Array out of bounds”. I’m not sure what this error means and how to correct it.

Logcat:

10-06 13:51:24.005: E/AndroidRuntime(4272): FATAL EXCEPTION: main
10-06 13:51:24.005: E/AndroidRuntime(4272): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
10-06 13:51:24.005: E/AndroidRuntime(4272):     at com.SharedPreferences.Login.SharedPrefLoginActivity.onClick(SharedPrefLoginActivity.java:84)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at android.view.View.performClick(View.java:3511)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at android.view.View$PerformClick.run(View.java:14105)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at android.os.Handler.handleCallback(Handler.java:605)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at android.os.Looper.loop(Looper.java:137)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at java.lang.reflect.Method.invokeNative(Native Method)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at java.lang.reflect.Method.invoke(Method.java:511)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-06 13:51:24.005: E/AndroidRuntime(4272):     at dalvik.system.NativeStart.main(Native Method)

Here is my Registration activity which should take EditText string, encode, and save to sharedpreferences xml.

 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 {
            myEditor.putString("USERNAME_KEY", user);
            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 < pass.length(); i++) {
                key[i] = superSecretKeyBytes[i];
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        try {
            myEditor.putString("PASSWORD_KEY", pass);
            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 < pass.length(); i++) {
                key[i] = superSecretKeyBytes[i];
            }
        } 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);
    return encrypted;

}
}

Corrections:

  The below corrections were applied.
 (int i = 0; i < superSecretKeyBytes.length && i < key.length; i++)

 Issue:
  Array error has cleared but EditText string is still saved in clear text.?
  • 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-12T12:39:42+00:00Added an answer on June 12, 2026 at 12:39 pm

    Why do you use pass length in cycle condition when it is not used inside the cycle? Are you sure that superSecretKeyBytes.length == pass.length()? (probably not equals, you are decoding from Base64)

    It is safer to write this:

    for (int i = 0; i < superSecretKeyBytes.length; i++) {
        key[i] = superSecretKeyBytes[i];
    }
    

    instead of:

    for (int i = 0; i < pass.length(); i++) {
        key[i] = superSecretKeyBytes[i];
    }
    

    Also, where are any guarantees that you wouldn’t get OutOfBoundsException for your key array? Prefer to check this in the cycle as well:

    for (int i = 0; i < superSecretKeyBytes.length && i < key.length; i++) {
        key[i] = superSecretKeyBytes[i];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to encode and decode a base64 string. It works fine normally, but
I'm trying to encode a simple hierarchy in my records, but I'm running into
I'm trying to encode an image using base64 in Node.JS to pass along to
I am trying to encode a stream using x264 (by feeding individual images), but
I am trying to encode an image by using the below code, but the
I am trying to encode a list as JSON, compress it using gzip in
I'm trying to encode a URL using the HttpUtility.UrlEncode() method, why am I getting
I'm trying to encode some denotational semantics into Agda based on a program I
I'm trying to encode a QBF in smt-lib 2 syntax for z3. Running z3
I'm trying to convert string using Var encode:ansistring; begin encode:=UTF8Encode('اختبار'); showmessage(encode); end; It's working

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.