meriley
meriley

Reputation: 1881

Issues using BouncyCastle to encrypt a String

I'm having an issue trying to encrypt and decrypt a string using BouncyCastle.

I'm following an example at http://www.aviransplace.com/2004/10/12/using-rsa-encryption-with-java/ and my code looks like:

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;
    }
}

When I Run this code I end up with an error:

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)    

I'm really new and quite honestly feeling a bit lost when it comes to cryptography. My goal is to figure this out so that I can create and use a RSA key pair using SHA512 and a 4k length. I'm having a lot of trouble finding clear examples of how to do achieve this.

Upvotes: 0

Views: 1952

Answers (1)

meriley
meriley

Reputation: 1881

Need to install the Unlimited Java Cryptography Extension (JCE) Unlimited Strength

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

Upvotes: 2

Related Questions