Jump to content
We've recently updated our Privacy Statement, available here ×

Illegal base64 character 20


mehulkatara

Recommended Posts

I have implemented token-based authentication with base64 encryption method.

It's working but sometimes when I pass token it reject that token because of below error

Error while decrypting !java.lang.IllegalArgumentException: Illegal base64 character 20Error while descrypt GetMEssage()  : Illegal base64 character 20[/code]

Hear the example of token

Token Pass: u=Katara Mehul

Token Fail: u=Mehul Katara

below is my java encryption mechanism

package com.jaspersoft.cipher;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Arrays;import java.util.Base64;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import com.jaspersoft.jasperserver.api.common.crypto.CipherI;public class myCipher implements CipherI{            private SecretKeySpec secretKey;    private byte[] key;    String myKey="123";                 public  void setKey(String myKey)        {                             MessageDigest sha = null;            try {                key = myKey.getBytes("UTF-8");                sha = MessageDigest.getInstance("SHA-1");                key = sha.digest(key);                key = Arrays.copyOf(key, 16);                secretKey = new SecretKeySpec(key, "AES");                            }            catch (NoSuchAlgorithmException e) {                e.printStackTrace();            }            catch (UnsupportedEncodingException e) {                e.printStackTrace();            }        }            public String decrypt(String strToDecrypt) {                try {            String secret=myKey;            setKey(secret);            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");            cipher.init(Cipher.DECRYPT_MODE, secretKey);            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt.getBytes("UTF-8"))));                    }        catch(Exception e)        {            System.out.println("Error while decrypting !"  +e.toString());            System.out.println("Error while descrypt GetMEssage()  : "  +e.getMessage());            e.printStackTrace();        }         return null;    }    public String encrypt(String strToEncrypt) {                try {            String secret=myKey;            setKey(secret);            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");            cipher.init(Cipher.ENCRYPT_MODE, secretKey);                        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));                            }        catch(Exception e)        {            System.out.println("Error while encrypting"  +e.toString());        }        return null;            }            public static void main(String args[]) {                String orginalString="u=Katara Mehul";                    myCipher obj = new myCipher();        String encryptString = obj.encrypt(orginalString);        String decryptString = obj.decrypt(encryptString);                System.out.println(orginalString);            System.out.println(encryptString);                System.out.println(decryptString);    }}[/code]

 

 

 

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...