public static SecretKey generateKey() throws CryptoException { try { synchronized(SymmetricCryptor.class) { if (keyGen == null) { keyGen = KeyGenerator.getInstance(DEFAULT_SYM_KEY_ALGORITHM); } keyGen.init(DEFAULT_KEY_BITS); return keyGen.generateKey(); } } catch (GeneralSecurityException e) { throw new CryptoException(CorePlugin.Event.TEIID10021, e); } }
throw new CommunicationException(JDBCPlugin.Event.TEIID20012, e, e.getMessage());
throw new CommunicationException(JDBCPlugin.Event.TEIID20012, e, e.getMessage());
public static SecretKey generateKey() throws CryptoException { try { synchronized(SymmetricCryptor.class) { if (keyGen == null) { keyGen = KeyGenerator.getInstance(DEFAULT_SYM_KEY_ALGORITHM); } keyGen.init(DEFAULT_KEY_BITS); return keyGen.generateKey(); } } catch (GeneralSecurityException e) { throw new CryptoException(CorePlugin.Event.TEIID10021, e); } }
public byte[] createPublicKey(boolean large) throws CryptoException { try { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM); if (large) { keyGen.initialize(DH_SPEC_2048); } else { keyGen.initialize(DH_SPEC); } KeyPair keypair = keyGen.generateKeyPair(); if (large) { privateKeyLarge = keypair.getPrivate(); } else { privateKey = keypair.getPrivate(); } PublicKey publicKey = keypair.getPublic(); return publicKey.getEncoded(); } catch (NoSuchAlgorithmException e) { throw new CryptoException(CorePlugin.Event.TEIID10001, e); } catch (InvalidAlgorithmParameterException e) { throw new CryptoException(CorePlugin.Event.TEIID10002, e); } }
private static void saveKey(String file, SecretKey key) throws CryptoException, IOException { ArgCheck.isNotNull(file); FileOutputStream fos = new FileOutputStream(file); try { KeyStore store = KeyStore.getInstance("JCEKS"); //$NON-NLS-1$ store.load(null,null); store.setKeyEntry(DEFAULT_ALIAS, key, DEFAULT_STORE_PASSWORD.toCharArray(),null); store.store(fos, DEFAULT_STORE_PASSWORD.toCharArray()); } catch (GeneralSecurityException e) { throw new CryptoException(CorePlugin.Event.TEIID10023, e); } finally { fos.close(); } }
public byte[] createPublicKey(boolean large) throws CryptoException { try { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM); if (large) { keyGen.initialize(DH_SPEC_2048); } else { keyGen.initialize(DH_SPEC); } KeyPair keypair = keyGen.generateKeyPair(); if (large) { privateKeyLarge = keypair.getPrivate(); } else { privateKey = keypair.getPrivate(); } PublicKey publicKey = keypair.getPublic(); return publicKey.getEncoded(); } catch (NoSuchAlgorithmException e) { throw new CryptoException(CorePlugin.Event.TEIID10001, e); } catch (InvalidAlgorithmParameterException e) { throw new CryptoException(CorePlugin.Event.TEIID10002, e); } }
private static void saveKey(String file, SecretKey key) throws CryptoException, IOException { ArgCheck.isNotNull(file); FileOutputStream fos = new FileOutputStream(file); try { KeyStore store = KeyStore.getInstance("JCEKS"); //$NON-NLS-1$ store.load(null,null); store.setKeyEntry(DEFAULT_ALIAS, key, DEFAULT_STORE_PASSWORD.toCharArray(),null); store.store(fos, DEFAULT_STORE_PASSWORD.toCharArray()); } catch (GeneralSecurityException e) { throw new CryptoException(CorePlugin.Event.TEIID10023, e); } finally { fos.close(); } }
/** * Initialize the ciphers used for encryption and decryption. The ciphers * define the algorithms to be used. They are initialized with the * appropriate key to be used in the encryption or decryption operation. */ protected void initDecryptCipher() throws CryptoException { // Create and initialize decryption cipher try { decryptCipher = Cipher.getInstance( cipherAlgorithm); decryptCipher.init( Cipher.DECRYPT_MODE, decryptKey, iv ); } catch ( NoSuchAlgorithmException e ) { throw new CryptoException(CorePlugin.Event.TEIID10009, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10009, cipherAlgorithm )); } catch ( NoSuchPaddingException e ) { throw new CryptoException(CorePlugin.Event.TEIID10010, CorePlugin.Util.gs(CorePlugin.Event.TEIID10010, cipherAlgorithm, e.getClass().getName(), e.getMessage() )); } catch ( InvalidKeyException e ) { throw new CryptoException(CorePlugin.Event.TEIID10011, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10011, e.getClass().getName(), e.getMessage()) ); } catch (InvalidAlgorithmParameterException e) { throw new CryptoException(CorePlugin.Event.TEIID10009, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10009, cipherAlgorithm )); } }
/** * Creates a SymmetricCryptor using the supplied URL contents as the key * * @param key * @return a new SymmetricCryptor * @throws CryptoException * @throws IOException */ public static SymmetricCryptor getSymmectricCryptor(URL keyResource) throws CryptoException, IOException { ArgCheck.isNotNull(keyResource); InputStream stream = keyResource.openStream(); try { KeyStore store = KeyStore.getInstance("JCEKS"); //$NON-NLS-1$ store.load(stream, DEFAULT_STORE_PASSWORD.toCharArray()); Key key = store.getKey(DEFAULT_ALIAS, DEFAULT_STORE_PASSWORD.toCharArray()); return new SymmetricCryptor(key, true); } catch (GeneralSecurityException e) { throw new CryptoException(CorePlugin.Event.TEIID10022, e); } finally { stream.close(); } }
/** * Initialize the ciphers used for encryption and decryption. The ciphers * define the algorithms to be used. They are initialized with the * appropriate key to be used in the encryption or decryption operation. */ protected void initDecryptCipher() throws CryptoException { // Create and initialize decryption cipher try { decryptCipher = Cipher.getInstance( cipherAlgorithm); decryptCipher.init( Cipher.DECRYPT_MODE, decryptKey, iv ); } catch ( NoSuchAlgorithmException e ) { throw new CryptoException(CorePlugin.Event.TEIID10009, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10009, cipherAlgorithm )); } catch ( NoSuchPaddingException e ) { throw new CryptoException(CorePlugin.Event.TEIID10010, CorePlugin.Util.gs(CorePlugin.Event.TEIID10010, cipherAlgorithm, e.getClass().getName(), e.getMessage() )); } catch ( InvalidKeyException e ) { throw new CryptoException(CorePlugin.Event.TEIID10011, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10011, e.getClass().getName(), e.getMessage()) ); } catch (InvalidAlgorithmParameterException e) { throw new CryptoException(CorePlugin.Event.TEIID10009, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10009, cipherAlgorithm )); } }
/** * Creates a SymmetricCryptor using the supplied URL contents as the key * * @param key * @return a new SymmetricCryptor * @throws CryptoException * @throws IOException */ public static SymmetricCryptor getSymmectricCryptor(URL keyResource) throws CryptoException, IOException { ArgCheck.isNotNull(keyResource); InputStream stream = keyResource.openStream(); try { KeyStore store = KeyStore.getInstance("JCEKS"); //$NON-NLS-1$ store.load(stream, DEFAULT_STORE_PASSWORD.toCharArray()); Key key = store.getKey(DEFAULT_ALIAS, DEFAULT_STORE_PASSWORD.toCharArray()); return new SymmetricCryptor(key, true); } catch (GeneralSecurityException e) { throw new CryptoException(CorePlugin.Event.TEIID10022, e); } finally { stream.close(); } }
/** * Initialize the cipher used for encryption. The cipher defines the * algorithm to be used. It is initialized with the appropriate key to * be used in the encryption operation. */ protected void initEncryptCipher() throws CryptoException { // Create and initialize encryption cipher try { encryptCipher = Cipher.getInstance( cipherAlgorithm ); encryptCipher.init( Cipher.ENCRYPT_MODE, encryptKey, iv ); } catch ( NoSuchAlgorithmException e ) { throw new CryptoException(CorePlugin.Event.TEIID10016, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10016, cipherAlgorithm )); } catch ( NoSuchPaddingException e ) { throw new CryptoException(CorePlugin.Event.TEIID10017, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10017, cipherAlgorithm , e.getMessage() )); } catch ( InvalidKeyException e ) { throw new CryptoException(CorePlugin.Event.TEIID10018, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10018, e.getMessage() )); } catch (InvalidAlgorithmParameterException e) { throw new CryptoException(CorePlugin.Event.TEIID10016, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10016, cipherAlgorithm )); } }
public synchronized byte[] encrypt(byte[] buffer, int offset, int length) throws CryptoException { try { byte[] initBlock = null; if (iv != null) { random.nextBytes(randBuffer); initBlock = encryptCipher.update(randBuffer); } byte[] result = encryptCipher.doFinal(buffer, offset, length); if (initBlock != null) { byte[] newResult = Arrays.copyOf(initBlock, initBlock.length + result.length); System.arraycopy(result, 0, newResult, initBlock.length, result.length); return newResult; } return result; } catch ( Exception e ) { try { initEncryptCipher(); } catch (CryptoException err) { //shouldn't happen } throw new CryptoException(CorePlugin.Event.TEIID10013, CorePlugin.Util.gs(CorePlugin.Event.TEIID10013, e.getMessage())); } }
/** * Initialize the cipher used for encryption. The cipher defines the * algorithm to be used. It is initialized with the appropriate key to * be used in the encryption operation. */ protected void initEncryptCipher() throws CryptoException { // Create and initialize encryption cipher try { encryptCipher = Cipher.getInstance( cipherAlgorithm ); encryptCipher.init( Cipher.ENCRYPT_MODE, encryptKey, iv ); } catch ( NoSuchAlgorithmException e ) { throw new CryptoException(CorePlugin.Event.TEIID10016, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10016, cipherAlgorithm )); } catch ( NoSuchPaddingException e ) { throw new CryptoException(CorePlugin.Event.TEIID10017, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10017, cipherAlgorithm , e.getMessage() )); } catch ( InvalidKeyException e ) { throw new CryptoException(CorePlugin.Event.TEIID10018, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10018, e.getMessage() )); } catch (InvalidAlgorithmParameterException e) { throw new CryptoException(CorePlugin.Event.TEIID10016, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10016, cipherAlgorithm )); } }
public synchronized byte[] encrypt(byte[] buffer, int offset, int length) throws CryptoException { try { byte[] initBlock = null; if (iv != null) { random.nextBytes(randBuffer); initBlock = encryptCipher.update(randBuffer); } byte[] result = encryptCipher.doFinal(buffer, offset, length); if (initBlock != null) { byte[] newResult = Arrays.copyOf(initBlock, initBlock.length + result.length); System.arraycopy(result, 0, newResult, initBlock.length, result.length); return newResult; } return result; } catch ( Exception e ) { try { initEncryptCipher(); } catch (CryptoException err) { //shouldn't happen } throw new CryptoException(CorePlugin.Event.TEIID10013, CorePlugin.Util.gs(CorePlugin.Event.TEIID10013, e.getMessage())); } }
/** * Decrypt the ciphertext to yield the original cleartext. * @param ciphertext The text to be encrypted, in byte form * @param The decrypted cleartext, in byte form */ public synchronized byte[] decrypt( byte[] ciphertext ) throws CryptoException { try { byte[] result = decryptCipher.doFinal(ciphertext); if (iv != null) { //throw away the first block return Arrays.copyOfRange(result, iv.getIV().length, result.length); } return result; } catch ( Exception e ) { try { initDecryptCipher(); } catch (CryptoException err) { //shouldn't happen } throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage())); } }
/** * Decrypt the ciphertext to yield the original cleartext. * @param ciphertext The text to be encrypted, in byte form * @param The decrypted cleartext, in byte form */ public synchronized byte[] decrypt( byte[] ciphertext ) throws CryptoException { try { byte[] result = decryptCipher.doFinal(ciphertext); if (iv != null) { //throw away the first block return Arrays.copyOfRange(result, iv.getIV().length, result.length); } return result; } catch ( Exception e ) { try { initDecryptCipher(); } catch (CryptoException err) { //shouldn't happen } throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage())); } }
throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage())); } finally { Thread.currentThread().setContextClassLoader(cl); return ois.readObject(); } catch (Exception e) { throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage()));
throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage())); } finally { Thread.currentThread().setContextClassLoader(cl); return ois.readObject(); } catch (Exception e) { throw new CryptoException(CorePlugin.Event.TEIID10006, CorePlugin.Util.gs(CorePlugin.Event.TEIID10006, e.getClass().getName(), e.getMessage()));