PaddedBufferedBlockCipher c = new PaddedBufferedBlockCipher(new CFBBlockCipher(new RijndaelEngine(256), 8), new ZeroBytePadding()); c.init(false, new ParametersWithIV(new KeyParameter(keybytes), iv));
withPadding.init(FOR_DECRYPTION, keyAndIV);
new CBCBlockCipher( new ThreefishEngine(ThreefishEngine.BLOCKSIZE_1024)), new PKCS7Padding()); cipher.init(encrypt, params); ByteArrayOutputStream baos = new ByteArrayOutputStream(); CipherOutputStream cos = new CipherOutputStream(baos, cipher);
_cipher.init(true, new KeyParameter(keyByte));
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)];
/** 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); }
public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) { try { 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); } }
public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) { try { 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); } }
/** * encrypt data as AES. * * @param plain plain data * @param key key data * @return encrypted data * @throws InvalidCipherTextException InvalidCipherTextException */ static byte[] encrypt(byte[] plain, byte[] key) throws InvalidCipherTextException { PaddedBufferedBlockCipher bbc = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); KeyParameter kp = new KeyParameter(key); byte[] ivBytes = new byte[AES_KEYLENGTH / 8]; SecureRandom prng = new SecureRandom(); prng.nextBytes(ivBytes); bbc.init(true, new ParametersWithIV(kp, ivBytes)); byte[] encData = new byte[bbc.getOutputSize(plain.length)]; int len = bbc.processBytes(plain, 0, plain.length, encData, 0); len += bbc.doFinal(encData, len); byte[] ivEncData = new byte[len + ivBytes.length]; System.arraycopy(ivBytes, 0, ivEncData, 0, ivBytes.length); System.arraycopy(encData, 0, ivEncData, ivBytes.length, encData.length); return ivEncData; }
public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) { try { 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); } }
public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) { try { 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); } }
/** * decrypt data as AES. * * @param ivEncData encrypted data with iv (iv(16) + encData) * @param key key data * @return plain data * @throws InvalidCipherTextException InvalidCipherTextException */ static byte[] decrypt(byte[] ivEncData, byte[] key) throws InvalidCipherTextException { //todo: exception catch for security PaddedBufferedBlockCipher bbc = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); KeyParameter kp = new KeyParameter(key); byte[] ivBytes = new byte[AES_KEYLENGTH / 8]; System.arraycopy(ivEncData, 0, ivBytes, 0, ivBytes.length); byte[] encData = new byte[ivEncData.length - ivBytes.length]; System.arraycopy(ivEncData, ivBytes.length, encData, 0, encData.length); bbc.init(false, new ParametersWithIV(kp, ivBytes)); byte[] plainData = new byte[bbc.getOutputSize(encData.length)]; int len = bbc.processBytes(encData, 0, encData.length, plainData, 0); len += bbc.doFinal(plainData, len); byte[] removePadding = new byte[len]; System.arraycopy(plainData, 0, removePadding, 0, len); return removePadding; }
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)); } }; } };
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)); } }; } };
private void initCiphers(final byte[] key, final byte[] iv) { // get the keyBytes this.keyBytes = new byte[key.length]; System.arraycopy(key, 0, this.keyBytes, 0, key.length); this.keyP = new KeyParameter(this.keyBytes); // get the IV this.IV = new byte[BLOCK_SIZE]; System.arraycopy(iv, 0, this.IV, 0, this.IV.length); // create the ciphers // AES block cipher in CBC mode with ISO7816d4 padding this.encryptCipher = new PaddedBufferedBlockCipher( new CBCBlockCipher( new AESEngine() ), new ISO7816d4Padding() ); this.decryptCipher = new PaddedBufferedBlockCipher( new CBCBlockCipher( new AESEngine() ), new ISO7816d4Padding() ); // create the IV parameter final ParametersWithIV parameterIV = new ParametersWithIV(this.keyP, this.IV); this.encryptCipher.init(true, parameterIV); this.decryptCipher.init(false, parameterIV); }
/** * 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); }
/** * 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); } }
/** * Get a cipher * * @param mode encryption or decryption * @param iv a 16 byte iv * @return an initialised Cipher */ PaddedBufferedBlockCipher getCipher(CMode mode, byte[] iv) { PaddedBufferedBlockCipher result = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); result.init(mode.getEncrypt(), new ParametersWithIV(new KeyParameter(getKey()), iv)); return result; }
/** * encrypt data as AES. * * @param plain plain data * @param key key data * @param ivBytes iv data * @return encrypted data * @throws InvalidCipherTextException InvalidCipherTextException */ public static byte[] encrypt(byte[] plain, byte[] key, byte[] ivBytes) throws InvalidCipherTextException { PaddedBufferedBlockCipher bbc = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); KeyParameter kp = new KeyParameter(key); bbc.init(true, new ParametersWithIV(kp, ivBytes)); byte[] encData = new byte[bbc.getOutputSize(plain.length)]; int len = bbc.processBytes(plain, 0, plain.length, encData, 0); len += bbc.doFinal(encData, len); byte[] removePadding = new byte[len]; System.arraycopy(encData, 0, removePadding, 0, len); return removePadding; }
/** * decrypt data as AES. * * @param encData encrypted data * @param key key data * @return plain data * @throws InvalidCipherTextException InvalidCipherTextException */ public static byte[] decrypt(byte[] encData, byte[] key, byte[] ivBytes) throws InvalidCipherTextException { PaddedBufferedBlockCipher bbc = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); KeyParameter kp = new KeyParameter(key); bbc.init(false, new ParametersWithIV(kp, ivBytes)); byte[] plainData = new byte[bbc.getOutputSize(encData.length)]; int len = bbc.processBytes(encData, 0, encData.length, plainData, 0); len += bbc.doFinal(plainData, len); byte[] removePadding = new byte[len]; System.arraycopy(plainData, 0, removePadding, 0, len); return removePadding; } }