List of usage examples for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider
public BouncyCastleProvider()
From source file:com.otterca.common.crypto.X509CertificateBuilderTest.java
License:Apache License
/** * Default constructor./*w w w. j a v a2s. c o m*/ * * @throws Exception */ public X509CertificateBuilderTest() throws GeneralSecurityException { provider = new BouncyCastleProvider(); Security.addProvider(provider); }
From source file:com.password.locker.crypto.SecureCryptoImpl.java
License:Open Source License
/** * SecureCrypto Constructor./*w ww. ja va 2 s . c o m*/ * * @param password * password for the crypto keyspec. * * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws NoSuchPaddingException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException */ public SecureCryptoImpl(final char[] password) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException { SHA256Digest digest = new SHA256Digest(); String s = Constants.PROPERTIES.getStringProperty(Constants.SALT_KEY, PasswordUtils.getSalt(digest)); salt = Hex.decode(s); if (salt.length != digest.getDigestSize()) { LOGGER.warn("Warning salt size is not the size of the Digest."); } //--------------------------------------------------- // Setup encryption. //--------------------------------------------------- PBEParametersGenerator pGen = new PKCS12ParametersGenerator(digest); pGen.init(PBEParametersGenerator.PKCS12PasswordToBytes(password), salt, ITERATIONS); ParametersWithIV params = (ParametersWithIV) pGen.generateDerivedParameters(KEY_LEN, IV_LEN); SecretKeySpec encKey = new SecretKeySpec(((KeyParameter) params.getParameters()).getKey(), "AES"); encryption = Cipher.getInstance(Constants.CRYPTO_ALGORITHM, new BouncyCastleProvider()); encryption.init(Cipher.ENCRYPT_MODE, encKey, new IvParameterSpec(params.getIV())); //--------------------------------------------------- // Setup decryption. //--------------------------------------------------- decryption = Cipher.getInstance(Constants.CRYPTO_SEC_KEY_SPEC, new BouncyCastleProvider()); PBEKeySpec keySpec = new PBEKeySpec(password, salt, ITERATIONS); SecretKeyFactory fact = SecretKeyFactory.getInstance(Constants.CRYPTO_SEC_KEY_SPEC, new BouncyCastleProvider()); try { decryption.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec)); } catch (InvalidKeySpecException e) { ExceptionUtils.fatalError(SecureCryptoImpl.class, e); } Constants.PROPERTIES.addProperty(Constants.SALT_KEY, s); }
From source file:com.password.locker.cypto.test.SecureCryptoTest.java
License:Open Source License
/** * @throws java.lang.Exception//w w w . j a v a 2 s.co m * if an error occurs. */ @Before public final void setUp() throws Exception { Security.addProvider(new BouncyCastleProvider()); String password = "password"; secImpl = new SecureCryptoImpl(password.toCharArray()); }
From source file:com.password.locker.Main.java
License:Open Source License
/** * Main constructor.//from w w w.j av a 2 s.c o m * @param args * arguments passed to the program. */ public static void main(final String[] args) { Security.addProvider(new BouncyCastleProvider()); setLookAndFeel(); StateMonitor monitor = new StateMonitor(); Main.COORDINATOR.scheduleAtFixedRate(monitor, Constants.MONITOR_DELAY, Constants.MONITOR_PERIOD, Constants.MONITOR_TIMEUNIT); addHook(); }
From source file:com.pazdev.jose.JWETest.java
License:Apache License
@Test public void testECDH() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchProviderException { Security.addProvider(new BouncyCastleProvider()); KeyPairGenerator gen = KeyPairGenerator.getInstance("EC", "BC"); gen.initialize(new ECGenParameterSpec("P-521")); KeyPair pair = gen.generateKeyPair(); String cleartext = "For unto us a child is born, unto us a son is given!"; Header header = Header.builder().withAlgorithm(Algorithm.ECDH_ES).withEncryptionAlgorithm(Algorithm.A256GCM) .build();//from ww w . ja v a2 s . c om JWE jwe = JWE.builder().withPayload(cleartext).withKey(pair.getPublic()).withProtectedHeader(header) .build(); System.out.println(jwe.toCompact()); System.out.println(jwe.toJson()); assertEquals(cleartext, jwe.decryptString(pair.getPrivate())); }
From source file:com.pazdev.jose.JWETest.java
License:Apache License
@Test public void testECDHKW() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchProviderException { Security.addProvider(new BouncyCastleProvider()); KeyPairGenerator gen = KeyPairGenerator.getInstance("EC", "BC"); gen.initialize(new ECGenParameterSpec("P-521")); KeyPair pair = gen.generateKeyPair(); String cleartext = "For unto us a child is born, unto us a son is given!"; Header header = Header.builder().withAlgorithm(Algorithm.ECDH_ES_A256KW) .withEncryptionAlgorithm(Algorithm.A256GCM).build(); JWE jwe = JWE.builder().withPayload(cleartext).withKey(pair.getPublic()).withProtectedHeader(header) .build();/*w w w .j a v a 2 s.c o m*/ System.out.println(jwe.toCompact()); System.out.println(jwe.toJson()); assertEquals(cleartext, jwe.decryptString(pair.getPrivate())); }
From source file:com.pieframework.runtime.utils.CertificateUtils.java
License:Apache License
public static String encryptPassword(String rdpPassword, X509Certificate certificate) { Security.addProvider(new BouncyCastleProvider()); String encryptedPassword = ""; //get PrivateKey And certificate from pfx file try {//from w w w. ja va 2 s .co m certificate.checkValidity(); CMSEnvelopedDataGenerator envDataGen = new CMSEnvelopedDataGenerator(); envDataGen.addKeyTransRecipient(certificate); CMSProcessable envData = new CMSProcessableByteArray(rdpPassword.getBytes()); CMSEnvelopedData enveloped = envDataGen.generate(envData, CMSEnvelopedDataGenerator.DES_EDE3_CBC, "BC"); byte[] data = enveloped.getEncoded(); encryptedPassword = new String(Base64.encodeBase64(data)); } catch (Exception e) { e.printStackTrace(); } return encryptedPassword; }
From source file:com.piusvelte.taplock.server.TapLockServer.java
License:Open Source License
private static void initialize() { (new File(APP_PATH)).mkdir(); if (OS == OS_WIN) Security.addProvider(new BouncyCastleProvider()); System.out.println("APP_PATH: " + APP_PATH); try {/*from w w w . j av a2 s . c o m*/ sLogFileHandler = new FileHandler(sLog); } catch (SecurityException e) { writeLog("sLogFileHandler init: " + e.getMessage()); } catch (IOException e) { writeLog("sLogFileHandler init: " + e.getMessage()); } File propertiesFile = new File(sProperties); if (!propertiesFile.exists()) { try { propertiesFile.createNewFile(); } catch (IOException e) { writeLog("propertiesFile.createNewFile: " + e.getMessage()); } } Properties prop = new Properties(); try { prop.load(new FileInputStream(sProperties)); if (prop.isEmpty()) { prop.setProperty(sPassphraseKey, sPassphrase); prop.setProperty(sDisplaySystemTrayKey, Boolean.toString(sDisplaySystemTray)); prop.setProperty(sDebuggingKey, Boolean.toString(sDebugging)); prop.store(new FileOutputStream(sProperties), null); } else { if (prop.containsKey(sPassphraseKey)) sPassphrase = prop.getProperty(sPassphraseKey); else prop.setProperty(sPassphraseKey, sPassphrase); if (prop.containsKey(sDisplaySystemTrayKey)) sDisplaySystemTray = Boolean.parseBoolean(prop.getProperty(sDisplaySystemTrayKey)); else prop.setProperty(sDisplaySystemTrayKey, Boolean.toString(sDisplaySystemTray)); if (prop.containsKey(sDebuggingKey)) sDebugging = Boolean.parseBoolean(prop.getProperty(sDebuggingKey)); else prop.setProperty(sDebuggingKey, Boolean.toString(sDebugging)); } } catch (FileNotFoundException e) { writeLog("prop load: " + e.getMessage()); } catch (IOException e) { writeLog("prop load: " + e.getMessage()); } if (sLogFileHandler != null) { sLogger = Logger.getLogger("TapLock"); sLogger.setUseParentHandlers(false); sLogger.addHandler(sLogFileHandler); SimpleFormatter sf = new SimpleFormatter(); sLogFileHandler.setFormatter(sf); writeLog("service starting"); } if (sDisplaySystemTray && SystemTray.isSupported()) { final SystemTray systemTray = SystemTray.getSystemTray(); Image trayIconImg = Toolkit.getDefaultToolkit() .getImage(TapLockServer.class.getResource("/systemtrayicon.png")); final TrayIcon trayIcon = new TrayIcon(trayIconImg, "Tap Lock"); trayIcon.setImageAutoSize(true); PopupMenu popupMenu = new PopupMenu(); MenuItem aboutItem = new MenuItem("About"); CheckboxMenuItem toggleSystemTrayIcon = new CheckboxMenuItem("Display Icon in System Tray"); toggleSystemTrayIcon.setState(sDisplaySystemTray); CheckboxMenuItem toggleDebugging = new CheckboxMenuItem("Debugging"); toggleDebugging.setState(sDebugging); MenuItem shutdownItem = new MenuItem("Shutdown Tap Lock Server"); popupMenu.add(aboutItem); popupMenu.add(toggleSystemTrayIcon); if (OS == OS_WIN) { MenuItem setPasswordItem = new MenuItem("Set password"); popupMenu.add(setPasswordItem); setPasswordItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JPanel panel = new JPanel(); JLabel label = new JLabel("Enter your Windows account password:"); JPasswordField passField = new JPasswordField(32); panel.add(label); panel.add(passField); String[] options = new String[] { "OK", "Cancel" }; int option = JOptionPane.showOptionDialog(null, panel, "Tap Lock", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]); if (option == 0) { String password = encryptString(new String(passField.getPassword())); if (password != null) { Properties prop = new Properties(); try { prop.load(new FileInputStream(sProperties)); prop.setProperty(sPasswordKey, password); prop.store(new FileOutputStream(sProperties), null); } catch (FileNotFoundException e1) { writeLog("prop load: " + e1.getMessage()); } catch (IOException e1) { writeLog("prop load: " + e1.getMessage()); } } } } }); } popupMenu.add(toggleDebugging); popupMenu.add(shutdownItem); trayIcon.setPopupMenu(popupMenu); try { systemTray.add(trayIcon); } catch (AWTException e) { writeLog("systemTray.add: " + e.getMessage()); } aboutItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String newline = System.getProperty("line.separator"); newline += newline; JOptionPane.showMessageDialog(null, "Tap Lock" + newline + "Copyright (c) 2012 Bryan Emmanuel" + newline + "This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version." + newline + "This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details." + newline + "You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>." + newline + "Bryan Emmanuel piusvelte@gmail.com"); } }); toggleSystemTrayIcon.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { setTrayIconDisplay(e.getStateChange() == ItemEvent.SELECTED); if (!sDisplaySystemTray) systemTray.remove(trayIcon); } }); toggleDebugging.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { setDebugging(e.getStateChange() == ItemEvent.SELECTED); } }); shutdownItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { shutdown(); } }); } synchronized (sConnectionThreadLock) { (sConnectionThread = new ConnectionThread()).start(); } }
From source file:com.playonlinux.core.gpg.SignatureChecker.java
License:Open Source License
private void initVerify(PGPSignature pgpSignature, PGPPublicKey pgpSigningKey) throws PGPException, NoSuchProviderException { try {/*from w w w. j a v a 2s. c o m*/ pgpSignature.initVerify(pgpSigningKey, "BC"); } catch (NoSuchProviderException e) { LOGGER.debug("No security provider found. Adding bouncy castle. This message can be ignored", e); Security.addProvider(new BouncyCastleProvider()); pgpSignature.initVerify(pgpSigningKey, "BC"); } }
From source file:com.playonlinux.utils.SignatureChecker.java
License:Open Source License
public Boolean check() throws IOException, CMSException, PGPException, NoSuchProviderException, SignatureException { PGPPublicKey pgpSigningKey = readPublicKey(new ByteArrayInputStream(publicKey.getBytes())); ArmoredInputStream armoredInputStream = new ArmoredInputStream( new ByteArrayInputStream(signature.getBytes())); PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(armoredInputStream); Object nextObject = pgpObjectFactory.nextObject(); PGPSignature pgpSignature = null;/*w w w .j av a 2s. c o m*/ if (nextObject instanceof PGPSignatureList) { PGPSignatureList list = (PGPSignatureList) nextObject; if (!list.isEmpty()) { pgpSignature = list.get(0); } } if (pgpSignature == null) { return false; } try { pgpSignature.initVerify(pgpSigningKey, "BC"); } catch (NoSuchProviderException e) { logger.info("No security provider found. Adding bouncy castle", e); Security.addProvider(new BouncyCastleProvider()); pgpSignature.initVerify(pgpSigningKey, "BC"); } pgpSignature.update(signedData.getBytes()); return pgpSignature.verify(); }