List of usage examples for org.bouncycastle.crypto.engines AESEngine AESEngine
public AESEngine()
From source file:ECToken3.java
License:Open Source License
public static final String encryptv3(String key, String input) throws java.io.UnsupportedEncodingException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException, java.security.InvalidAlgorithmParameterException { //System.out.format("+-------------------------------------------------------------\n"); //System.out.format("| Encrypt\n"); //System.out.format("+-------------------------------------------------------------\n"); //System.out.format("| key: %s\n", key); //System.out.format("| token: %s\n", input); //---------------------------------------------------- // Get SHA-256 of key //---------------------------------------------------- MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(key.getBytes("ASCII")); byte[] keyDigest = md.digest(); //---------------------------------------------------- // Get Random IV //---------------------------------------------------- SecureRandom random = new SecureRandom(); byte[] ivBytes = new byte[12]; random.nextBytes(ivBytes);// w ww . j a v a 2s . c om //---------------------------------------------------- // Encrypt //---------------------------------------------------- AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); cipher.init(true, new AEADParameters(new KeyParameter(keyDigest), MAC_SIZE_BITS, ivBytes)); byte[] inputBytes = input.getBytes("ASCII"); byte[] enc = new byte[cipher.getOutputSize(inputBytes.length)]; try { int res = cipher.processBytes(inputBytes, 0, inputBytes.length, enc, 0); cipher.doFinal(enc, res); } catch (Exception e) { throw new RuntimeException(e); } byte[] ivPlusCipherText = new byte[ivBytes.length + enc.length]; System.arraycopy(ivBytes, 0, ivPlusCipherText, 0, ivBytes.length); System.arraycopy(enc, 0, ivPlusCipherText, ivBytes.length, enc.length); //System.out.format("+-------------------------------------------------------------\n"); //System.out.format("| iv: %s\n", bytesToHex(ivBytes)); //System.out.format("| ciphertext: %s\n", bytesToHex(Arrays.copyOfRange(enc, 0, enc.length - 16))); //System.out.format("| tag: %s\n", bytesToHex(Arrays.copyOfRange(enc, enc.length - 16, enc.length))); //System.out.format("+-------------------------------------------------------------\n"); //System.out.format("| token: %s\n", bytesToHex(ivPlusCipherText)); //System.out.format("+-------------------------------------------------------------\n"); String result = null; byte[] temp = null; Base64 encoder = new Base64(0, temp, true); byte[] encodedBytes = encoder.encode(ivPlusCipherText); String encodedStr = new String(encodedBytes, "ASCII").trim(); String encodedStrTrim = encodedStr.trim(); return encodedStr.trim(); }
From source file:ECToken3.java
License:Open Source License
public static final String decryptv3(String key, String input) throws java.io.UnsupportedEncodingException, java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException, javax.crypto.BadPaddingException, java.security.InvalidAlgorithmParameterException { //---------------------------------------------------- // Base64 decode //---------------------------------------------------- String result = null;//from w ww. java 2 s . c o m Base64 encoder = new Base64(true); byte[] inputBytes = encoder.decode(input.getBytes("ASCII")); //---------------------------------------------------- // Get SHA-256 of key //---------------------------------------------------- MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(key.getBytes("ASCII")); byte[] keyDigest = md.digest(); //System.out.format("+-------------------------------------------------------------\n"); //System.out.format("| Decrypt\n"); //System.out.format("+-------------------------------------------------------------\n"); //System.out.format("| key: %s\n", key); //System.out.format("| token: %s\n", input); //---------------------------------------------------- // Rip up the ciphertext //---------------------------------------------------- byte[] ivBytes = new byte[12]; ivBytes = Arrays.copyOfRange(inputBytes, 0, ivBytes.length); byte[] cipherBytes = new byte[inputBytes.length - ivBytes.length]; cipherBytes = Arrays.copyOfRange(inputBytes, ivBytes.length, inputBytes.length); //---------------------------------------------------- // Decrypt //---------------------------------------------------- AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); cipher.init(false, new AEADParameters(new KeyParameter(keyDigest), MAC_SIZE_BITS, ivBytes)); //System.out.format("+-------------------------------------------------------------\n"); //System.out.format("| iv: %s\n", bytesToHex(ivBytes)); //System.out.format("| ciphertext: %s\n", bytesToHex(Arrays.copyOfRange(cipherBytes, 0, cipherBytes.length - 16))); //System.out.format("| tag: %s\n", bytesToHex(Arrays.copyOfRange(cipherBytes, cipherBytes.length - 16, cipherBytes.length))); //System.out.format("+-------------------------------------------------------------\n"); byte[] dec = new byte[cipher.getOutputSize(cipherBytes.length)]; try { int res = cipher.processBytes(cipherBytes, 0, cipherBytes.length, dec, 0); cipher.doFinal(dec, res); } catch (Exception e) { throw new RuntimeException(e); } //System.out.format("token: %s\n", new String(dec, "ASCII")); return new String(dec, "ASCII"); }
From source file:cc.agentx.security.AesCipher.java
License:Apache License
/** * <b>Notice: </b><br/> * 1. the <code>AESFastEngine</code> was replaced by <code>AESEngine</code> now.<br/> * 2. in <code>new CFBBlockCipher(engine, <b>16</b> * 8);</code> the IV length (16) is * reference to the shadowsocks's design. * * @see <a href="https://www.bouncycastle.org/releasenotes.html"> * https://www.bouncycastle.org/releasenotes.html</a>#CVE-2016-1000339<br/> * <a href="https://shadowsocks.org/en/spec/cipher.html"> * https://shadowsocks.org/en/spec/cipher.html</a>#Cipher *///from w w w . jav a2s. c om public AesCipher(String password, int mode) { key = new SecretKeySpec(password.getBytes(), "AES"); keyLength = Math.abs(mode); AESEngine engine = new AESEngine(); if (mode > 0) { cipher = new CFBBlockCipher(engine, 16 * 8); } else { cipher = new OFBBlockCipher(engine, 16 * 8); } }
From source file:ch.dissem.bitmessage.cryptography.bc.BouncyCryptography.java
License:Apache License
@Override public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) { BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector); cipher.init(encrypt, params);/*from w w w . j a va2 s . c o m*/ byte[] buffer = new byte[cipher.getOutputSize(data.length)]; int length = cipher.processBytes(data, 0, data.length, buffer, 0); try { length += cipher.doFinal(buffer, length); } catch (InvalidCipherTextException e) { throw new IllegalArgumentException(e); } if (length < buffer.length) { return Arrays.copyOfRange(buffer, 0, length); } return buffer; }
From source file:co.lqnt.lockbox.Cipher.java
License:Open Source License
/** * Construct a new bi-directional cipher. */// w w w . ja va 2s.c o m public Cipher() { CodecInterface base64UriCodec = new Base64UriCodec(); AsymmetricBlockCipher rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest()); BufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); Digest sha1Digest = new SHA1Digest(); SecureRandom random = new SecureRandom(); this.encryptionCipher = new EncryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest, random); this.decryptionCipher = new DecryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest); }
From source file:co.lqnt.lockbox.DecryptionCipher.java
License:Open Source License
/** * Construct a new decryption cipher.// w w w . ja va 2s.c om */ public DecryptionCipher() { this.base64UriCodec = new Base64UriCodec(); this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest()); this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); this.sha1Digest = new SHA1Digest(); this.asciiCharset = Charset.forName("US-ASCII"); }
From source file:co.lqnt.lockbox.EncryptionCipher.java
License:Open Source License
/** * Construct a new encryption cipher./*from w w w .java 2 s. c o m*/ */ public EncryptionCipher() { this.base64UriCodec = new Base64UriCodec(); this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest()); this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); this.sha1Digest = new SHA1Digest(); this.random = new SecureRandom(); this.asciiCharset = Charset.forName("US-ASCII"); }
From source file:co.rsk.crypto.KeyCrypterAes.java
License:Open Source License
/** * Password based encryption using AES - CBC 256 bits. *//*ww w. j a v a2 s . c o m*/ @Override public EncryptedData encrypt(byte[] plainBytes, KeyParameter key) { checkNotNull(plainBytes); checkNotNull(key); try { // Generate iv - each encryption call has a different iv. byte[] iv = new byte[BLOCK_LENGTH]; secureRandom.nextBytes(iv); ParametersWithIV keyWithIv = new ParametersWithIV(key, iv); // Encrypt using AES. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(true, keyWithIv); byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)]; final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0); final int length2 = cipher.doFinal(encryptedBytes, length1); return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2)); } catch (Exception e) { throw new KeyCrypterException("Could not encrypt bytes.", e); } }
From source file:co.rsk.crypto.KeyCrypterAes.java
License:Open Source License
/** * Decrypt bytes previously encrypted with this class. * * @param dataToDecrypt The data to decrypt * @param key The AES key to use for decryption * @return The decrypted bytes * @throws KeyCrypterException if bytes could not be decrypted *///from w ww . j av a 2 s. co m @Override public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter key) { checkNotNull(dataToDecrypt); checkNotNull(key); try { ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(key.getKey()), dataToDecrypt.initialisationVector); // Decrypt the message. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(false, keyWithIv); byte[] cipherBytes = dataToDecrypt.encryptedBytes; byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)]; final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0); final int length2 = cipher.doFinal(decryptedBytes, length1); return Arrays.copyOf(decryptedBytes, length1 + length2); } catch (Exception e) { throw new KeyCrypterException("Could not decrypt bytes", e); } }
From source file:cologne.eck.peafactory.gui.Menu.java
License:Open Source License
@Override public void actionPerformed(ActionEvent ape) { //JComponent source = (JComponent) ape.getSource(); String command = ape.getActionCommand(); //Menu/*from www. java 2 s . c o m*/ if (command.equals("newProject")) { ProjectSelection proj = new ProjectSelection(); Point p = MainView.getFrameLocation(); proj.setLocation((int) p.getX() + 100, (int) p.getY() + 60); proj.setVisible(true); } else if (command.equals("randomPassword")) { PasswordGeneratorDialog pg = new PasswordGeneratorDialog(PeaFactory.getFrame()); pg.setVisible(true); } else if (command.equals("keyboard")) { int input = JOptionPane.showConfirmDialog(PeaFactory.getFrame(), languageBundle.getString("add_keyboard"), " ", JOptionPane.YES_NO_OPTION); if (input == 0) { FileModifier.setSetKeyboard(true); } else { FileModifier.setSetKeyboard(false); } } else if (command.equals("psw_generator")) { int input = JOptionPane.showConfirmDialog(PeaFactory.getFrame(), languageBundle.getString("add_psw_generator"), " ", JOptionPane.YES_NO_OPTION); if (input == 0) { FileModifier.setPswGenerator(true); } else { FileModifier.setPswGenerator(false); } } else if (command.equals("quit")) { System.exit(0); } else if (command.equals("generalPeaSettings")) { @SuppressWarnings("unused") GeneralPeaSettings imageSetting = new GeneralPeaSettings(); } else if (command.equals("setThoughtless")) { securityLevel = 1; setSecurityLevel(1); } else if (command.equals("setLow")) { securityLevel = 2; setSecurityLevel(2); } else if (command.equals("setStandard")) { securityLevel = 3; setSecurityLevel(3); } else if (command.equals("setHigh")) { securityLevel = 4; setSecurityLevel(4); } else if (command.equals("setParanoid")) { securityLevel = 5; setSecurityLevel(5); } else if (command.equals("setBcrypt")) { setSecurityLevel(securityLevel); KeyDerivation.setKdf(new BcryptKDF()); } else if (command.equals("setScrypt")) { setSecurityLevel(securityLevel); KeyDerivation.setKdf(new ScryptKDF()); } else if (command.equals("setDragonfly")) { setSecurityLevel(securityLevel); CatenaKDF.setVersionID("Dragonfly-Full"); KeyDerivation.setKdf(new CatenaKDF()); } else if (command.equals("setButterfly")) { setSecurityLevel(securityLevel); CatenaKDF.setVersionID("Butterfly-Full"); KeyDerivation.setKdf(new CatenaKDF()); } else if (command.equals("setPomelo")) { setSecurityLevel(securityLevel); KeyDerivation.setKdf(new PomeloKDF()); } else if (command.equals("setBcryptParameters")) { @SuppressWarnings("unused") BcryptSetting bcryptSetting = new BcryptSetting(); } else if (command.equals("setPomeloParameters")) { @SuppressWarnings("unused") PomeloSetting pomeloSetting = new PomeloSetting(); } else if (command.equals("setScryptParameters")) { @SuppressWarnings("unused") ScryptSetting scryptSetting = new ScryptSetting(); } else if (command.equals("setCatenaParameters")) { @SuppressWarnings("unused") CatenaSetting catenaSetting = new CatenaSetting(); } else if (command.equals("setImageParameters")) { @SuppressWarnings("unused") ImageSetting imageSetting = new ImageSetting(); } else if (command.equals("setShacal2")) { CipherStuff.setCipherAlgo(new Shacal2Engine()); } else if (command.equals("setThreefish256")) { CipherStuff.setCipherAlgo(new ThreefishEngine(256)); } else if (command.equals("setThreefish512")) { CipherStuff.setCipherAlgo(new ThreefishEngine(512)); } else if (command.equals("setThreefish1024")) { CipherStuff.setCipherAlgo(new ThreefishEngine(1024)); } else if (command.equals("setTwofish")) { CipherStuff.setCipherAlgo(new TwofishEngine()); } else if (command.equals("setSerpent")) { CipherStuff.setCipherAlgo(new SerpentEngine()); } else if (command.equals("setAES")) { CipherStuff.setCipherAlgo(new AESEngine()); } else if (command.equals("setAESFast")) { CipherStuff.setCipherAlgo(new AESFastEngine()); // hash function: } else if (command.equals("setWhirlpool")) { HashStuff.setHashAlgo(new WhirlpoolDigest()); } else if (command.equals("setKeccak")) { HashStuff.setHashAlgo(new SHA3Digest()); } else if (command.equals("setSha512")) { HashStuff.setHashAlgo(new SHA512Digest()); } else if (command.equals("setSha384")) { HashStuff.setHashAlgo(new SHA384Digest()); } else if (command.equals("setSkein512")) { HashStuff.setHashAlgo(new SkeinDigest(512, 512)); } else if (command.equals("setBlake512")) { HashStuff.setHashAlgo(new Blake2bDigest()); // } else if (command.equals("setRipemd256")) { // HashStuff.setHashAlgo( new RIPEMD256Digest() ); } else if (command.equals("setRipemd320")) { HashStuff.setHashAlgo(new RIPEMD320Digest()); } else if (command.equals("setDE")) { PeaFactory.setI18n("de"); } else if (command.equals("setEN")) { PeaFactory.setI18n("en"); } else if (command.equals("notes")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog(languageBundle.getString("notes_description"), null, "notes"); } else if (command.equals("editor")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog(languageBundle.getString("editor_description"), null, "editor"); } else if (command.equals("image")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog(languageBundle.getString("image_description"), null, "image"); } else if (command.equals("keyboard_info")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog("Onscreen Keyboard", null, "keyboard"); } else if (command.equals("file")) { @SuppressWarnings("unused") InfoDialog info = new InfoDialog(languageBundle.getString("file_description"), null, "file"); } else if (command.equals("problemHelp")) { JOptionPane pane = new JOptionPane(languageBundle.getString("problem_help_dialog"), JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_OPTION, null, null);//new ImageIcon(PswDialogView.getImage()), null); pane.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12)); //pane.setIconImage(PswDialogView.getImage()); pane.setVisible(true); //pane.showMessageDialog(null, languageBundle.getString("problem_help_dialog"), null, JOptionPane.PLAIN_MESSAGE); } else if (command.equals("howToUse")) { JOptionPane.showMessageDialog(PeaFactory.getFrame(), languageBundle.getString("how_to_use_dialog"), null, JOptionPane.PLAIN_MESSAGE); } else if (command.equals("aboutLicense")) { JOptionPane.showMessageDialog(PeaFactory.getFrame(), languageBundle.getString("about_license_dialog"), null, JOptionPane.PLAIN_MESSAGE); } }