List of usage examples for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher
public CBCBlockCipher(BlockCipher cipher)
From source file:org.ourfilesystem.security.SecurityTools.java
License:Open Source License
public static String decodePublicPost(BBytes enc, RSAPrivateCrtKeyParameters key) { try {/*from www . j a v a2 s .com*/ byte encdata[] = enc.getBytes(); //Extract the header ByteBuffer bbuf = ByteBuffer.wrap(encdata); int headlen = bbuf.getInt(); if (headlen > 4096) { throw new Exception("Header is too long: " + headlen); } byte head[] = new byte[headlen]; System.arraycopy(encdata, Integer.SIZE / Byte.SIZE, head, 0, headlen); //Decode the header RSAEngine eng = new RSAEngine(); PKCS1Encoding eng2 = new PKCS1Encoding(eng); eng2.init(false, key); byte dechead[] = eng2.processBlock(head, 0, head.length); if (dechead.length < (2 * Long.SIZE / Byte.SIZE)) { //Magic number can't be there, not enough data. return null; } ByteBuffer mbuf = ByteBuffer.wrap(dechead); long m0 = mbuf.getLong(); long m1 = mbuf.getLong(); if (m0 != 0x01234567) { return null; } if (m1 != 0x76543210) { return null; } //Exctract the symmetric key. byte skey[] = new byte[dechead.length - (2 * Long.SIZE / Byte.SIZE)]; System.arraycopy(dechead, 2 * Long.SIZE / Byte.SIZE, skey, 0, skey.length); KeyParameter kp = new KeyParameter(skey); CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(aes, new PKCS7Padding()); cipher.init(false, kp); int foffset = (Integer.SIZE / Byte.SIZE) + headlen; int enclen = encdata.length - foffset; byte[] output = new byte[cipher.getOutputSize(enclen)]; int len = cipher.processBytes(encdata, foffset, enclen, output, 0); int len2 = cipher.doFinal(output, len); byte rawdata[] = new byte[len + len2]; System.arraycopy(output, 0, rawdata, 0, rawdata.length); return new String(rawdata, Charset.forName("UTF-16BE")); } catch (Exception e) { //e.printStackTrace(); } return null; }
From source file:org.ourfilesystem.security.SecurityTools.java
License:Open Source License
public static BBytes encodePublicPost(String raw, PublicKeySet pubkey) { try {//from www. j a v a2 s. c om byte strb[] = raw.getBytes(Charset.forName("UTF-16BE")); byte[] key = new byte[32]; Random.nextBytes(key); KeyParameter kp = new KeyParameter(key); CBCBlockCipher aes = new CBCBlockCipher(new AESEngine()); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(aes, new PKCS7Padding()); cipher.init(true, kp); byte[] output = new byte[cipher.getOutputSize(strb.length)]; int len = cipher.processBytes(strb, 0, strb.length, output, 0); cipher.doFinal(output, len); RSAEngine eng = new RSAEngine(); byte headblk[] = new byte[(2 * Long.SIZE / Byte.SIZE) + key.length]; ByteBuffer bbuf = ByteBuffer.wrap(headblk); bbuf.putLong(0x01234567); //magic number bbuf.putLong(0x76543210); //magic number System.arraycopy(key, 0, headblk, (2 * Long.SIZE / Byte.SIZE), key.length); PKCS1Encoding enc = new PKCS1Encoding(eng); enc.init(true, (CipherParameters) pubkey.getPublicEncryptionKey()); byte enchead[] = enc.processBlock(headblk, 0, headblk.length); byte rslt[] = new byte[(Integer.SIZE / Byte.SIZE) + enchead.length + output.length]; ByteBuffer rbuf = ByteBuffer.wrap(rslt); rbuf.putInt(enchead.length); System.arraycopy(enchead, 0, rslt, Integer.SIZE / Byte.SIZE, enchead.length); System.arraycopy(output, 0, rslt, (Integer.SIZE / Byte.SIZE) + enchead.length, output.length); BBytes orslt = new BBytes(rslt); return orslt; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.pwsafe.lib.crypto.BlowfishPws.java
License:Open Source License
/** * Constructor, sets the initial vector to the value given. * * @param bfkey the encryption/decryption key. * @param ivBytes the initial vector./*from w w w .ja v a 2 s . co m*/ * @param cbc Use CBC mode (otherwise ECB is used). Normally this * should be true. */ BlowfishPws(byte[] bfkey, byte[] ivBytes, boolean cbc) { byte[] riv = Util.cloneByteArray(ivBytes); Util.bytesToLittleEndian(riv); KeyParameter dkp; KeyParameter ekp; if (cbc) { decipher = new CBCBlockCipher(new BlowfishEngine()); encipher = new CBCBlockCipher(new BlowfishEngine()); dkp = new KeyParameter(bfkey); ParametersWithIV div = new ParametersWithIV(dkp, riv); ekp = new KeyParameter(bfkey); ParametersWithIV eiv = new ParametersWithIV(ekp, riv); decipher.init(false, div); encipher.init(true, eiv); } else { /* ECB mode */ decipher = new BlowfishEngine(); encipher = new BlowfishEngine(); dkp = new KeyParameter(bfkey); ekp = new KeyParameter(bfkey); decipher.init(false, dkp); encipher.init(true, ekp); } }
From source file:org.pwsafe.lib.crypto.TwofishPws.java
License:Open Source License
public TwofishPws(byte[] key, boolean forEncryption, byte[] IV) { TwofishEngine tfe = new TwofishEngine(); cipher = new CBCBlockCipher(tfe); KeyParameter kp = new KeyParameter(key); ParametersWithIV piv = new ParametersWithIV(kp, IV); cipher.init(forEncryption, piv);/*from w ww .j a va2s. c o m*/ }
From source file:org.ScripterRon.BitcoinCore.EncryptedPrivateKey.java
License:Apache License
/** * Create a new EncryptedPrivateKey using the supplied private key and key phrase * * @param privKey Private key * @param keyPhrase Phrase used to derive the encryption key * @throws ECException Unable to complete a cryptographic function *//*from w w w. ja v a 2 s . c om*/ public EncryptedPrivateKey(BigInteger privKey, String keyPhrase) throws ECException { // // Derive the AES encryption key // salt = new byte[KEY_LENGTH]; secureRandom.nextBytes(salt); KeyParameter aesKey = deriveKey(keyPhrase, salt); // // Encrypt the private key using the generated AES key // try { iv = new byte[BLOCK_LENGTH]; secureRandom.nextBytes(iv); ParametersWithIV keyWithIV = new ParametersWithIV(aesKey, iv); CBCBlockCipher blockCipher = new CBCBlockCipher(new AESFastEngine()); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); cipher.init(true, keyWithIV); byte[] privKeyBytes = privKey.toByteArray(); int encryptedLength = cipher.getOutputSize(privKeyBytes.length); encKeyBytes = new byte[encryptedLength]; int length = cipher.processBytes(privKeyBytes, 0, privKeyBytes.length, encKeyBytes, 0); cipher.doFinal(encKeyBytes, length); } catch (Exception exc) { throw new ECException("Unable to encrypt the private key", exc); } }
From source file:org.ScripterRon.BitcoinCore.EncryptedPrivateKey.java
License:Apache License
/** * Returns the decrypted private key/* w w w. j a va2 s. c om*/ * * @param keyPhrase Key phrase used to derive the encryption key * @return Private key * @throws ECException Unable to complete a cryptographic function */ public BigInteger getPrivKey(String keyPhrase) throws ECException { KeyParameter aesKey = deriveKey(keyPhrase, salt); // // Decrypt the private key using the generated AES key // BigInteger privKey; try { ParametersWithIV keyWithIV = new ParametersWithIV(aesKey, iv); CBCBlockCipher blockCipher = new CBCBlockCipher(new AESFastEngine()); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); cipher.init(false, keyWithIV); int bufferLength = cipher.getOutputSize(encKeyBytes.length); byte[] outputBytes = new byte[bufferLength]; int length1 = cipher.processBytes(encKeyBytes, 0, encKeyBytes.length, outputBytes, 0); int length2 = cipher.doFinal(outputBytes, length1); int actualLength = length1 + length2; byte[] privKeyBytes = new byte[actualLength]; System.arraycopy(outputBytes, 0, privKeyBytes, 0, actualLength); privKey = new BigInteger(privKeyBytes); } catch (Exception exc) { throw new ECException("Unable to decrypt the private key", exc); } return privKey; }
From source file:org.sejda.sambox.encryption.AESEngineNoPadding.java
License:Apache License
/** * @return and instance of EncryptionAlgorithmEngine AES/CBC/NoPadding and no initialization vector *///from w w w .j a va2 s .c o m static AESEngineNoPadding cbc() { return new AESEngineNoPadding(new BufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()))); }
From source file:org.sejda.sambox.encryption.ConcatenatingAESEngine.java
License:Apache License
ConcatenatingAESEngine() { super(new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()))); random = new SecureRandom(); }
From source file:org.sejda.sambox.pdmodel.encryption.SecurityHandler.java
License:Apache License
/** * Encrypt or decrypt data with AES256./*from w w w .ja v a 2 s. c om*/ * * @param data The data to encrypt. * @param output The output to write the encrypted data to. * * @throws IOException If there is an error reading the data. */ private void decryptDataAES256(InputStream data, OutputStream output) throws IOException { byte[] iv = new byte[16]; // read IV from stream int ivSize = data.read(iv); if (ivSize == -1) { return; } if (ivSize != iv.length) { throw new IOException("AES initialization vector not fully read: only " + ivSize + " bytes read instead of " + iv.length); } PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(encryptionKey), iv)); try (CipherInputStream cis = new CipherInputStream(data, cipher)) { org.apache.commons.io.IOUtils.copy(cis, output); } }
From source file:org.sejda.sambox.pdmodel.encryption.StandardSecurityHandler.java
License:Apache License
private byte[] computeEncryptedKeyRev56(byte[] password, boolean isOwnerPassword, byte[] o, byte[] u, byte[] oe, byte[] ue, int encRevision) throws IOException { byte[] hash, fileKeyEnc; if (isOwnerPassword) { byte[] oKeySalt = new byte[8]; System.arraycopy(o, 40, oKeySalt, 0, 8); if (encRevision == 5) { hash = computeSHA256(password, oKeySalt, u); } else {/* w ww .ja v a2s .co m*/ hash = computeHash2A(password, oKeySalt, u); } fileKeyEnc = oe; } else { byte[] uKeySalt = new byte[8]; System.arraycopy(u, 40, uKeySalt, 0, 8); if (encRevision == 5) { hash = computeSHA256(password, uKeySalt, null); } else { hash = computeHash2A(password, uKeySalt, null); } fileKeyEnc = ue; } try { BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, new KeyParameter(hash)); byte[] buf = new byte[cipher.getOutputSize(fileKeyEnc.length)]; int len = cipher.processBytes(fileKeyEnc, 0, fileKeyEnc.length, buf, 0); len += cipher.doFinal(buf, len); return copyOf(buf, len); } catch (DataLengthException | IllegalStateException | InvalidCipherTextException e) { throw new IOException(e); } }