【问题标题】:Issues using BouncyCastle to encrypt a String使用 BouncyCastle 加密字符串的问题
【发布时间】:2013-05-22 04:54:19
【问题描述】:

我在尝试使用 BouncyCastle 加密和解密字符串时遇到问题。

我正在关注http://www.aviransplace.com/2004/10/12/using-rsa-encryption-with-java/ 的示例,我的代码如下所示:

public class Cryptotests {

    public static final String ALGORITHM = "RSA";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try {
            init();
            KeyPair kp = generateKey();
            byte[] enc = encrypt("The Fat Cat Jumped Over the Bat".getBytes("UTF8"), kp.getPublic());
            byte[] dec = decrypt(enc, kp.getPrivate());
        } catch (Exception ex) {
            Logger.getLogger(Cryptotests.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void init() {
        Security.addProvider(new BouncyCastleProvider());
    }

    public static KeyPair generateKey() throws NoSuchAlgorithmException {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
        keyGen.initialize(1024);
        KeyPair key = keyGen.generateKeyPair();
        return key;
    }

    /**
     * Encrypt a text using public key.
     *
     * @param text The original unencrypted text
     * @param key The public key
     * @return Encrypted text
     * @throws java.lang.Exception
     */
    public static byte[] encrypt(byte[] text, PublicKey key) throws Exception {

        byte[] cipherText = null;
        // get an RSA cipher object and print the provider
        Cipher cipher = Cipher.getInstance(
                "RSA / ECB / PKCS1Padding");
        System.out.println(
                "nProvider is:" + cipher.getProvider().getInfo());

        // encrypt the plaintext using the public key
        cipher.init(Cipher.ENCRYPT_MODE, key);
        cipherText = cipher.doFinal(text);
        return cipherText;

    }

    /**
     * Decrypt text using private key
     *
     * @param text The encrypted text
     * @param key The private key
     * @return The unencrypted text
     * @throws java.lang.Exception
     */
    public static byte[] decrypt(byte[] text, PrivateKey key) throws Exception {

        byte[] dectyptedText = null;
        // decrypt the text using the private key
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        dectyptedText = cipher.doFinal(text);
        return dectyptedText;
    }
}

当我运行此代码时,我最终会遇到错误:

May 21, 2013 10:20:31 AM cryptotests.Cryptotests main
SEVERE: null
java.security.InvalidKeyException: Illegal key size or default parameters
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1011)
    at javax.crypto.Cipher.init(Cipher.java:1209)
    at javax.crypto.Cipher.init(Cipher.java:1153)
    at cryptotests.Cryptotests.encrypt(Cryptotests.java:70)
    at cryptotests.Cryptotests.main(Cryptotests.java:34)    

当谈到密码学时,我真的是个新手,老实说有点迷茫。我的目标是弄清楚这一点,以便我可以使用 SHA512 和 4k 长度创建和使用 RSA 密钥对。我很难找到明确的例子来说明如何做到这一点。

【问题讨论】:

  • 这很明显:Illegal key size or default parameters 找出什么是合法的。
  • 什么版本的 bouncycastle jar 和你正在运行什么 jre。我使用 JRE 1.6 和 bcprov-jdk16-1.46.jar 毫无例外地运行了您的示例。
  • 出于兴趣 - 您是否出于任何特定原因使用 BouncyCastle?我原以为您可以使用标准 Java 提供程序来实现所有这些。
  • 邓肯,老实说这可能是因为我没有密码学经验。我希望能够实现的目标:我在一个文本文件中提供了一个私钥,它是 PKCS8 PEM 和一个可以使用这个密钥解码的文件,我不知道如何解码该文件,因为我不够熟悉密码学。老实说,我越读越觉得自己越来越迷失。它还需要是 Java

标签: java cryptography bouncycastle


【解决方案1】:

需要安装 Unlimited Java Cryptography Extension (JCE) Unlimited Strength

http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

【讨论】:

    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多