java - Issues using BouncyCastle to encrypt a String -
i'm having issue trying encrypt , decrypt string using bouncycastle.
i'm following example @ http://www.aviransplace.com/2004/10/12/using-rsa-encryption-with-java/ , code looks like:
public class cryptotests { public static final string algorithm = "rsa"; /** * @param args command line arguments */ public static void main(string[] args) { try { init(); keypair kp = generatekey(); byte[] enc = encrypt("the fat cat jumped on 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 text using public key. * * @param text original unencrypted text * @param key public key * @return encrypted text * @throws java.lang.exception */ public static byte[] encrypt(byte[] text, publickey key) throws exception { byte[] ciphertext = null; // rsa cipher object , print provider cipher cipher = cipher.getinstance( "rsa / ecb / pkcs1padding"); system.out.println( "nprovider is:" + cipher.getprovider().getinfo()); // encrypt plaintext using public key cipher.init(cipher.encrypt_mode, key); ciphertext = cipher.dofinal(text); return ciphertext; } /** * decrypt text using private key * * @param text encrypted text * @param key private key * @return unencrypted text * @throws java.lang.exception */ public static byte[] decrypt(byte[] text, privatekey key) throws exception { byte[] dectyptedtext = null; // decrypt text using private key cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding"); cipher.init(cipher.decrypt_mode, key); dectyptedtext = cipher.dofinal(text); return dectyptedtext; } }
when run code end error:
may 21, 2013 10:20:31 cryptotests.cryptotests main severe: null java.security.invalidkeyexception: illegal key size or default parameters @ javax.crypto.cipher.checkcryptoperm(cipher.java:1011) @ javax.crypto.cipher.init(cipher.java:1209) @ javax.crypto.cipher.init(cipher.java:1153) @ cryptotests.cryptotests.encrypt(cryptotests.java:70) @ cryptotests.cryptotests.main(cryptotests.java:34)
i'm new , quite feeling bit lost when comes cryptography. goal figure out can create , use rsa key pair using sha512 , 4k length. i'm having lot of trouble finding clear examples of how achieve this.
need install unlimited java cryptography extension (jce) unlimited strength
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
Comments
Post a Comment