【问题标题】:Base64 apache.commons .encodeBase64 symbol not foundBase64 apache.commons .encodeBase64 符号未找到
【发布时间】:2020-01-25 13:05:36
【问题描述】:

我正在编写一个加密类,以在发送 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;
    }    

【问题讨论】:

    标签: java encoding base64 apache-commons-codec


    【解决方案1】:

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

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

    String encStr = Base64.encodeBase64String(out);
    

    应该可以解决问题。

    【讨论】:

    • 谢谢。作品。男孩,尽管非常简单,但我什至还没有弄清楚:(
    • @Dima:我怀疑这一点,但到目前为止还没有使用过这个公共类。我想在Base64 文档中查找以确认。
    【解决方案2】:

    对于 Android 开发者:

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

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

    确保您导入,android.util.Base64

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 2014-01-09
      • 1970-01-01
      • 2012-04-26
      • 2021-12-17
      • 2021-07-18
      • 2014-08-31
      • 2018-10-21
      • 2021-01-06
      相关资源
      最近更新 更多