简体   繁体   English

找不到 Base64 apache.commons .encodeBase64 符号

[英]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.我正在编写一个加密类,在发送 TCP 数据之前/之后使用密钥加密/解密数据。 I am having a problem getting org.apache.commons.codec.binary.Base64 to work on my system.我在让org.apache.commons.codec.binary.Base64在我的系统上工作时遇到问题。 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.在大多数情况下,我可以看到人们将其与 android studio 相关联,但是,我使用的是 notepad++ 和命令行,但仍然遇到问题。

I have added commons-codec-1.10.jar to my project directory.我已将commons-codec-1.10.jar添加到我的项目目录中。 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;
    }    

The new keyword expects a type to be created. new关键字期望创建一个类型。 As the little caret points out, there should be brackets () behind Base64 .正如小插号指出的那样, Base64后面应该有括号()

Yet, Base64 is a collection of static methods, so you are done if you just drop the new in this case.然而, Base64static方法的集合,所以如果你在这种情况下删除new ,你就完成了。

String encStr = Base64.encodeBase64String(out);

should do the trick.应该做的伎俩。

For Android developers:对于安卓开发者:

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

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

make sure you import, android.util.Base64确保你导入, android.util.Base64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java - 方法 encodeBase64 - Java - The method encodeBase64 Base64编码和解码Apache公用 - Base64 encoding and decoding apache commons JAVA中的EncodeBase64返回错误的输出 - EncodeBase64 in JAVA return a wrong output java.lang.NoMethodError:org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString - java.lang.NoMethodError : org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64.encodeBase64String - java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String 没有 static 方法 encodeBase64String([B)Ljava/lang/String; 在 class Lorg/apache/commons/codec/binary/Base64 中; 或其超类 - No static method encodeBase64String([B)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Base64; or its super classes 导入 apache 通用编解码器时无法解析符号“Base64” - Cannot resolve symbol “Base64” while importing apache commons codec Java EE应用程序中的java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64.encodeBase64String() - java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String() in Java EE application BouncyCastle和Apache Commons Codec Base64编码之间的区别 - Difference between BouncyCastle and Apache Commons Codec Base64 encoding Apache通用base64解码和Sun base64解码 - Apache commons base64 decode and Sun base64 decode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM