无法运行具有一个依赖项的简单程序

时间:2018-01-06 12:18:27

标签: java jar bouncycastle

我正在尝试编译并运行一个小样本应用程序,没有运气。示例应用程序是从SF上的this response中选取的。

这就是我正在做的事情:

  1. bcprov-jdk15on-159.jar下载了带有BountyCastle库的this page文件;将其放入sample文件夹,

  2. 从提到的response复制粘贴来源;将其放入sample文件夹,

  3. 我现在将这些文件放在一个文件夹中:

    $ 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 ..
    
  4. PBE.java编译为PBE.class(此处没有问题):

    $ javac -cp bcprov-jdk15on-159.jar PBE.java
    
  5. PBE.class放入.jar文件中:

    $ jar cvf pbe.jar PBE.class
    added manifest
    adding: PBE.class(in = 2448) (out= 1289)(deflated 47%)
    
  6. 尝试运行包含依赖项的程序,如here所示,例如:

    $ java -classpath 'bcprov-jdk15on-159.jar;pbe.jar;' PBE
    Error: Could not find or load main class PBE
    
  7. 我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试运行(在Windows上):

java -cp bcprov-jdk15on-159.jar;pbe.jar PBE

或者在Linux /类似的地方:

java -cp bcprov-jdk15on-159.jar:pbe.jar PBE

如果您确实希望在工作中看到加密/解密,请使用相同的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);
    }

}