Search in sources :

Example 11 with Cipher

use of javax.crypto.Cipher in project OpenAttestation by OpenAttestation.

the class HisPrivacyCAWebService2ImplTest method encryptRSA.

public static byte[] encryptRSA(byte[] text, PublicKey pubRSA) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA", "BC");
    cipher.init(Cipher.ENCRYPT_MODE, pubRSA);
    return cipher.doFinal(text);
}
Also used : Cipher(javax.crypto.Cipher)

Example 12 with Cipher

use of javax.crypto.Cipher in project Signal-Android by WhisperSystems.

the class DecryptingPartInputStream method initializeCipher.

private Cipher initializeCipher(SecretKeySpec key) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, IOException {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = readIv(cipher.getBlockSize());
    cipher.init(Cipher.DECRYPT_MODE, key, iv);
    return cipher;
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 13 with Cipher

use of javax.crypto.Cipher in project Signal-Android by WhisperSystems.

the class EncryptingPartOutputStream method initializeCipher.

private Cipher initializeCipher(Mac mac, SecretKeySpec key) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] ivBytes = cipher.getIV();
    mac.update(ivBytes);
    super.write(ivBytes, 0, ivBytes.length);
    return cipher;
}
Also used : Cipher(javax.crypto.Cipher)

Example 14 with Cipher

use of javax.crypto.Cipher in project Signal-Android by WhisperSystems.

the class MasterCipher method encryptBytes.

public byte[] encryptBytes(byte[] body) {
    try {
        Cipher cipher = getEncryptingCipher(masterSecret.getEncryptionKey());
        Mac mac = getMac(masterSecret.getMacKey());
        byte[] encryptedBody = getEncryptedBody(cipher, body);
        byte[] encryptedAndMacBody = getMacBody(mac, encryptedBody);
        return encryptedAndMacBody;
    } catch (GeneralSecurityException ge) {
        Log.w("bodycipher", ge);
        return null;
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) Mac(javax.crypto.Mac)

Example 15 with Cipher

use of javax.crypto.Cipher in project Signal-Android by WhisperSystems.

the class MasterSecretUtil method getCipherFromPassphrase.

private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int iterations, int opMode) throws GeneralSecurityException {
    SecretKey key = getKeyFromPassphrase(passphrase, salt, iterations);
    Cipher cipher = Cipher.getInstance(key.getAlgorithm());
    cipher.init(opMode, key, new PBEParameterSpec(salt, iterations));
    return cipher;
}
Also used : SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Aggregations

Cipher (javax.crypto.Cipher)626 SecretKey (javax.crypto.SecretKey)168 SecretKeySpec (javax.crypto.spec.SecretKeySpec)166 IvParameterSpec (javax.crypto.spec.IvParameterSpec)130 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)105 InvalidKeyException (java.security.InvalidKeyException)76 SecretKeyFactory (javax.crypto.SecretKeyFactory)75 IOException (java.io.IOException)72 GeneralSecurityException (java.security.GeneralSecurityException)68 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)60 Key (java.security.Key)54 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)54 BadPaddingException (javax.crypto.BadPaddingException)51 SecureRandom (java.security.SecureRandom)44 UnsupportedEncodingException (java.io.UnsupportedEncodingException)43 KeyGenerator (javax.crypto.KeyGenerator)43 PrivateKey (java.security.PrivateKey)41 PublicKey (java.security.PublicKey)40 PBEKeySpec (javax.crypto.spec.PBEKeySpec)36 KeyFactory (java.security.KeyFactory)35