Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode Base64 cannot find symbol error

It gives me a error when I try to compile it in terminal. It prints out this error:

-bash-4.1$ javac CPS3498/HW_Ch2/encrypt.java
CPS3498/HW_Ch2/encrypt.java:9: cannot find symbol
symbol  : class Base64
location: package java.util
import java.util.Base64;
                ^
CPS3498/HW_Ch2/encrypt.java:61: cannot find symbol
symbol  : variable Base64
location: class encrypt
        String encryptedValue = Base64.getEncoder().encodeToString(encVal);
                                ^
2 errors

I completely lost on how to fix this problem. I have tried different java utilities to compile and they all give me almost the same error.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

//create public class encrypt
public class encrypt {
    //algorithm AES 128 with a secret key
    private static final String ALGO = "AES";
    private static final byte[] keyValue = 
        new byte[] { 'H', 't', 'v', 'b', 'a', 'w', 'e',
'i', 'n', 'v', 'a','l', 't', 'k', 'y', 'e' };
    private static BufferedReader reader;

    public static void main(String[] args) throws Exception {
        //Filereader letter to read from a file letter.txt
        FileReader letter = new FileReader("/Users/Shiv/Eclipse/CPS3498_HW/src/letter.txt");
        reader = new BufferedReader(letter);
        //string text blank, data that stres reader contents.
        String text = "";
        String data = reader.readLine();
        //while loop to see if data is not blank
        while (data != null){
            text += data;
            data = reader.readLine();
        }
        String textEnc = encrypt(text);
        //        
        File secret = new File("/Users/Shiv/Eclipse/CPS3498_HW/src/secret.txt");
            try
            {
                secret.createNewFile();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }           
            try {
                FileWriter secretFile = new FileWriter(secret);
                BufferedWriter secretBuff = new BufferedWriter(secretFile);
                secretBuff.write(textEnc);
                secretBuff.close();
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
//encrypt method
public static String encrypt(String Data) throws Exception {
        Key pass = generateKey();
        // cipher class to provide the encryption and intialize
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, pass);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = Base64.getEncoder().encodeToString(encVal);
        return encryptedValue;     
    }
//generateKey method to generate a secret key
private static Key generateKey() throws Exception {
    Key pass = new SecretKeySpec(keyValue, ALGO);
    return pass;
}
}
like image 229
Shiv Patel Avatar asked Sep 26 '16 19:09

Shiv Patel


2 Answers

I had the same problem despite running on jdk1.8.0_181.

I got it to work with following code:

import java.util.Base64;

...

byte[] encodedPv = Base64.getDecoder().decode(PRIVATE_KEY);

...

Hope this helps someone! Cheers Manzn

like image 189
Manzn Avatar answered Oct 02 '22 23:10

Manzn


java.util.Base64 is available since Java 8. You are compiling the class with an older Java version. javac -version will show you which one you are using.

like image 21
Magnilex Avatar answered Oct 02 '22 22:10

Magnilex