0

Code:


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

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class AESTest 
{ 
    public static void main(String [] args)
    {
        String enc = AESEncryptToBase64("000000", "XJ5QJSVMKZGBOQO7HMSIJO5BERW2OYWDVNPM3BH32NLSWUCNJ4FIP3BML7EKUBNO");
        System.out.println(enc);
    }

    /**
     * 
     * @param secret
     * @param cleartext
     * @return encrypted b64 string
     */
    public static String AESEncryptToBase64(String secret, String clearText) {
        byte[] rawKey = new byte[32];
        java.util.Arrays.fill(rawKey, (byte) 0);
        byte[] secretBytes = secret.getBytes();
        for(int i = 0; i < secretBytes.length; i++){
            rawKey[i] = secretBytes[i];
        }

        SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
        try{ 
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encryptedData = cipher.doFinal(clearText.getBytes());
            if(encryptedData == null) return null;
            // return "l";
            return Base64.encodeBase64String(encryptedData);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;      

    }
}

Compile And run:


 $ javac -cp "commons-codec-1.7.jar" AESTest.java 
 $ java -cp "commons-codec-1.7.jar" AESTest
 Exception in thread "main" java.lang.NoClassDefFoundError: AESTest
 Caused by: java.lang.ClassNotFoundException: AESTest

Here's the apache-commons-codec: http://apache.mirrors.pair.com//commons/codec/binaries/commons-codec-1.7-bin.zip

4
  • 3
    include . into your classpath: java -cp ".:commons-codec-1.7.jar" AESTest Commented Dec 7, 2012 at 15:34
  • to be fair, hoaz comment should be a (accepted) response :-) Commented Dec 7, 2012 at 15:48
  • What java compiler are you using? I can't reproduce your error with 1.6.0_37 Commented Dec 7, 2012 at 15:49
  • @hoaz this should be the answer. Can you put it as an answer please Commented Dec 7, 2012 at 15:50

1 Answer 1

3

Include . into your classpath: java -cp ".:commons-codec-1.7.jar" AESTest

This will tell JVM to include classes from current folder to classpath

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.