Illegal base64 character 20

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 20
Error while descrypt GetMEssage()  : Illegal base64 character 20

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);
    }
}

mehulkatara's picture
Joined: Feb 19 2018 - 12:38am
Last seen: 7 months 1 week ago

0 Answers:

No answers yet
Feedback
randomness