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[] 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; }
private 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; }
byte[] outBytes = new byte[_cipher.getOutputSize(inBytes.length)];
byte[] output = new byte[cipher.getOutputSize(bytes.length)]; int outputlen = cipher.processBytes(bytes, 0, bytes.length, output, 0); try {
byte[] ciphertext = new byte[pbbc.getOutputSize(plaintext.length)]; byte[] decrypted = new byte[pbbc.getOutputSize(ciphertext.length)]; offset = 0; offset += pbbc.processBytes(ciphertext, 0, ciphertext.length, decrypted, offset);
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);
outblock = new byte[cipher.getOutputSize(inblock.length)];
outblock = new byte[cipher.getOutputSize(inblock.length)];
byte[] ciphertext = new byte[aesCBCPadded.getOutputSize(plaintext.length)]; int bytesWrittenOut = aesCBCPadded.processBytes( plaintext, 0, plaintext.length, ciphertext, 0);
int outBlockSize = cipher.getOutputSize(inBlockSize);
int outBlockSize = cipher.getOutputSize(inBlockSize);
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; }
/** * 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; } }
/** * 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; }