代码:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class crypto {
public static void main(String [] args) {
String s = args[0];
String s1 = args[1];
String ivkey = "thisisasecretword1";
byte[] ivraw = ivkey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(ivraw, "AES");
byte[] iv = { 't','h','i','s','a','s','e','c','r','e','t','w','o','r','d','1' };
IvParameterSpec ivspec = new IvParameterSpec(iv);
if (s.equalsIgnoreCase("ENCRYPT")) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
byte[] encrypted = cipher.doFinal(s1.getBytes());
System.out.println("encrypt: " + new String(Base64.encodeBase64(encrypted)));
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
byte[] encrypted = cipher.doFinal(Base64.decodeBase64(s1));
System.out.println("decrypt: " + new String(encrypted));
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
我正在尝试编译它,只是为了测试我们正在考虑使用的新加密方法。这是第一次迭代,但将在以后尽职尽责地保护以实现随机密钥。无论如何,当我尝试编译它时,我将osx中的classpath
设置为commons-coded-1.9.jar
所在的位置。我运行javac crypto.java
并且没有错误。我运行java crypto "ENCRYPT" "TEST"
并收到NoClassDefFoundError: crypto
消息。我一直在搞这个但是看不出我弄错了。希望一双新鲜的眼睛能够照亮它。