List of usage examples for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher processBytes
public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) throws DataLengthException, IllegalStateException
From source file:freemail.RTSFetcher.java
License:Open Source License
private byte[] decrypt_rts(File rtsmessage) throws IOException, InvalidCipherTextException { // initialise our ciphers RSAKeyParameters ourprivkey = AccountManager.getPrivateKey(account.getProps()); AsymmetricBlockCipher deccipher = new RSAEngine(); deccipher.init(false, ourprivkey);//from w ww.ja v a 2s . com PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); // first n bytes will be an encrypted RSA block containting the // AES IV and Key. Read that. byte[] encrypted_params = new byte[deccipher.getInputBlockSize()]; FileInputStream fis = new FileInputStream(rtsmessage); int read = 0; while (read < encrypted_params.length) { read += fis.read(encrypted_params, read, encrypted_params.length - read); if (read < 0) break; } if (read < 0) { throw new InvalidCipherTextException("RTS Message too short"); } byte[] aes_iv_and_key = deccipher.processBlock(encrypted_params, 0, encrypted_params.length); KeyParameter kp = new KeyParameter(aes_iv_and_key, aescipher.getBlockSize(), aes_iv_and_key.length - aescipher.getBlockSize()); ParametersWithIV kpiv = new ParametersWithIV(kp, aes_iv_and_key, 0, aescipher.getBlockSize()); try { aescipher.init(false, kpiv); } catch (IllegalArgumentException iae) { throw new InvalidCipherTextException(iae.getMessage()); } byte[] plaintext = new byte[aescipher.getOutputSize((int) rtsmessage.length() - read)]; int ptbytes = 0; while (read < rtsmessage.length()) { byte[] buf = new byte[(int) rtsmessage.length() - read]; int thisread = fis.read(buf, 0, (int) rtsmessage.length() - read); ptbytes += aescipher.processBytes(buf, 0, thisread, plaintext, ptbytes); read += thisread; } fis.close(); try { aescipher.doFinal(plaintext, ptbytes); } catch (DataLengthException dle) { throw new InvalidCipherTextException(dle.getMessage()); } return plaintext; }
From source file:heat.crypto.Crypto.java
License:Open Source License
public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) { try {// ww w. j av a 2 s . com byte[] dhSharedSecret = new byte[32]; Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey); for (int i = 0; i < 32; i++) { dhSharedSecret[i] ^= nonce[i]; } byte[] key = sha256().digest(dhSharedSecret); byte[] iv = new byte[16]; secureRandom.get().nextBytes(iv); PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(true, ivAndKey); byte[] output = new byte[aes.getOutputSize(plaintext.length)]; int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0); ciphertextLength += aes.doFinal(output, ciphertextLength); byte[] result = new byte[iv.length + ciphertextLength]; System.arraycopy(iv, 0, result, 0, iv.length); System.arraycopy(output, 0, result, iv.length, ciphertextLength); return result; } catch (InvalidCipherTextException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:heat.crypto.Crypto.java
License:Open Source License
public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) { try {/*from w ww .j av a 2 s . c om*/ if (ivCiphertext.length < 16 || ivCiphertext.length % 16 != 0) { throw new InvalidCipherTextException("invalid ciphertext"); } byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16); byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length); byte[] dhSharedSecret = new byte[32]; Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey); for (int i = 0; i < 32; i++) { dhSharedSecret[i] ^= nonce[i]; } byte[] key = sha256().digest(dhSharedSecret); PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); aes.init(false, ivAndKey); byte[] output = new byte[aes.getOutputSize(ciphertext.length)]; int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0); plaintextLength += aes.doFinal(output, plaintextLength); byte[] result = new byte[plaintextLength]; System.arraycopy(output, 0, result, 0, result.length); return result; } catch (InvalidCipherTextException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:jcrypter.JCrypterFrame.java
License:Apache License
private void decryptWithBypass() //TODO this is only a test, review!! { try //TODO this is only a test, review!! {// ww w . ja v a2 s. c o m PaddedBufferedBlockCipher bc = new PaddedBufferedBlockCipher(new TwofishEngine(), new PKCS7Padding()); int bs = bc.getBlockSize(); //Blocksize //Get data byte[] passwordBytes = new String(passwordField.getPassword()).getBytes(); byte[] input; //Base64-decode the ciphertext input = Base64.decode(inputField.getText().getBytes()); //All data will be read from this stream ByteArrayInputStream bin = new ByteArrayInputStream(input); //Hash the password to fit it into the right size (with salt) byte[] salt = new byte[8]; byte[] keyBytes = new byte[32]; bin.read(salt); //Get the salt from the input stream Digest digest = new SHA256Digest(); digest.update(salt, 0, salt.length); //Add the salt... digest.update(passwordBytes, 0, passwordBytes.length); //...and the password to the generator digest.doFinal(keyBytes, 0); //Do the final hashing //IV generation/retrievement byte[] iv = new byte[cipher.getBlockSize()]; //Using iv array only with offset bin.read(iv); ByteArrayOutputStream bout = new ByteArrayOutputStream(); /* * IMPORTANT NOTE: * This implementation bypasses the policy but does only support TWOFISH/ECB */ CipherParameters params = new ParametersWithIV(new KeyParameter(keyBytes), iv); bc.init(false, params); byte[] plaintext = new byte[bc.getOutputSize(bin.available())]; //All at once byte[] buffer = new byte[bs]; byte[] oBuffer = new byte[bs]; //OutputBuffer while (bin.available() > 0) { int read = bin.read(buffer); bc.processBytes(buffer, 0, read, oBuffer, 0); bout.write(oBuffer); } outputField.setText(new String(bout.toByteArray())); } catch (IOException ex) { Logger.getLogger(JCrypterFrame.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:jcrypter.JCrypterFrame.java
License:Apache License
private void encryptWithBypass() //TODO this is only a test, review!! { try //TODO this is only a test, review!! {/* w ww. j a v a 2 s .c o m*/ PaddedBufferedBlockCipher bc = new PaddedBufferedBlockCipher(new TwofishEngine(), new PKCS7Padding()); //Using BouncyCastle JCEs int bs = cipher.getBlockSize(); //Blocksize //Get data byte[] passwordBytes = new String(passwordField.getPassword()).getBytes(); byte[] input; //Base64-decode the ciphertext input = inputField.getText().getBytes(); ByteArrayInputStream bin = new ByteArrayInputStream(input); //All data is written to this stream ByteArrayOutputStream bout = new ByteArrayOutputStream(); //Hash the password to fit it into the right size (with salt) byte[] salt = new byte[8]; byte[] keyBytes = new byte[32]; //Assume a 256-bit key rand.nextBytes(salt); bout.write(salt); //Write the salt to the stream Digest digest = new SHA256Digest(); //Assume a 256-bit key digest.update(salt, 0, salt.length); //Add the salt... digest.update(passwordBytes, 0, passwordBytes.length); //...and the password to the generator digest.doFinal(keyBytes, 0); //Do the final hashing //Generate the iv and the IvParameter spec byte[] iv = new byte[cipher.getBlockSize()]; rand.nextBytes(iv); /* * IMPORTANT NOTE: * This implementation bypasses the policy but does only support TWOFISH/ECB */ CipherParameters params = new ParametersWithIV(new KeyParameter(keyBytes), iv); bc.init(true, params); byte[] plaintext = new byte[bc.getOutputSize(bin.available())]; //All at once byte[] buffer = new byte[bs]; byte[] oBuffer = new byte[bs]; //OutputBuffer while (bin.available() > 0) { int read = bin.read(buffer); bc.processBytes(buffer, 0, read, oBuffer, 0); bout.write(oBuffer); } outputField.setText(new String(bout.toByteArray())); } catch (IOException ex) { Logger.getLogger(JCrypterFrame.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:my.adam.smo.common.SymmetricEncryptionBox.java
License:Open Source License
public byte[] encrypt(byte[] plainText, byte[] key) { if (key.length != 32) { throw new IllegalArgumentException("key have to be 32 bytes long (256 bits)"); }//from ww w. j ava2s . co m byte[] seed = new byte[seedLength]; secureRandom.nextBytes(seed); byte[] seededPlainText = addSeedToMessage(plainText, seed); byte[] out = seededPlainText.clone(); byte[] iv = new byte[ivLength]; secureRandom.nextBytes(iv); CipherParameters cp = new ParametersWithIV(new KeyParameter(key), iv); PaddedBufferedBlockCipher encCipher; encCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()), new ISO7816d4Padding()); encCipher.init(true, cp); encCipher.processBytes(seededPlainText, 0, seededPlainText.length, out, 0); return appendIV(out, iv); }
From source file:my.adam.smo.common.SymmetricEncryptionBox.java
License:Open Source License
public byte[] decrypt(byte[] cryptogram, byte[] key) { if (key.length != 32) { throw new IllegalArgumentException("key have to be 16 bytes long (32 bits)"); }/*from www .j a v a 2s . c o m*/ byte[] out = Arrays.copyOfRange(cryptogram, ivLength, cryptogram.length); CipherParameters cp = new ParametersWithIV(new KeyParameter(key), getIV(cryptogram)); PaddedBufferedBlockCipher descCipher; descCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); descCipher.init(false, cp); descCipher.processBytes(cryptogram, ivLength, cryptogram.length - ivLength, out, 0); return getMessageWithoutSeed(out); }
From source file:org.albertschmitt.crypto.AESService.java
License:Open Source License
/** * Encode the byte data to AES256 and return it in byte array. * * @param data Byte array to be encoded. * @return AES256 encoded byte array of input data. * @throws org.bouncycastle.crypto.InvalidCipherTextException * @see #decode(byte[] data)// ww w. j av a 2 s .c om */ public byte[] encode(byte[] data) throws InvalidCipherTextException { byte[] iv = new byte[IV_LENGTH]; SecureRandom secure = new SecureRandom(); secure.nextBytes(iv); PaddedBufferedBlockCipher cipher = getCipher(iv, true); int outSize = cipher.getOutputSize(data.length); byte[] enc = new byte[outSize]; int length1 = cipher.processBytes(data, 0, data.length, enc, 0); cipher.doFinal(enc, length1); byte[] encrypted = ByteUtil.concatenate(iv, enc); return encrypted; }
From source file:org.albertschmitt.crypto.AESService.java
License:Open Source License
/** * Decode the AES256 encoded byte data and return it in an byte array. * * @param data AES256 encoded byte array. * @return Decoded byte array of AES256 encoded input data. * @throws org.bouncycastle.crypto.InvalidCipherTextException * @see #encode(byte[] data)//from ww w . ja va2s . c o m */ public byte[] decode(byte[] data) throws InvalidCipherTextException { byte[] iv = new byte[IV_LENGTH]; System.arraycopy(data, 0, iv, 0, IV_LENGTH); PaddedBufferedBlockCipher cipher = getCipher(iv, false); int outSize = cipher.getOutputSize(data.length - IV_LENGTH); byte[] dec = new byte[outSize]; int count = cipher.processBytes(data, iv.length, data.length - IV_LENGTH, dec, 0); count += cipher.doFinal(dec, count); // Remove padding byte[] out = new byte[count]; System.arraycopy(dec, 0, out, 0, count); return out; }
From source file:org.bigmouth.nvwa.utils.degist.AesUtils.java
License:Apache License
/** * Encrypt or decrypt data./*ww w . j av a 2s. c o m*/ * * @param cipher * @param data * @return * @throws Exception */ private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data) throws Exception { int minSize = cipher.getOutputSize(data.length); byte[] outBuf = new byte[minSize]; int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0); int length2 = cipher.doFinal(outBuf, length1); int actualLength = length1 + length2; byte[] result = new byte[actualLength]; System.arraycopy(outBuf, 0, result, 0, result.length); return result; }