PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher( new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv); PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher( new AESEngine())); CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
private byte[] decryptWithLWCrypto(byte[] cipher, String password, byte[] salt, final int iterationCount) throws Exception { PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA256Digest()); char[] passwordChars = password.toCharArray(); final byte[] pkcs12PasswordBytes = PBEParametersGenerator .PKCS12PasswordToBytes(passwordChars); pGen.init(pkcs12PasswordBytes, salt, iterationCount); CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine()); ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 128); aesCBC.init(false, aesCBCParams); PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding()); byte[] plainTemp = new byte[aesCipher.getOutputSize(cipher.length)]; int offset = aesCipher.processBytes(cipher, 0, cipher.length, plainTemp, 0); int last = aesCipher.doFinal(plainTemp, offset); final byte[] plain = new byte[offset + last]; System.arraycopy(plainTemp, 0, plain, 0, plain.length); return plain; }
public byte[] update(byte[] inp, int inpOff, int inpLen) { int neededLen = bp.getUpdateOutputSize(inpLen); byte[] outp = null; if (neededLen > 0) outp = new byte[neededLen]; else neededLen = 0; bp.processBytes(inp, inpOff, inpLen, outp, 0); return outp; }
public byte[] doFinal() { int neededLen = bp.getOutputSize(0); byte[] outp = new byte[neededLen]; int n = 0; try { n = bp.doFinal(outp, 0); } catch (Exception ex) { return outp; } if (n != outp.length) { byte[] outp2 = new byte[n]; System.arraycopy(outp, 0, outp2, 0, n); return outp2; } else return outp; }
_cipher.init(true, new KeyParameter(keyByte)); byte[] outBytes = new byte[_cipher.getOutputSize(inBytes.length)]; int len = _cipher.processBytes(inBytes, 0, inBytes.length, outBytes, 0); _cipher.doFinal(outBytes,len);
/** Creates a new instance of AESCipher */ public AESCipher(boolean forEncryption, byte[] key, byte[] iv) { BlockCipher aes = new AESFastEngine(); BlockCipher cbc = new CBCBlockCipher(aes); bp = new PaddedBufferedBlockCipher(cbc); KeyParameter kp = new KeyParameter(key); ParametersWithIV piv = new ParametersWithIV(kp, iv); bp.init(forEncryption, piv); }
withPadding.init(FOR_DECRYPTION, keyAndIV); int plaintextSize = withPadding.processBytes(ciphertext, 0, ciphertext.length, ciphertext, 0); plaintextSize += withPadding.doFinal(ciphertext, plaintextSize); final byte[] plaintext = Arrays.copyOf(ciphertext, plaintextSize);
RijndaelEngine rijndaelEngine = new RijndaelEngine(256); // *1 *2 KeyParameter keyParam = new KeyParameter(rijndaelKeyBytes) rijndaelEngine.init(false, keyParam); //false == decrypt PaddedBufferedBlockCipher bbc = new PaddedBufferedBlockCipher(rijndaelEngine, new ZeroBytePadding()); // *3 byte[] decryptedBytes = new byte[decryptedLenght]; //from the storageOptions string int processed = bbc.processBytes(inputBytes, 0, inputBytes.length, decryptedBytes, 0); bbc.doFinal(decryptedBytes, processed);
public InputDecryptor get(final AlgorithmIdentifier algorithmIdentifier) { final PaddedBufferedBlockCipher engine = PKCS12PBEUtils.getEngine(algorithmIdentifier.getAlgorithm()); PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algorithmIdentifier.getParameters()); CipherParameters params = PKCS12PBEUtils.createCipherParameters(algorithmIdentifier.getAlgorithm(), digest, engine.getBlockSize(), pbeParams, password); engine.init(false, params); return new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return algorithmIdentifier; } public InputStream getInputStream(InputStream input) { return new CipherInputStream(input, engine); } public GenericKey getKey() { return new GenericKey(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password)); } }; } };
PaddedBufferedBlockCipher c = new PaddedBufferedBlockCipher(new CFBBlockCipher(new RijndaelEngine(256), 8), new ZeroBytePadding()); c.init(false, new ParametersWithIV(new KeyParameter(keybytes), iv));
int blockSize = getBlockSize(); int length = getUpdateOutputSize(len);
pbbc.init(ENCRYPT, key); byte[] ciphertext = new byte[pbbc.getOutputSize(plaintext.length)]; offset += pbbc.processBytes(plaintext, 0, plaintext.length, ciphertext, offset); offset += pbbc.doFinal(ciphertext, offset); pbbc.init(DECRYPT, key); byte[] decrypted = new byte[pbbc.getOutputSize(ciphertext.length)]; offset = 0; offset += pbbc.processBytes(ciphertext, 0, ciphertext.length, decrypted, offset); offset += pbbc.doFinal(decrypted, offset);
/** * Create a decrypted input stream from an encrypted one */ public static InputStream getDecryptedInputStream (InputStream encryptedInputStream, byte[] keyData, byte[] ivData) { final ParametersWithIV keyAndIV = new ParametersWithIV(new KeyParameter(keyData), ivData); PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); pbbc.init(false, keyAndIV); return new CipherInputStream(encryptedInputStream, pbbc); }
try { while ((noBytesRead = bin.read(this.buf)) >= 0) { noBytesProcessed = this.encryptCipher.processBytes( this.buf, 0, noBytesProcessed = this.encryptCipher.doFinal(this.obuf, 0); bout.write(this.obuf, 0, noBytesProcessed); bout.flush();
public InputDecryptor get(final AlgorithmIdentifier algorithmIdentifier) { final PaddedBufferedBlockCipher engine = PKCS12PBEUtils.getEngine(algorithmIdentifier.getAlgorithm()); PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algorithmIdentifier.getParameters()); CipherParameters params = PKCS12PBEUtils.createCipherParameters(algorithmIdentifier.getAlgorithm(), digest, engine.getBlockSize(), pbeParams, password); engine.init(false, params); return new InputDecryptor() { public AlgorithmIdentifier getAlgorithmIdentifier() { return algorithmIdentifier; } public InputStream getInputStream(InputStream input) { return new CipherInputStream(input, engine); } public GenericKey getKey() { return new GenericKey(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password)); } }; } };
new CBCBlockCipher( new ThreefishEngine(ThreefishEngine.BLOCKSIZE_1024)), new PKCS7Padding()); cipher.init(encrypt, params); ByteArrayOutputStream baos = new ByteArrayOutputStream(); CipherOutputStream cos = new CipherOutputStream(baos, cipher);
int blockSize = getBlockSize(); int length = getUpdateOutputSize(len);
ZeroBytePadding c = new ZeroBytePadding(); PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(rijndael, c); pbbc.init(true, keyParameter); byte[] plaintext = data.getBytes(Charset.forName("UTF8")); byte[] ciphertext = new byte[pbbc.getOutputSize(plaintext.length)]; int offset = 0; offset += pbbc.processBytes(plaintext, 0, plaintext.length, ciphertext, offset); offset += pbbc.doFinal(ciphertext, offset); System.out.println("Encrypted: " + Base64.encodeBase64String(ciphertext));
protected BrokenJCEBlockCipher( BlockCipher engine, int pbeType, int pbeHash, int pbeKeySize, int pbeIvSize) { cipher = new PaddedBufferedBlockCipher(engine); this.pbeType = pbeType; this.pbeHash = pbeHash; this.pbeKeySize = pbeKeySize; this.pbeIvSize = pbeIvSize; }
/** * Create an encrypted output stream from an unencrypted output stream */ public static OutputStream getEncryptedOutputStream (OutputStream decryptedOutputStream, byte[] keyData, byte[] ivData) { final ParametersWithIV keyAndIV = new ParametersWithIV(new KeyParameter(keyData), ivData); PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); pbbc.init(true, keyAndIV); return new CipherOutputStream(decryptedOutputStream, pbbc); } }