未找到Base64 apache.commons .encodeBase64符号

时间:2016-03-13 22:29:16

标签: java encoding base64 apache-commons-codec

我正在编写一个加密类,用于在发送TCP数据之前/之后用密钥加密/解密数据。我在使用org.apache.commons.codec.binary.Base64处理我的系统时遇到问题。在大多数情况下,我可以看到有人将这与android studio相关联,但是,我使用的是notepad ++和命令行,但仍然存在问题。

我已将commons-codec-1.10.jar添加到我的项目目录中。我在命令行运行:

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

我在顶部有这个

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

我的错误是:

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

我的封闭功能:

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

2 个答案:

答案 0 :(得分:2)

new关键字需要创建一个类型。正如小插入点所指出的那样,()后面应该有Base64括号。

然而,Base64static方法的集合,所以如果你在这种情况下放弃new,你就完成了。

String encStr = Base64.encodeBase64String(out);

应该这样做。

答案 1 :(得分:0)

对于Android开发人员:

要编码: 使用Base64.encode(yourByteArray,Base64.DEFAULT)

要解码: 使用Base64.decode(stringData,Base64.DEFAULT)

确保您导入android.util.Base64