无法 运行 具有一个依赖项的简单程序
Unable to run a simple program with one dependency
我正在尝试编译 运行 一个小型示例应用程序,但没有成功。示例应用程序选自 SF 上的 this response。
这就是我正在做的事情:
下载了一个bcprov-jdk15on-159.jar
file with BountyCastle library from this page;放入sample
文件夹,
复制粘贴上述 response 的来源;放入sample
文件夹,
我现在在一个文件夹中有这些文件:
$ ls -lat
total 8008
drwxr-xr-x 4 gmile staff 136 Jan 6 13:58 .
-rw-r--r--@ 1 gmile staff 4092400 Jan 6 13:58 bcprov-jdk15on-159.jar
-rw-r--r-- 1 gmile staff 2302 Jan 6 13:39 PBE.java
drwxr-xr-x 10 gmile staff 340 Jan 6 13:38 ..
将PBE.java
编译成PBE.class
(这里没有问题):
$ javac -cp bcprov-jdk15on-159.jar PBE.java
将PBE.class
放入.jar
文件中:
$ jar cvf pbe.jar PBE.class
added manifest
adding: PBE.class(in = 2448) (out= 1289)(deflated 47%)
尝试 运行 包含依赖项的程序,如 here 所示,例如:
$ java -classpath 'bcprov-jdk15on-159.jar;pbe.jar;' PBE
Error: Could not find or load main class PBE
我做错了什么?
尝试 运行(在 Windows 上):
java -cp bcprov-jdk15on-159.jar;pbe.jar PBE
或 Linux/similar:
java -cp bcprov-jdk15on-159.jar:pbe.jar PBE
如果你真的想看到 encryption/decryption 在工作,请使用相同的 IV 和 SecretKey 进行加密和解密,例如:
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class PBE {
private static final String salt = "A long, but constant phrase that will be used each time as the salt.";
private static final int iterations = 2000;
private static final int keyLength = 256;
private static final SecureRandom random = new SecureRandom();
public static void main(String [] args) throws Exception {
Security.insertProviderAt(new BouncyCastleProvider(), 1);
String passphrase = "The quick brown fox jumped over the lazy brown dog";
String plaintext = "hello world";
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
SecretKey key = generateKey(passphrase);
byte[] iv = new byte[cipher.getBlockSize()];
random.nextBytes(iv);
byte [] ciphertext = encrypt(key, iv, plaintext);
String recoveredPlaintext = decrypt(key, iv, ciphertext);
System.out.println(recoveredPlaintext);
}
private static byte [] encrypt(SecretKey key, byte[] iv, String plaintext) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv), random);
return cipher.doFinal(plaintext.getBytes());
}
private static String decrypt(SecretKey key, byte[] iv, byte [] ciphertext) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv), random);
return new String(cipher.doFinal(ciphertext));
}
private static SecretKey generateKey(String passphrase) throws Exception {
PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt.getBytes(), iterations, keyLength);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
return keyFactory.generateSecret(keySpec);
}
}
我正在尝试编译 运行 一个小型示例应用程序,但没有成功。示例应用程序选自 SF 上的 this response。
这就是我正在做的事情:
下载了一个
bcprov-jdk15on-159.jar
file with BountyCastle library from this page;放入sample
文件夹,复制粘贴上述 response 的来源;放入
sample
文件夹,我现在在一个文件夹中有这些文件:
$ ls -lat total 8008 drwxr-xr-x 4 gmile staff 136 Jan 6 13:58 . -rw-r--r--@ 1 gmile staff 4092400 Jan 6 13:58 bcprov-jdk15on-159.jar -rw-r--r-- 1 gmile staff 2302 Jan 6 13:39 PBE.java drwxr-xr-x 10 gmile staff 340 Jan 6 13:38 ..
将
PBE.java
编译成PBE.class
(这里没有问题):$ javac -cp bcprov-jdk15on-159.jar PBE.java
将
PBE.class
放入.jar
文件中:$ jar cvf pbe.jar PBE.class added manifest adding: PBE.class(in = 2448) (out= 1289)(deflated 47%)
尝试 运行 包含依赖项的程序,如 here 所示,例如:
$ java -classpath 'bcprov-jdk15on-159.jar;pbe.jar;' PBE Error: Could not find or load main class PBE
我做错了什么?
尝试 运行(在 Windows 上):
java -cp bcprov-jdk15on-159.jar;pbe.jar PBE
或 Linux/similar:
java -cp bcprov-jdk15on-159.jar:pbe.jar PBE
如果你真的想看到 encryption/decryption 在工作,请使用相同的 IV 和 SecretKey 进行加密和解密,例如:
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class PBE {
private static final String salt = "A long, but constant phrase that will be used each time as the salt.";
private static final int iterations = 2000;
private static final int keyLength = 256;
private static final SecureRandom random = new SecureRandom();
public static void main(String [] args) throws Exception {
Security.insertProviderAt(new BouncyCastleProvider(), 1);
String passphrase = "The quick brown fox jumped over the lazy brown dog";
String plaintext = "hello world";
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
SecretKey key = generateKey(passphrase);
byte[] iv = new byte[cipher.getBlockSize()];
random.nextBytes(iv);
byte [] ciphertext = encrypt(key, iv, plaintext);
String recoveredPlaintext = decrypt(key, iv, ciphertext);
System.out.println(recoveredPlaintext);
}
private static byte [] encrypt(SecretKey key, byte[] iv, String plaintext) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv), random);
return cipher.doFinal(plaintext.getBytes());
}
private static String decrypt(SecretKey key, byte[] iv, byte [] ciphertext) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv), random);
return new String(cipher.doFinal(ciphertext));
}
private static SecretKey generateKey(String passphrase) throws Exception {
PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt.getBytes(), iterations, keyLength);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
return keyFactory.generateSecret(keySpec);
}
}