javaencodingbase64apache-commons-codec

Base64 apache.commons .encodeBase64 symbol not found


I am working on writing an encryption class to encrypt/decrypt data with a key before/after sending TCP data. I am having a problem getting org.apache.commons.codec.binary.Base64 to work on my system. In most cases, I can see people relating this to android studio, however, I'm using notepad++ and the command line and am still having problems.

I have added commons-codec-1.10.jar to my project directory. I run at the command line:

javac -cp .;commons-codec-1.10.jar Server.java ... CryptoUtil.java 

I have this at the top

import org.apache.commons.codec.binary.Base64;

My error is:

CryptoUtil.java:60: error: cannot find symbol
               String encStr = new Base64.encodeBase64String(out);
                                         ^
    symbol: class encodeBase64String
    location: class Base64

CryptoUtil.java:87: error: cannot find symbol
                byte[] enc = new Base64.decodeBase64(encryptedText);
                                       ^
     symbol: class decodeBase64
     location: class Base64
2 errors

And my enclosing functions:

    public String encrypt(String secretKey, String plainText) 
            throws NoSuchAlgorithmException, 
            InvalidKeySpecException, 
            NoSuchPaddingException, 
            InvalidKeyException,
            InvalidAlgorithmParameterException, 
            UnsupportedEncodingException, 
            IllegalBlockSizeException, 
            BadPaddingException{
        //Key generation for enc and desc
        KeySpec keySpec = new PBEKeySpec(secretKey.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);        
         // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

        //Enc process
        ecipher = Cipher.getInstance(key.getAlgorithm());
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);      
        String charSet="UTF-8";       
        byte[] in = plainText.getBytes(charSet);
        byte[] out = ecipher.doFinal(in);
        String encStr = new Base64.encodeBase64String(out);

        return encStr;
    }

     public String decrypt(String secretKey, String encryptedText)
     throws NoSuchAlgorithmException, 
            InvalidKeySpecException, 
            NoSuchPaddingException, 
            InvalidKeyException,
            InvalidAlgorithmParameterException, 
            UnsupportedEncodingException, 
            IllegalBlockSizeException, 
            BadPaddingException, 
            IOException{
         //Key generation for enc and desc
        KeySpec keySpec = new PBEKeySpec(secretKey.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);        
         // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
        //Decryption process; same key will be used for decr
        dcipher=Cipher.getInstance(key.getAlgorithm());
        dcipher.init(Cipher.DECRYPT_MODE, key,paramSpec);
        byte[] enc = new Base64.decodeBase64(encryptedText);
        byte[] utf8 = dcipher.doFinal(enc);
        String charSet="UTF-8";     
        String plainStr = new String(utf8, charSet);
        return plainStr;
    }    

Solution

  • The new keyword expects a type to be created. As the little caret points out, there should be brackets () behind Base64.

    Yet, Base64 is a collection of static methods, so you are done if you just drop the new in this case.

    String encStr = Base64.encodeBase64String(out);
    

    should do the trick.