List of usage examples for org.bouncycastle.crypto.paddings PKCS7Padding PKCS7Padding
PKCS7Padding
From source file:org.cryptacular.util.CipherUtil.java
License:Open Source License
/** * Encrypts data using the given block cipher with PKCS5 padding. A {@link * CiphertextHeader} is prepended to the resulting ciphertext. * * @param cipher Block cipher./*from www .jav a 2s . co m*/ * @param key Encryption key. * @param nonce IV generator. Callers must take care to ensure that the * length of generated IVs is equal to the cipher block size. * @param data Plaintext data to be encrypted. * * @return Concatenation of encoded {@link CiphertextHeader} and encrypted * data that completely fills the returned byte array. */ public static byte[] encrypt(final BlockCipher cipher, final SecretKey key, final Nonce nonce, final byte[] data) { final byte[] iv = nonce.generate(); final byte[] header = new CiphertextHeader(iv).encode(); final PaddedBufferedBlockCipher padded = new PaddedBufferedBlockCipher(cipher, new PKCS7Padding()); padded.init(true, new ParametersWithIV(new KeyParameter(key.getEncoded()), iv)); return encrypt(new BufferedBlockCipherAdapter(padded), header, data); }
From source file:org.cryptacular.util.CipherUtil.java
License:Open Source License
/** * Encrypts data using the given block cipher with PKCS5 padding. A {@link * CiphertextHeader} is prepended to the resulting ciphertext. * * @param cipher Block cipher./*from w w w . j a v a2 s . c o m*/ * @param key Encryption key. * @param nonce IV generator. Callers must take care to ensure that the * length of generated IVs is equal to the cipher block size. * @param input Input stream containing plaintext data. * @param output Output stream that receives ciphertext produced by block * cipher in encryption mode. */ public static void encrypt(final BlockCipher cipher, final SecretKey key, final Nonce nonce, final InputStream input, final OutputStream output) { final byte[] iv = nonce.generate(); final byte[] header = new CiphertextHeader(iv).encode(); final PaddedBufferedBlockCipher padded = new PaddedBufferedBlockCipher(cipher, new PKCS7Padding()); padded.init(true, new ParametersWithIV(new KeyParameter(key.getEncoded()), iv)); writeHeader(header, output); process(new BufferedBlockCipherAdapter(padded), input, output); }
From source file:org.cryptacular.util.CipherUtil.java
License:Open Source License
/** * Decrypts data using the given block cipher with PKCS5 padding. * * @param cipher Block cipher./* w ww .ja va 2 s . com*/ * @param key Encryption key. * @param data Ciphertext data containing a prepended {@link * CiphertextHeader}. * * @return Decrypted data that completely fills the returned byte array. */ public static byte[] decrypt(final BlockCipher cipher, final SecretKey key, final byte[] data) { final CiphertextHeader header = CiphertextHeader.decode(data); final PaddedBufferedBlockCipher padded = new PaddedBufferedBlockCipher(cipher, new PKCS7Padding()); padded.init(false, new ParametersWithIV(new KeyParameter(key.getEncoded()), header.getNonce())); return decrypt(new BufferedBlockCipherAdapter(padded), data, header.getLength()); }
From source file:org.cryptacular.util.CipherUtil.java
License:Open Source License
/** * Decrypts data using the given block cipher with PKCS5 padding. * * @param cipher Block cipher./*w w w . j a v a2 s . c o m*/ * @param key Encryption key. * @param input Input stream containing a {@link CiphertextHeader} followed * by ciphertext data. * @param output Output stream that receives plaintext produced by block * cipher in decryption mode. */ public static void decrypt(final BlockCipher cipher, final SecretKey key, final InputStream input, final OutputStream output) { final CiphertextHeader header = CiphertextHeader.decode(input); final PaddedBufferedBlockCipher padded = new PaddedBufferedBlockCipher(cipher, new PKCS7Padding()); padded.init(false, new ParametersWithIV(new KeyParameter(key.getEncoded()), header.getNonce())); process(new BufferedBlockCipherAdapter(padded), input, output); }
From source file:org.fnppl.opensdx.security.SymmetricKey.java
License:Open Source License
public SymmetricKey(byte[] key_bytes, byte[] iv) { this.keyBytes = key_bytes; this.initVector = iv; CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine()); KeyParameter kp = new KeyParameter(keyBytes); ParametersWithIV aesCBCParams = new ParametersWithIV(kp, initVector); aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding()); aesCipher.init(true, aesCBCParams);//from w w w. jav a2 s . c om blockSize = aesCipher.getBlockSize(); }
From source file:org.fnppl.opensdx.security.SymmetricKey.java
License:Open Source License
public void decrypt(InputStream in, OutputStream out) throws Exception { // if(key.length!=initvector.length || key.length!=keybits/8) { // throw new Exception("invalid params"); // }// w ww . j av a2 s. c o m CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine()); KeyParameter kp = new KeyParameter(keyBytes); ParametersWithIV aesCBCParams = new ParametersWithIV(kp, initVector); PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding()); aesCipher.init(false, aesCBCParams); // aesCipher.init(true, aesCBCParams); //TODO pad block corrupted error when false. WHY?? int read = -1; byte[] buff = new byte[128 / 8];//blocksize while ((read = in.read(buff)) != -1) { byte[] ou = new byte[buff.length]; // System.err.println("read: "+read); int rg = aesCipher.processBytes(buff, 0, read, ou, 0); out.write(ou, 0, rg); // System.err.println("rg: "+rg); } buff = new byte[2 * 128 / 8];//blocksize read = aesCipher.doFinal(buff, 0); out.write(buff, 0, read); }
From source file:org.freenetproject.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 ww w . j av a2s . co m*/ 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()]; int read = 0; FileInputStream fis = new FileInputStream(rtsmessage); try { while (read < encrypted_params.length) { read += fis.read(encrypted_params, read, encrypted_params.length - read); if (read < 0) break; } if (read < 0) { fis.close(); 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) { fis.close(); 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; } try { aescipher.doFinal(plaintext, ptbytes); } catch (DataLengthException dle) { throw new InvalidCipherTextException(dle.getMessage()); } return plaintext; } finally { fis.close(); } }
From source file:org.jpedal.io.DecryptionFactory.java
License:Open Source License
/** * decode AES ecnoded data with IV parameters * @param password/* ww w . j ava 2 s.c om*/ * @param encKey * @param encData a data gained from deducting IV bytes in beginning (encData = data - ivBytes) * @param ivData * @return * @throws Exception */ private static byte[] decodeAES(final byte[] encKey, final byte[] encData, final byte[] ivData) throws Exception { final KeyParameter keyParam = new KeyParameter(encKey); final CipherParameters params = new ParametersWithIV(keyParam, ivData); // setup AES cipher in CBC mode with PKCS7 padding final BlockCipherPadding padding = new PKCS7Padding(); final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding); cipher.reset(); cipher.init(false, params); // create a temporary buffer to decode into (it'll include padding) final byte[] buf = new byte[cipher.getOutputSize(encData.length)]; int len = cipher.processBytes(encData, 0, encData.length, buf, 0); len += cipher.doFinal(buf, len); // remove padding final byte[] out = new byte[len]; System.arraycopy(buf, 0, out, 0, len); // return string representation of decoded bytes return out; }
From source file:org.jpedal.io.security.BouncyCastleDecryption.java
License:Open Source License
@Override public byte[] decodeAES(final byte[] encKey, final byte[] encData, final byte[] ivData) throws Exception { final KeyParameter keyParam = new KeyParameter(encKey); final CipherParameters params = new ParametersWithIV(keyParam, ivData); // setup AES cipher in CBC mode with PKCS7 padding final BlockCipherPadding padding = new PKCS7Padding(); final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);/*from w w w . j ava2 s. c om*/ cipher.reset(); cipher.init(false, params); // create a temporary buffer to decode into (it'll include padding) final byte[] buf = new byte[cipher.getOutputSize(encData.length)]; int len = cipher.processBytes(encData, 0, encData.length, buf, 0); len += cipher.doFinal(buf, len); // remove padding final byte[] out = new byte[len]; System.arraycopy(buf, 0, out, 0, len); // return string representation of decoded bytes return out; }
From source file:org.ourfilesystem.security.SecurityTools.java
License:Open Source License
public static String decodePublicPost(BBytes enc, RSAPrivateCrtKeyParameters key) { try {/*w w w . j a v a 2s .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; }