public AesCipher(byte[] key, byte[] iv) { this.key = key; this.iv = iv; this.zeroIv = new IvParameterSpec(iv); this.keySpec = new SecretKeySpec(key, KEY_ALGORITHM); }
public CombinedCipherOutputStream(OutputStream out, Cipher asym, String algorithm) throws IOException, GeneralSecurityException { super(out); // create a new symmetric cipher key used for this stream String keyAlgorithm = getKeyAlgorithm(algorithm); SecretKey symKey = KeyGenerator.getInstance(keyAlgorithm).generateKey(); // place the symmetric key by encrypting it with asymmetric cipher out.write(asym.doFinal(symKey.getEncoded())); // the rest of the data will be encrypted by this symmetric cipher Cipher sym = Secret.getCipher(algorithm); sym.init(Cipher.ENCRYPT_MODE,symKey, keyAlgorithm.equals(algorithm) ? null : new IvParameterSpec(symKey.getEncoded())); super.out = new CipherOutputStream(out,sym); }
/** * Upgrades a connection with transport encryption by the specified symmetric cipher. * * @return * A new {@link Connection} object that includes the transport encryption. */ public Connection encryptConnection(SecretKey sessionKey, String algorithm) throws IOException, GeneralSecurityException { Cipher cout = Cipher.getInstance(algorithm); cout.init(Cipher.ENCRYPT_MODE, sessionKey, new IvParameterSpec(sessionKey.getEncoded())); CipherOutputStream o = new CipherOutputStream(out, cout); Cipher cin = Cipher.getInstance(algorithm); cin.init(Cipher.DECRYPT_MODE, sessionKey, new IvParameterSpec(sessionKey.getEncoded())); CipherInputStream i = new CipherInputStream(in, cin); return new Connection(i,o); }
private byte[] aes(byte[] data, byte[] key, byte[] iv, int mode) throws Exception { Cipher cipher = Cipher.getInstance(AES_MODE); cipher.init(mode, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); return cipher.doFinal(data); }
private static final int iterations = 2000; private static final int keyLength = 256; private static final SecureRandom random = new SecureRandom(); random.nextBytes(ivBytes); return new IvParameterSpec(ivBytes);
protected void init() { try { if (iv == null) { iv = new byte[getIvLength()]; rng.nextBytes(iv); } cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (InvalidAlgorithmParameterException e) { throw new RuntimeException(e); } initialized = true; }
@Override public void init(byte[] key, byte[] iv) throws IOException { Preconditions.checkNotNull(key); Preconditions.checkNotNull(iv); contextReset = false; try { cipher.init(mode, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); } catch (Exception e) { throw new IOException(e); } }
final SecretKeySpec symKey = new SecretKeySpec(symKeyData, "AES"); rnd.nextBytes(ivData); final IvParameterSpec iv = new IvParameterSpec(ivData); final SecretKeySpec symKey = new SecretKeySpec(symKeyData, "AES"); final IvParameterSpec iv = new IvParameterSpec(ivData);
protected void init() { try { if (iv == null) { throw new NullPointerException("IV is null"); } cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (InvalidAlgorithmParameterException e) { throw new RuntimeException(e); } initialized = true; }
cipher.init(Cipher.DECRYPT_MODE, secretSpec, new IvParameterSpec(ivBytes)); sr.nextBytes(salt); return new String(salt);
@Nullable public static String encrypt(String plainText, String encryptionKey, String salt) { try { @SuppressWarnings("CIPHER_INTEGRITY") Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8"))); return Hex.encodeToString(cipher.doFinal(plainText.getBytes("UTF-8"))); } catch (Exception e) { LOG.error("Could not encrypt value.", e); } return null; }
byte[] iv = new byte[16]; SecureRandom random = new SecureRandom(); random.nextBytes(iv); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); AesCipher.init(Cipher.ENCRYPT_MODE, SecKey, ivParameterSpec);
private CryptBuf(int mode, SecretKey sharedSecret) throws GeneralSecurityException { cipher = Cipher.getInstance("AES/CFB8/NoPadding"); // NON-NLS cipher.init(mode, sharedSecret, new IvParameterSpec(sharedSecret.getEncoded())); }
public static String encrypt(String value, String secretKey, byte[] initVector) throws Exception { IvParameterSpec ivSpec = new IvParameterSpec(initVector); Cipher cipher = getCipher(); cipher.init(Cipher.ENCRYPT_MODE, loadKey(secretKey), ivSpec); byte[] encrypted = cipher.doFinal(value.getBytes(TEXT_ENCODING)); return Base64.encodeBase64String(encrypted); }
private final Cipher getCoder(final boolean generateIV) throws IOException, GeneralSecurityException { if (generateIV) { rnd.nextBytes(ivEncoder); enc.init(Cipher.ENCRYPT_MODE, encSecretCipher, new IvParameterSpec(ivEncoder)); } return enc; }
@Override public final long open(DataSpec dataSpec) throws IOException { Cipher cipher; try { cipher = getCipherInstance(); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new RuntimeException(e); } Key cipherKey = new SecretKeySpec(encryptionKey, "AES"); AlgorithmParameterSpec cipherIV = new IvParameterSpec(encryptionIv); try { cipher.init(Cipher.DECRYPT_MODE, cipherKey, cipherIV); } catch (InvalidKeyException | InvalidAlgorithmParameterException e) { throw new RuntimeException(e); } DataSourceInputStream inputStream = new DataSourceInputStream(upstream, dataSpec); cipherInputStream = new CipherInputStream(inputStream, cipher); inputStream.open(); return C.LENGTH_UNSET; }
public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); return decrypt(data, zeroIv, key); }
/** * Returns a {@link Cipher} object for decrypting with this key using the provided initialization vector. * @param iv the initialization vector * @return the cipher */ @Restricted(NoExternalUse.class) // TODO pending ApI public Cipher decrypt(byte[] iv) { try { Cipher cipher = Secret.getCipher(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, getKey(), new IvParameterSpec(iv)); return cipher; } catch (GeneralSecurityException e) { throw new AssertionError(e); } }
/** * @param keyLength * Block size of the asymmetric cipher, in bits. I thought I can get it from {@code asym.getBlockSize()} * but that doesn't work with Sun's implementation. */ public CombinedCipherInputStream(InputStream in, Cipher asym, String algorithm, int keyLength) throws IOException, GeneralSecurityException { super(in); String keyAlgorithm = getKeyAlgorithm(algorithm); // first read the symmetric key cipher byte[] symKeyBytes = new byte[keyLength/8]; new DataInputStream(in).readFully(symKeyBytes); SecretKey symKey = new SecretKeySpec(asym.doFinal(symKeyBytes),keyAlgorithm); // the rest of the data will be decrypted by this symmetric cipher Cipher sym = Secret.getCipher(algorithm); sym.init(Cipher.DECRYPT_MODE,symKey, keyAlgorithm.equals(algorithm) ? null : new IvParameterSpec(symKey.getEncoded())); super.in = new CipherInputStream(in,sym); }
ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[IV_LENGTH]; SecureRandom random = new SecureRandom(); random.nextBytes(iv); ecipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv)); byte[] enc = ecipher.doFinal(utf8);