|
From: Pelle B. <pe...@us...> - 2004-04-09 20:16:16
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4281/src/java/org/neuclear/commons/crypto Modified Files: CryptoTools.java Log Message: Added PrivateKey wrapping and unwrapping to CryptoTools with the methods: byte [] wrapKey(char passphrase[], PrivateKey key) and PrivateKey unWrapKey(char passphrase[],byte wrapped[],String algorithm) PrivateKey unWrapRSAKey(char passphrase[],byte wrapped[]) Index: CryptoTools.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/crypto/CryptoTools.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** CryptoTools.java 31 Mar 2004 18:48:25 -0000 1.19 --- CryptoTools.java 9 Apr 2004 20:02:54 -0000 1.20 *************** *** 2,5 **** --- 2,12 ---- * $Id$ * $Log$ + * Revision 1.20 2004/04/09 20:02:54 pelle + * Added PrivateKey wrapping and unwrapping to CryptoTools with the methods: + * byte [] wrapKey(char passphrase[], PrivateKey key) + * and + * PrivateKey unWrapKey(char passphrase[],byte wrapped[],String algorithm) + * PrivateKey unWrapRSAKey(char passphrase[],byte wrapped[]) + * * Revision 1.19 2004/03/31 18:48:25 pelle * Added various Streams for simplified crypto operations. *************** *** 267,270 **** --- 274,278 ---- import org.neuclear.commons.time.TimeTools; + import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; *************** *** 689,692 **** --- 697,701 ---- } + /** * Adapted from BouncyCastle's JDKKeyStore class.<p> *************** *** 704,707 **** --- 713,741 ---- } + public static byte[] wrapKey(final char password[], final PrivateKey key) throws CryptoException { + try { + Cipher cipher = makePBECipher(Cipher.WRAP_MODE, password); + return cipher.wrap(key); + } catch (BadPaddingException e) { + throw new CryptoException(e); + } catch (GeneralSecurityException e) { + throw new CryptoException(e); + } + } + + public static PrivateKey unWrapKey(final char password[], final byte data[], final String algorithm) throws CryptoException { + try { + Cipher cipher = makePBECipher(Cipher.UNWRAP_MODE, password); + return (PrivateKey) cipher.unwrap(data, algorithm, Cipher.PRIVATE_KEY); + } catch (BadPaddingException e) { + throw new CryptoException(e); + } catch (GeneralSecurityException e) { + throw new CryptoException(e); + } + } + + public static PrivateKey unWrapRSAKey(final char password[], final byte data[]) throws CryptoException { + return unWrapKey(password, data, "RSA"); + } public static PublicKey createPK(final String mod, final String exp) throws CryptoException { |