简体   繁体   English

java.lang.NoClassDefFoundError:org / apache / commons / codec / binary / Base64

[英]java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64

Admin please don't mark it as duplicate read my question completely. 管理员请不要将其标记为重复完整阅读我的问题。 I am encrypting and decrypting some text but while running in same file with main its running fine but when i call its encrypt and decrypt function from outside. 我正在加密和解密一些文本但是在运行在同一个文件中时,main运行正常但是当我从外部调用它的加密和解密函数时。 Its giving an error at runtime. 它在运行时发出错误。 I am attaching the code. 我附上了代码。 package desede; 包装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
    }
}

The code is running fine with main but not when i call its function from other class which is in other package. 代码运行正常与main,但不是当我从其他类中的其他类调用其函数时。 Here is the exception. 这是例外。

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

And this is .classpath file 这是.classpath文件 这是我的包资源管理器的截图 Thanks in advance for any help. 在此先感谢您的帮助。

You are missing commons-codec.jar. 你缺少commons-codec.jar。 Download it from http://commons.apache.org/proper/commons-codec/download_codec.cgi . http://commons.apache.org/proper/commons-codec/download_codec.cgi下载。 Then add it project build path. 然后添加项目构建路径。 To do that right click the project, click Properties, click "Java Build Path", open "Library" tab, and click "Add External JARs...". 要执行此操作,请右键单击项目,单击“属性”,单击“Java构建路径”,打开“库”选项卡,然后单击“添加外部JAR ...”。

Or if you are using maven add dependency for 或者,如果您正在使用maven添加依赖项

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

暂无
暂无

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

相关问题 Selenium SouceLabs-java.lang.NoClassDefFoundError:org / apache / commons / codec / binary / Base64 - Selenium SouceLabs - java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64 java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64: from a library jar from import with Maven - java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64: from a library jar imported with Maven java.lang.NoClassDefFoundError:org / apache / tomcat / util / codec / binary / Base64 - java.lang.NoClassDefFoundError: org/apache/tomcat/util/codec/binary/Base64 如何使用org.apache.commons.codec.binary.base64对Java对象进行Base64编码? - How to Base64 encode a Java object using org.apache.commons.codec.binary.base64? NoClassDefFoundError:org / apache / tomcat / util / codec / binary / Base64 - NoClassDefFoundError: org/apache/tomcat/util/codec/binary/Base64 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 尝试创建CommonsHttpOAuthConsumer时java.lang.ClassNotFoundException:org.apache.commons.codec.binary.Base64 - java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64 when trying to create CommonsHttpOAuthConsumer java.lang.ClassNotFoundException:org.apache.commons.codec.binary.Base64 - java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM