List of usage examples for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher PaddedBufferedBlockCipher
public PaddedBufferedBlockCipher(BlockCipher cipher)
From source file:com.thoughtworks.go.security.GoCipher.java
License:Apache License
String decipher(byte[] key, String cipherText) throws InvalidCipherTextException { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); cipher.init(false, new KeyParameter(Hex.decode(key))); byte[] cipherTextBytes = Base64.decodeBase64(cipherText); byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)]; int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0); cipher.doFinal(plainTextBytes, outputLength); int paddingStarts = plainTextBytes.length - 1; for (; paddingStarts >= 0; paddingStarts--) { if (plainTextBytes[paddingStarts] != 0) { break; }//from w w w. j av a 2 s . c om } return new String(plainTextBytes, 0, paddingStarts + 1); }
From source file:com.wlami.mibox.core.encryption.AesEncryption.java
License:Open Source License
/** * Encrypts and decrypts a byte array.//from w ww . j av a 2s .c om * * @param encrypt * <code>true</code> for encryption <br/> * <code>false</code> for decryption * @param ciphertext * the data which shall be decrypted or encrypted * @param initVector * the iv * @param key * the key * @return an encrypted /decrypted byte array * @throws CryptoException */ public static byte[] crypt(boolean encrypt, byte[] ciphertext, byte[] initVector, byte[] key) throws InvalidCipherTextException { log.debug("starting " + (encrypt ? "encryption" : "decryption")); BlockCipher engine = new AESEngine(); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine)); log.debug("retrieved an AESEngine"); cipher.init(encrypt, new ParametersWithIV(new KeyParameter(key), initVector)); byte[] cipherArray = new byte[cipher.getOutputSize(ciphertext.length)]; log.debug("creatied cipherArray with size " + cipherArray.length + "\n encryption..."); int outputByteCount = cipher.processBytes(ciphertext, 0, ciphertext.length, cipherArray, 0); log.debug("finalizing cipher"); outputByteCount += cipher.doFinal(cipherArray, outputByteCount); byte[] result = new byte[outputByteCount]; System.arraycopy(cipherArray, 0, result, 0, outputByteCount); return result; }
From source file:com.zotoh.crypto.BCOfuscator.java
License:Open Source License
private String decrypt(String encrypted) throws Exception { if (isEmpty(encrypted)) { return encrypted; }//from ww w . ja v a2 s .co m PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); byte[] p = Base64.decodeBase64(encrypted), out = new byte[1024]; ByteOStream baos = new ByteOStream(); int c; // initialise the cipher with the key bytes, for encryption cipher.init(false, new KeyParameter(getKey())); c = cipher.processBytes(p, 0, p.length, out, 0); if (c > 0) { baos.write(out, 0, c); } c = cipher.doFinal(out, 0); if (c > 0) { baos.write(out, 0, c); } return asString(baos.asBytes()); }
From source file:com.zotoh.crypto.BCOfuscator.java
License:Open Source License
private String encrypt(String clearText) throws Exception { if (isEmpty(clearText)) { return clearText; }// www .jav a 2 s .com PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); ByteOStream baos = new ByteOStream(); byte[] p = asBytes(clearText), out = new byte[4096]; int c; // initialise the cipher with the key bytes, for encryption cipher.init(true, new KeyParameter(getKey())); c = cipher.processBytes(p, 0, p.length, out, 0); if (c > 0) { baos.write(out, 0, c); } c = cipher.doFinal(out, 0); if (c > 0) { baos.write(out, 0, c); } return Base64.encodeBase64String(baos.asBytes()); }
From source file:dbn.crypto.Crypto.java
public static byte[] aesEncrypt(byte[] plaintext, byte[] key) { try {//from w w w. jav a 2 s .c o m 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:dbn.crypto.Crypto.java
public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] key) { try {//from www.jav a2s. c o m if (ivCiphertext.length < 16 || ivCiphertext.length % 16 != 0) { throw new InvalidCipherTextException("invalid ivCiphertext length"); } byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16); byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length); 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:de.gs_sys.kp2016.crypto.SymmetricCipher.java
License:Open Source License
public static byte[] encrypt(byte[] key, byte[] in) { key = extendKey(key);//from w w w .ja v a 2 s . co m BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine)); cipher.init(true, new KeyParameter(key)); byte[] out = new byte[cipher.getOutputSize(in.length)]; int length = cipher.processBytes(in, 0, in.length, out, 0); try { cipher.doFinal(out, length); } catch (Exception ce) { ce.printStackTrace(); return null; } return out; }
From source file:de.gs_sys.kp2016.crypto.SymmetricCipher.java
License:Open Source License
/** * May Trim 0-bytes at the end, witch are needed ! * @param key/*from www .java 2 s . co m*/ * @param cipherText */ public static byte[] decrypt(byte[] key, byte[] cipherText) throws DecryptionException { key = extendKey(key); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine)); cipher.init(false, new KeyParameter(key)); byte[] out = new byte[cipher.getOutputSize(cipherText.length)]; int length = cipher.processBytes(cipherText, 0, cipherText.length, out, 0); try { cipher.doFinal(out, length); } catch (Exception ce) { // ce.printStackTrace(); throw new DecryptionException(SymmetricCipher.class.getName()); } int t = -1; for (int i = out.length - 1; i > 0; i--) { if (out[i] == 0) t = i; else break; } if (t >= 0) { byte[] out2 = new byte[t]; System.arraycopy(out, 0, out2, 0, t); return out2; } return out; }
From source file:de.tntinteractive.portalsammler.engine.CryptoHelper.java
License:Open Source License
private static PaddedBufferedBlockCipher initAes(final byte[] key, final SecureRandom srand, final boolean forEncryption) { final CipherParameters cipherParams = new ParametersWithRandom(new KeyParameter(key), srand); final AESFastEngine aes = new AESFastEngine(); final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(aes)); cipher.init(forEncryption, cipherParams); return cipher; }
From source file:dorkbox.util.crypto.AesTest.java
License:Apache License
@SuppressWarnings("deprecation") @Test//www . ja v a 2 s . co m public void AesBlock() throws IOException { byte[] bytes = "hello, my name is inigo montoya".getBytes(); SecureRandom rand = new SecureRandom(entropySeed.getBytes()); PaddedBufferedBlockCipher aesEngine = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESFastEngine())); byte[] key = new byte[32]; byte[] iv = new byte[16]; // note: the IV needs to be VERY unique! rand.nextBytes(key); // 256bit key rand.nextBytes(iv); // 16bit block size byte[] encryptAES = CryptoAES.encrypt(aesEngine, key, iv, bytes, logger); byte[] decryptAES = CryptoAES.decrypt(aesEngine, key, iv, encryptAES, logger); if (Arrays.equals(bytes, encryptAES)) { fail("bytes should not be equal"); } if (!Arrays.equals(bytes, decryptAES)) { fail("bytes not equal"); } }