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

  • Home
  • SEARCH
  • 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 6195727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:31:04+00:00 2026-05-24T03:31:04+00:00

I’m a newbie to android dev, right now I implemented an AES on Android

  • 0

I’m a newbie to android dev, right now I implemented an AES on Android and it could encrypt strings with user input passwords. The encryption seems fine and it could omit Base64/Hex encoded strings.

But When I try to decrypt it, the problem comes: With the decryption, the omission always showing me a load of messy characters.

In order to get rid of it, I’ve tried debugging it by define a charset (Like UTF-8) while casting from string to byte[], but no hit, and also tried to encode the omit with base 64 or Hex, but both of them failed.

I’ve also tried to define AES/CBC/PKCS5Padding or just AES while using cipher.getInstance method, but still no go.

It is quite irritating, could you guys help me please?

Forgot to mention that I once asked a similar question https://stackoverflow.com/questions/6727255/aes-decryption-on-android-not-correct , the grammer problem there have been corrected.

And Here’s the code:

For Encryption

public String AESEncrypt(String sKey, String PlainMsg)
                throws Exception {
            //Try use some Android based alert dialog to catch this exception.
            if (sKey == null) {
                Log.e("SecureChat", "IllegalArgumentException Catched");
                throw new IllegalArgumentException ("NULL Secret NOT ALLOWED!");
            }           
            /*Old Method
            //byte[] rawKey = getRawKey(sKey.getBytes("UTF-8"));
            byte[] rawKey = getRawKey(sKey.getBytes());
            //Encrypt start
            SecretKeySpec keySpec = new SecretKeySpec(rawKey, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            //byte[] cipherText = cipher.doFinal(PlainMsg.getBytes("UTF-8"));
            byte[] cipherText = cipher.doFinal(PlainMsg.getBytes());
            return Base64Encoded(cipherText);
            */
            //New Method
            byte[] salt = getSalt();
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
            KeySpec spec = new PBEKeySpec(sKey.toCharArray(), salt, 1024, 256); 
            SecretKey tmp = factory.generateSecret(spec);
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
            //Encryption Process
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secret);
            byte[] cipherText = cipher.doFinal(PlainMsg.getBytes());
            //return Base64Encoded(cipherText);
            //Hex
            return toHex(cipherText);
        }

For Decryption

public String AESDecrypt(String sKey, String EncryptMsg)
                throws Exception {          
            /*Old Method
            //byte[] rawKey = getRawKey(sKey.getBytes("UTF-8"));
            byte[] rawKey = getRawKey(sKey.getBytes());
            SecretKeySpec keySpec = new SecretKeySpec(rawKey, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            //byte[] plainText = Base64Decoded(EncryptMsg.getBytes("UTF-8"));
            byte[] plainText = Base64Decoded(EncryptMsg);           
            cipher.doFinal(plainText);
            return new String(plainText, "UTF-8");
            */
            //New Method
            byte[] salt = getSalt();
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
            KeySpec spec = new PBEKeySpec(sKey.toCharArray(), salt, 1024, 256); 
            SecretKey tmp = factory.generateSecret(spec);
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
            //byte[] bCipherText = Base64Decoded(EncryptMsg);
            //Hex
            byte[] bCipherText = toByte(EncryptMsg);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secret);
            cipher.doFinal(bCipherText);
            return new String(bCipherText);
        }

        private byte[] getSalt() throws NoSuchAlgorithmException {
            /*Mark for old key method
            //Initialize the KeyGenerator
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            sr.setSeed(seed);
            //Init for 256bit AES key
            kgen.init(Constants.AES_KEY_SIZE, sr);;
            SecretKey secret = kgen.generateKey();
            //Get secret raw key
            byte[] rawKey = secret.getEncoded();
            return rawKey;
            */

            //New key method with some salt
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            byte[] ransalt = new byte[20];
            random.nextBytes(ransalt);
            return ransalt;
        }

        @Override
        public byte[] getRawKey(byte[] seed) throws Exception {
            /*Old Method
            //Initialize the KeyGenerator
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            sr.setSeed(seed);
            //Init for 256bit AES key
            kgen.init(Constants.AES_KEY_SIZE, sr);
            SecretKey secret = kgen.generateKey();
            //Get secret raw key
            byte[] rawKey = secret.getEncoded();
            return rawKey;
            */
            return null;
        }
/**
 * 
 * @param toBeDecoded
 * @return
 */
        public byte[] Base64Decoded(String toBeDecoded) {
            byte[] decoded = Base64.decode(toBeDecoded, 0);
            return decoded;
        }

        //Hex Mode
        public String toHex(String txt) {
            return toHex(txt.getBytes());
        }
        public String fromHex(String hex) {
            return new String(toByte(hex));
        }

        public byte[] toByte(String hexString) {
            int len = hexString.length()/2;
            byte[] result = new byte[len];
            for (int i = 0; i < len; i++)
                result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
            return result;
        }

        public String toHex(byte[] buf) {
            if (buf == null)
                return "";
            StringBuffer result = new StringBuffer(2*buf.length);
            for (int i = 0; i < buf.length; i++) {
                appendHex(result, buf[i]);
            }
            return result.toString();
        }
        private final String HEX = "0123456789ABCDEF";
        private void appendHex(StringBuffer sb, byte b) {
            sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
        }


    }

I’ve referenced/compared with these code on Stackoverflow:
Android aes encryption pad block corrupted
and
incorrect decryption using AES/CBC/PKCS5Padding in Android

Seems that my problem lies on the charset encoding, but I cannot find out where the problem is.

Any comments/answers are very appreciated!
Thank you for helping me!

  • 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-05-24T03:31:05+00:00Added an answer on May 24, 2026 at 3:31 am

    This code I wrote works flawlessly. Look at this link below:

    http://pocket-for-android.1047292.n5.nabble.com/Encryption-method-and-reading-the-Dropbox-backup-td4344194.html#a4454327

    Without looking too carefully at your code I would suggest you should specify the encoding here though I’m not sure if this is the cause of your problems:

    byte[] cipherText = cipher.doFinal(PlainMsg.getBytes());
    

    and here:

    return new String(bCipherText);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.