【问题标题】:Unable to run a simple program with one dependency无法运行具有一个依赖项的简单程序
【发布时间】:2018-01-06 20:38:54
【问题描述】:

我正在尝试编译和运行一个小型示例应用程序,但没有成功。示例应用选自 SF 上的this response

这就是我正在做的:

  1. this page 下载了带有 BountyCastle 库的 bcprov-jdk15on-159.jar 文件;放入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
    

我做错了什么?

【问题讨论】:

    标签: java jar bouncycastle


    【解决方案1】:

    尝试运行(在 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);
        }
    

    }

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2016-03-08
    • 1970-01-01
    • 2020-12-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多