【问题标题】:java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
【发布时间】:2014-12-02 10:11:27
【问题描述】:

请管理员不要将其标记为重复,请完整阅读我的问题。我正在加密和解密一些文本,但是在与 main 运行正常的同一文件中运行时,但是当我从外部调用它的加密和解密函数时。它在运行时给出错误。我附上代码。 包装desede;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

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

import security.SHA256Algo;
import shradhafinalwiddesign.UpdateFile;
import shradhafinalwiddesign.UserRegistration;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Simple TripleDES Encrypt/Decrypt Test 
 * sha1, utf-8, no padding
 *
 * uses commons-codec-1.6 
 * javac -cp :commons-codec-1.6.jar TripleDESTest.java
 * java -cp :commons-codec-1.6.jar TripleDESTest 
 */

public class TripleDesDemo {


    public static void main(String[] args) throws Exception {

        String text = "textToEncrypt";
        UserRegistration user = new UserRegistration() ;
        user.setlUsername("tarunv") ;
        user.setAnswer("tommysdsfdsfsd") ;
        user.setLastaccess("pets namesdfsfds") ;
        user.setLpassword("computersdfdsfd") ;

        String h1 = SHA256Algo.createHash(user.getlUsername()) ;
        String h2 = SHA256Algo.createHash(user.getLpassword()) ;
        String h3 = SHA256Algo.createHash(user.getAnswer()) ;

        String hash1 = UpdateFile.modifyHashValue(h1).substring(0, 24) ;
        String hash2 = UpdateFile.modifyHashValue(h2) ;
        String hash3 = UpdateFile.modifyHashValue(h3) ;

        System.out.println("    key1 : "+hash1.length()+"    key2 : "+hash2.length()+"   key3 : "+hash3.length());
        byte[] arr = toByteArray(user) ;

        byte[] codedtext = TripleDesDemo._encrypt(arr,"tarunvermacdac@gmail.com");
        byte[] codedtext1 = TripleDesDemo._encrypt(codedtext,"tarun.spicyabc@gmail.com");
        byte[] codedtext2 = TripleDesDemo._encrypt(codedtext1,"direct_tarun@yahoo.co.in");

        writeSmallBinaryFile(codedtext2, "tarun.bat") ;
        byte[] texttoDecrypt = readSmallBinaryFile("tarun.bat");

        byte[] decodedtext = TripleDesDemo._decrypt(texttoDecrypt,"direct_tarun@yahoo.co.in");
        byte[] decodedtext1 = TripleDesDemo._decrypt(decodedtext,"tarun.spicyabc@gmail.com");
        byte[] decodedtext2 = TripleDesDemo._decrypt(decodedtext1,"tarunvermacdac@gmail.com");

        System.out.println(codedtext + " ---> " + toObject(decodedtext2));

      }


    public static byte[] _encrypt(byte[] plainTextBytes, String secretKey) throws Exception {

        byte[] keyBytes = secretKey.getBytes();

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        //byte[] plainTextBytes = message.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte [] base64Bytes = Base64.encodeBase64(buf);
        //String base64EncryptedString = new String(base64Bytes);

        return base64Bytes ;
    }

    public static byte[] _decrypt(byte[] encryptedText, String secretKey) throws Exception {

        //byte[] message = Base64.decodeBase64(encryptedText);
        byte[] message = Base64.decodeBase64(encryptedText);
        byte[] keyBytes = secretKey.getBytes();
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);
        return plainText ;
        //return toObject(plainText);
    }

    public static byte[] toByteArray(UserRegistration obj) throws IOException {
        byte[] bytes = null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray();
        } finally {
            if (oos != null) {
                oos.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
        return bytes;
    }

    public static UserRegistration toObject(byte[] bytes) throws IOException, ClassNotFoundException {
        UserRegistration obj = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            bis = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bis);
            obj = (UserRegistration) ois.readObject();
        } finally {
            if (bis != null) {
                bis.close();
            }
            if (ois != null) {
                ois.close();
            }
        }
        return obj;
    }

    public static byte[] readSmallBinaryFile(String aFileName) throws IOException {
        Path path = Paths.get(aFileName);
        return Files.readAllBytes(path);
    }

    public static void writeSmallBinaryFile(byte[] aBytes, String aFileName) throws IOException {
        Path path = Paths.get(aFileName);
        Files.write(path, aBytes); //creates, overwrites
    }
}

代码在 main 中运行良好,但当我从其他包中的其他类调用它的函数时却不行。这是一个例外。

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64   at desede.TripleDesAlgo._encrypt(TripleDesAlgo.java:81)

这是 .classpath 文件 提前感谢您的帮助。

【问题讨论】:

    标签: java eclipse encryption tripledes


    【解决方案1】:

    您缺少 commons-codec.jar。从http://commons.apache.org/proper/commons-codec/download_codec.cgi 下载。 然后添加它的项目构建路径。为此,右键单击项目,单击属性,单击“Java 构建路径”,打开“库”选项卡,然后单击“添加外部 JAR...”。

    或者如果你使用 maven 添加依赖

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.6</version>
    </dependency>
    

    【讨论】:

    • 我已经在使用 commons-codec-1.9 并且我还为构建路径设置了它,并且它在 main 中运行良好。但不在外面工作。而且我没有使用 ant 或 maven 只是 eclipse。
    • 您的意思是当您在 Eclipse 之外运行应用程序时?在这种情况下尝试 java -cp c:\lib*; com.company.Main,然后将 jars 放入 lib。
    • 没有这个文件有主方法所以我们可以运行那个文件但是当我在同一个项目中使用它但不同的包它不起作用。昨天它错误地工作了我删除了相关的jar,它是commons-codec-1.9 jar,但是当我将它添加回来时,当我从同一个项目中但在不同包中的不同java文件调用它时它不起作用。
    • 如果您将 jar 添加回来,请尝试 Project>Clean。如果这不起作用,请附上您的项目>属性>构建路径的屏幕截图,以确保 jar 在那里。
    • 我遇到了一个类似的问题,有人建议让它在运行时可用,因此添加到 WEB-INF/lib 会有所帮助。 check here
    猜你喜欢
    • 2017-10-16
    • 2017-04-18
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2021-05-20
    • 2017-01-19
    • 1970-01-01
    相关资源
    最近更新 更多