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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:09:51+00:00 2026-06-14T21:09:51+00:00

I am trying to encrypt editText strings using Base64 wnich will be saved in

  • 0

I am trying to encrypt editText strings using Base64 wnich will be saved in sharedpreferences but once the strings are encoded and saved my .xml file is blank. I am a novice programmer and very new to encrytion so Im lost and do not know how to properly analyze my code to determine the problem which may be very simple. I have received some suggestion but nothing that clearly shows where the issue is, why its not working, and how to resolve it. I suspect my encryption method but not sure where. This must be a common task for coders who are trying to protect username and password entries. This tutorial was the used for this encryption project Click Here

Here is class for encoding, encrypting, and saving EditText String:

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 (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
    try {
             String encryptedPass = encrypt(pass, key);  
             myEditor.putString("PASSWORD_KEY", encryptedPass); 

    } catch (Exception 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;

}

}

Logcat:

 11-11 23:41:59.873: W/System.err(11404): java.security.InvalidKeyException: Key length not 128/192/256 bits.
 11-11 23:41:59.873: W/System.err(11404):   at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineInit(JCEBlockCipher.java:570)
 11-11 23:41:59.894: W/System.err(11404):   at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineInit(JCEBlockCipher.java:617)
 11-11 23:41:59.903: W/System.err(11404):   at javax.crypto.Cipher.init(Cipher.java:519)
 11-11 23:41:59.928: W/System.err(11404):   at javax.crypto.Cipher.init(Cipher.java:479)
 11-11 23:41:59.943: W/System.err(11404):   at com.SharedPreferences.Login.SharedPrefLoginActivity.encrypt(SharedPrefLoginActivity.java:139)
 11-11 23:41:59.953: W/System.err(11404):   at com.SharedPreferences.Login.SharedPrefLoginActivity.onClick(SharedPrefLoginActivity.java:82)
 11-11 23:41:59.953: W/System.err(11404):   at android.view.View.performClick(View.java:3511)
 11-11 23:41:59.953: W/System.err(11404):   at android.view.View$PerformClick.run(View.java:14105)
 11-11 23:41:59.973: W/System.err(11404):   at android.os.Handler.handleCallback(Handler.java:605)
 11-11 23:41:59.973: W/System.err(11404):   at android.os.Handler.dispatchMessage(Handler.java:92)
 11-11 23:42:00.034: W/System.err(11404):   at android.os.Looper.loop(Looper.java:137)
 11-11 23:42:00.034: W/System.err(11404):   at android.app.ActivityThread.main(ActivityThread.java:4424)
 11-11 23:42:00.034: W/System.err(11404):   at java.lang.reflect.Method.invokeNative(Native Method)
 11-11 23:42:00.103: W/System.err(11404):   at java.lang.reflect.Method.invoke(Method.java:511)
 11-11 23:42:00.103: W/System.err(11404):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 11-11 23:42:00.124: W/System.err(11404):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 11-11 23:42:00.124: W/System.err(11404):   at dalvik.system.NativeStart.main(Native Method)
  • 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-14T21:09:52+00:00Added an answer on June 14, 2026 at 9:09 pm

    You seem to have miscounted your bytes:

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

    You need a 128bit key (16 bytes), but you’ve declared 17 bytes here. Reduce the key length by one and I think it should work.

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

Sidebar

Related Questions

I'm trying to encrypt an array of 256 bytes by using RSACryptoServiceProvider but I'm
Have been trying to encrypt an xml file to a string so that I
Talking about javax.crypto.Cipher I was trying to encrypt data using Cipher.getInstance(RSA/None/NoPadding, BC) but I
I am trying to encrypt/decrypt an XML file. I found this sample for encrypting
I'm trying to encrypt a text file using Perl and then decrypt it using
I'm trying to encrypt and decrypt a file stream over a socket using RijndaelManaged,
I am trying to encrypt strings in .NET by using a RSA algorithm and
I'm trying to encrypt a file using the implementation I found here: http://msdn.microsoft.com/en-us/magazine/cc164055.aspx This
I am trying to encrypt a text file using a .pfx certificate file using
I'm trying to encrypt the contents of one file into another file using a

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.